how can I append the coordinates of the white pixels in the picture into arrays? I want the two white lines to be seperated into two different arrays, and then calculate max and min distance between two lines. Im quite new to OpenCV and Python, so any help or code example is greatly appriciated.
Asked
Active
Viewed 1,301 times
1 Answers
2
What's done in the below code is that we use recursion to get all the adjacent whites thus covering a whole 'line'. The recursion is easy, we just need to get the adjacent cells, maintain a check array and the work is done.
Next we need to get them in 2 separate arrays. For that we iterate through the image and pass the first array to the recursive function if it's length is 0 ie nothing has been added to it otherwise the 2nd array is passed.
The code has not been tested I'm sorry. Also this involves concepts such as recursion and is a bit tricky as well. Please notify me in comments if there are any errors or you couldn't understand any part. I'll get back to you at the earliest. Thanks
Your result coordinates are stored in arr1 and arr2.
## let image_arr be your 2d image array
check_arr = numpy.zeros(shape=(rows,cols))
arr1 = []
arr2 = []
def get_neighbour_whites(x,y,arr):
def get_adjacent_cells( self, x_coord, y_coord ):
result = set()
for k,l in [(x_coord+i,y_coord+j) for i in (-1,0,1) for j in (-1,0,1) if i != 0 or j != 0]:
if k>=0 and k<rows and l>=0 and l<cols:
result.add((k,l))
return result
check_arr[x,y] = 1
arr.append((x,y))
adj_cells = get_adjacent_cells(x,y)
for i,j in adj_cells:
if image_arr[i,j]==255 and not check_arr[i,j]:
get_neighbour_whites(i,j,arr)
for x in xrange(rows):
for y in xrange(cols):
if image_arr[x,y] == 255 and not check_arr[x,y]:
get_neighbour_whites(x,y,arr1 if len(arr1)==0 else arr2)

Abhishek J
- 2,386
- 2
- 21
- 22
-
Thanks for the response, this was very helpfull! Almost got it to work, but It seems like all the white points from both lines are stored in array2, and only one point in array1, do you have any idea why? Also got a question about your code, when you have `for i in (-1,0,1)`, why `(-1,0,1)` ? and why is `check_arr[x,y] ` equal `1`?Thanks for you help, really appriciate it :) – agrom Feb 24 '17 at 16:43
-
1Your welcome.. check_arr[x,y] = 1 means we have already visited it and thus need not visit it again. This is to prevent infinite recursion. – Abhishek J Feb 25 '17 at 06:57
-
1(-1,0,1) is for the adjacent points. Check out this thread for more http://stackoverflow.com/questions/2373306/pythonic-and-efficient-way-of-finding-adjacent-cells-in-grid – Abhishek J Feb 25 '17 at 06:58
-
1The image is not binary. When I downed it on my PC it gave the following colour values [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]. You could threshold it to make it only have 0 and 255 as colors and then mostly the code would work – Abhishek J Feb 25 '17 at 07:40
-
Hmm, still can't get it to work. Tried with threshold aswell. Are you sure the part `if len(arr1)==0 else arr2)` is correct, since my program append one point to arr1 and then all the rest to arr2. – agrom Feb 26 '17 at 22:19
-
1Strange its working on my end.. yes thats right.. what it does is it will pass the reference of arr1 to the function and then the function recursively appends all neighbouring white points and so on till the whole line is appended to it. http://pastebin.com/rLrGqFdU Ran this exact code based on my solution with your image and works perfectly check it out.. – Abhishek J Feb 27 '17 at 09:28
-
Got it to work now, it was just me beeing stupid. Thanks for the help :) – agrom Feb 27 '17 at 10:10