简体   繁体   English

使用 Opencv python 从图像中删除蒙版圆圈

[英]Remove mask circle from image using Opencv python

i'm trying to remove the center of image using the BitWish_not but's the result is not good.我正在尝试使用 BitWish_not 移除图像的中心,但结果并不好。 see it below.见下文。

戴口罩

As you see, it didn't remove all the colors如您所见,它并没有删除所有 colors

Below my code在我的代码下面

image = cv2.imread("icons/agta.jpg")
height,width,depth = image.shape
circle = np.zeros((height,width), np.uint8)
cv2.circle(circle,(int(width/2),int(height/2)),90,1,thickness=-1)

masked = cv2.bitwise_not(image, image, mask=circle)

cv2.imshow("masked", masked)
cv2.imwrite("hue.jpg",masked)
cv2.waitKey(0) 

If I understand the problem, here is one way to do it in Python/OpenCV如果我理解这个问题,这是在 Python/OpenCV 中解决问题的一种方法

  • Read the input读取输入
  • Convert to gray转换为灰色
  • Threshold临界点
  • Apply morphology to clean it as a mask应用形态来清洁它作为面膜
  • Invert mask and make 3 channel反转蒙版并制作 3 个通道
  • Apply mask to input to make circle black将蒙版应用于输入以使圆圈变黑
  • Save the results保存结果

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# load image
img = cv2.imread('white_circle.jpg')

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold input image
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# apply morphology open and close and dilate
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (55,55))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, kernel)

# invert mask
mask = 255 -mask

# make mask 3 channel
mask = cv2.merge([mask,mask,mask])

# apply mask to image to blacken circle area
result = img.copy()
result = cv2.bitwise_and(img, mask)

# save resulting masked image
cv2.imwrite('white_circle_thresh.jpg', thresh)
cv2.imwrite('white_circle_mask.jpg', mask)
cv2.imwrite('white_circle_masked.jpg', result)

# display result, though it won't show transparency
cv2.imshow("thresh", thresh)
cv2.imshow("mask", mask)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()


Thresholded image:阈值图像:

在此处输入图像描述

Mask image:蒙版图片:

在此处输入图像描述

Blackened circle in input:输入中的黑圈:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM