如何将图片中白色像素的坐标附加到数组中? 我希望将两条白线分成两个不同的阵列,然后计算两条线之间的最大和最小距离。 我是OpenCV和Python的新手,所以任何帮助或代码示例都非常适合。
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.
最满意答案
在下面的代码中做的是我们使用递归来获得所有相邻的白色,从而覆盖整个“线”。 递归很简单,我们只需要获取相邻的单元格,维护一个检查数组并完成工作。
接下来我们需要将它们分成2个独立的数组。 为此,我们遍历图像并将第一个数组传递给递归函数,如果它的长度为0,即没有添加任何内容,否则传递第二个数组。
代码尚未经过测试我很抱歉。 这也涉及递归等概念,也有点棘手。 如果有任何错误或您无法理解任何部分,请在评论中通知我。 我会尽早回复你。 谢谢
结果坐标存储在arr1和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)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)更多推荐
发布评论