【问题标题】:Masking and bitwise_and in openCVopenCV中的掩码和bitwise_and
【发布时间】:2021-04-16 19:28:27
【问题描述】:

所以我刚开始学习 openCV,我正在学习加入和屏蔽图像。因此,在实际做一些事情时,我遇到了以下代码的两个问题。

    ...
    # HSV values
    lower_bounds = numpy.array([h_min, s_min, v_min])
    upper_bounds = numpy.array([h_max, s_max, v_max])

    # Generating a mask
    mask = cv2.inRange(img_hsv, lower_bounds, upper_bounds)
    # Using this to convert the image into a 3 channel image so as to
    # join it with other images below using the hstack and stack
    # **Line 1**
    th_mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

    # Using bitwise_and with img as src1 and three channel mask as src2
    # **Line 2**
    result = cv2.bitwise_and(img, th_mask)

    horizontal_stack_1 = numpy.hstack((img, img_hsv))
    horizontal_stack_2 = numpy.hstack((th_mask, result))

    vertical_stack = numpy.vstack((horizontal_stack_1, horizontal_stack_2))
    ...

所以我的问题是:

  1. 代码中的第 1 行 是否可以正确生成三通道图像以将其与其他三通道图像连接起来?如果不是更好的解决方案是什么?
  2. Line 2result = cv2.bitwise_and(img, img, mask = mask) 有什么区别,如果我只需要提取特定的颜色,哪种方法更好?

谢谢。

【问题讨论】:

    标签: python python-3.x opencv computer-vision mask


    【解决方案1】:
    1. 我建议在处理图像通道时使用 cv2.split 和 cv2.merge。 detailed explanation.

    2. 如果 mask[index] 值为零,则不会执行该操作。 more

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 2018-03-25
      • 1970-01-01
      相关资源
      最近更新 更多