尝试使用 OpenCV 在特定范围内查找像素(使用 for 循环)



我一直试图解决这个问题一段时间,但卡住了。我将不胜感激任何帮助。

ret,thresh = cv2.threshold(res_gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# something to notify us of black or white (after threshold application)
flag = 255
#for loop to find white pixel (scanning columns first - after 100)
for j in range (100, thresh.shape[1]): 
if flag == 0: # check for 'found'
break 
for i in range (0, thresh.shape[0]): 
if thresh[i,j] == 255:
# once black is found, log starting coordinates
starting_Y, starting_X = j, i
# now we are looking for white
flag = 0
break
lower_blue = np.array([70,50,50])
upper_blue = np.array([130,255,255])

上面是我编写的代码示例,用于在二进制图像中查找白色像素。它返回ji这是通过扫描列(然后是行((从第 100 列开始(找到的第一个白色像素的坐标。我想在HSV图像中找到属于特定范围内的细胞。该范围是lower_blueupper_blue

使用 numpy 数组时,循环图像的效率极低。有更有效的方法来执行上述操作。只需搜索有关切片和索引 numpy 数组的信息。 要找到白色像素,

white_pixels = thresh == 0

这将返回一个形状与 thresh 形状相同的布尔数组,在具有白色像素的地方使用 True,否则返回 false。使用相同的方法查找所需范围内的像素。您可以浏览 numpy 文档以了解有关切片和索引数组的更多信息

最新更新