如何使用某种颜色在边界窗口外填充图像

时间:2018-02-02 06:58:05

标签: python-3.x image opencv

是否可以使用opencv,python

填充图像以外的图像填充一些颜色,就像我们在涂料中使用桶填充一样

enter image description here

2 个答案:

答案 0 :(得分:0)

你可以使用洪水填充来做到这一点。您首先需要一个遮罩来从图像中分割出矩形。

首先制作一个遮罩来分割矩形。我是通过简单的灰色阈值做到的。一旦你有了面具,用图像填充roi,剩下的就是你想要的颜色。

enter image description here

代码:

import cv2
import numpy as np 

img = cv2.imread("face.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

h,w =img.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)

ret,thresh2 = cv2.threshold(gray, 10,255,cv2.THRESH_BINARY_INV)
cv2.floodFill(thresh2, mask, (350,250), (255,255,0));

thresh2 = cv2.cvtColor(thresh2,cv2.COLOR_GRAY2RGB)


thresh2[np.where((thresh2 == [0,0,0]).all(axis = 2))] = [0,255,255]
thresh2[np.where((thresh2 == [255,255,255]).all(axis=2))] = img[np.where((thresh2 == [255,255,255]).all(axis=2))]

cv2.imshow("img", thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()

答案 1 :(得分:0)

ALL CREDITS TO I.NEWTON orwhatever his actual name is..  ;)
     img = cv2.imread(f)
     size=img.shape
     dets1 = detector1(img)    
     for k1, d1 in enumerate(dets1):
         x1=d1.left()
         y1=d1.top()
         w1=d1.right()
         h1=d1.bottom()
         cv2.rectangle(img,(x1,y1),(w1,h1),(0,0,0),1)
         cv2.rectangle(img,(0,0),(x1,size[1]),(0,0,0),-1)
         cv2.rectangle(img,(0,0),(size[1],y1),(0,0,0),-1)
         cv2.rectangle(img,(size[1],0),(w1,size[0]),(0,0,0),-1)
         img=cv2.rectangle(img,(size[1],size[0]),(0,h1),(0,0,0),-1)
    cv2.imshow("img", thresh2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()