0

I have this inputimage1 which is 20x20 size and looks like this

00000000000000000000
00111000000000000000
01101100000001111000
11000110000001111110
11100000001100011110

and I need a function that will generate an output, say image2 of 20x20 size, that will look like this

00000000000000000000
00111000000000000000
11011000000022220000
11000110000002222220
11000110000002222220
11100000003300022220

The difference is that the first appears to be a grey-scale image (only 0 and 1) while the desired output, based on the similar areas of the input image, will now contain 2,3 and so on.

So far I am looking for some of pillow's build in functions that might suit me but I am not even sure if I am looking in the right direction. Could you please suggest a way to approach this?

2
  • 2
    You should have a look to Flood-Fill-Algorithms they are easy and fast to implement. Commented May 28, 2014 at 11:35
  • @PeterNL I had no clue.. Alright thanks Commented May 28, 2014 at 11:50

2 Answers 2

1

Did you checked this previous post on stackoverflow (Simple object recognition) ?

In sum you can use SciPy's ndimage.label() http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.measurements.label.html#scipy.ndimage.measurements.label

Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

ndimage.label() is the way to go. It essentially performs flood fill. Good answer.
0

Well I would suggest to write a simple function

STEPS :

  1. make a copy of the image filled with zeros and use it as the visited array
  2. perform dfs. You just have to find islands on the image it is a simple graph problem. google for finding islands in a graph.

Hope it helps and you always dont need a named algo to do simple things

Comments