【问题标题】:Trying to find a pixel within a specific range (using for loop) using OpenCV尝试使用 OpenCV 查找特定范围内的像素(使用 for 循环)
【发布时间】:2019-11-28 20:08:20
【问题描述】:

我一直在尝试解决这个问题并陷入困境。我将不胜感激。

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_blueupper_blue

【问题讨论】:

  • 使用cv2.inRange
  • 另外,使用numpy.argwhere 查找白色像素的坐标,即np.argwhere(thresh == 255)

标签: python numpy opencv image-processing


【解决方案1】:

在使用 numpy 数组时,循环遍历图像效率极低。执行上述操作的效率要高得多。只需搜索切片和索引 numpy 数组。 要找到白色像素,

    white_pixels = thresh == 0

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

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2021-05-02
    • 1970-01-01
    相关资源
    最近更新 更多