Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在openCV中屏蔽和按位_和_Python_Python 3.x_Opencv_Computer Vision_Mask - Fatal编程技术网

Python 在openCV中屏蔽和按位_和

Python 在openCV中屏蔽和按位_和,python,python-3.x,opencv,computer-vision,mask,Python,Python 3.x,Opencv,Computer Vision,Mask,所以我刚开始学习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_bo

所以我刚开始学习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生成三通道图像以与其他三通道图像连接是否正确?如果不是,那么更好的解决方案是什么
  • 第2行
    result=cv2.bitwise_和(img,img,mask=mask)
    之间有什么区别,如果我只需要提取特定的颜色,哪种方法看起来更好
  • 谢谢

  • 我建议在使用图像通道时使用cv2.split和cv2.merge

  • 如果掩码[索引]值为零,则不会执行该操作