Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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_Image_Python 2.7_Opencv_Opencv Contour - Fatal编程技术网

Python 填充OpenCV的外部轮廓

Python 填充OpenCV的外部轮廓,python,image,python-2.7,opencv,opencv-contour,Python,Image,Python 2.7,Opencv,Opencv Contour,我正在尝试使用openCV和python语言将轮廓的外部区域涂成黑色。 这是我的密码: contours, hierarchy = cv2.findContours(copy.deepcopy(img_copy),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) areas = [cv2.contourArea(c) for c in contours] max_index = np.argmax(areas) cnt=contours[max_index] # ho

我正在尝试使用openCV和python语言将轮廓的外部区域涂成黑色。 这是我的密码:

contours, hierarchy = cv2.findContours(copy.deepcopy(img_copy),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]
# how to fill of black the outside of the contours cnt please? `

以下是如何在一组轮廓外用黑色填充图像:

import cv2
import numpy
img = cv2.imread("zebra.jpg")
stencil = numpy.zeros(img.shape).astype(img.dtype)
contours = [numpy.array([[100, 180], [200, 280], [200, 180]]), numpy.array([[280, 70], [12, 20], [80, 150]])]
color = [255, 255, 255]
cv2.fillPoly(stencil, contours, color)
result = cv2.bitwise_and(img, stencil)
cv2.imwrite("result.jpg", result)

UPD:上面的代码利用了一个事实,即
按位_和
使用0-s生成0-s,并且不适用于黑色以外的填充颜色。要使用任意颜色填充,请执行以下操作:

import cv2
import numpy

img = cv2.imread("zebra.jpg")

fill_color = [127, 256, 32] # any BGR color value to fill with
mask_value = 255            # 1 channel white (can be any non-zero uint8 value)

# contours to fill outside of
contours = [ numpy.array([ [100, 180], [200, 280], [200, 180] ]), 
             numpy.array([ [280, 70], [12, 20], [80, 150]])
           ]

# our stencil - some `mask_value` contours on black (zeros) background, 
# the image has same height and width as `img`, but only 1 color channel
stencil  = numpy.zeros(img.shape[:-1]).astype(numpy.uint8)
cv2.fillPoly(stencil, contours, mask_value)

sel      = stencil != mask_value # select everything that is not mask_value
img[sel] = fill_color            # and fill it with fill_color

cv2.imwrite("result.jpg", img)

也可以使用另一个图像进行填充,例如,使用
img[sel]=~img[sel]
而不是
img[sel]=fill\u color
将使用轮廓外的相同反转图像进行填充:

import cv2
import numpy
img = cv2.imread("zebra.jpg")
stencil = numpy.zeros(img.shape).astype(img.dtype)
contours = [numpy.array([[100, 180], [200, 280], [200, 180]]), numpy.array([[280, 70], [12, 20], [80, 150]])]
color = [255, 255, 255]
cv2.fillPoly(stencil, contours, color)
result = cv2.bitwise_and(img, stencil)
cv2.imwrite("result.jpg", result)

谢谢兄弟,我很感激。无论何时,只要你在丹佛,告诉我我会给你一个favor@Bill如果这回答了你的问题,请接受/投票。不需要帮忙;D@Miki,别碰我的恩惠,得到时谢绝你的恩惠!:)@我真的很抱歉。。。无论何时你在米兰,请告诉我。。我会帮你一个忙;D@Mvk1312这取决于图像类型。为什么,你想用白色代替黑色?