[英]Trying to find a pixel within a specific range (using for loop) using OpenCV
一段时间以来,我一直试图解决这个问题,但被卡住了。 我将不胜感激任何帮助。
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])
以上是我编写的用于在二进制图像中查找白色像素的代码示例。 它返回j
, i
,它们是通过扫描列(然后是行)(从第 100 列开始)找到的第一个白色像素的坐标。 我想在 HSV 图像中找到特定范围内的单元格。 该范围是lower_blue
和upper_blue
。
使用 numpy 数组时,循环图像的效率极低。有更有效的方法来执行上述操作。 只需搜索有关切片和索引 numpy 数组的信息。 要找到白色像素,
white_pixels = thresh == 0
这将返回一个与 thresh 形状相同的布尔数组,在白色像素的地方为 True,否则为 false。 使用相同的方法查找所需范围内的像素。 您可以通过 numpy 文档了解有关切片和索引数组的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.