opencv 使用python从图像中删除/擦除对象

lymnna71  于 2023-05-18  发布在  Python
关注(0)|答案(2)|浏览(202)

我一直在尝试删除/擦除对象从图像是红色突出显示使用opencv x1c 0d1x
我已经获得了一个更接近的结果,但它只是降低了对象

的透明度,但我希望它使它完全不可见。
代码如下:

import numpy as np
import sys

input = sys.argv[1]

# Load the image
img = cv2.imread(input)

# Define the color range of the object to be removed
lower_red = np.array([0, 0, 200])
upper_red = np.array([50, 50, 255])

# Create a mask that identifies the object to be removed
mask = cv2.inRange(img, lower_red, upper_red)

# Inpaint the object area using the TELEA algorithm
inpaint = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)

# Save the inpainted image
cv2.imwrite(input, inpaint)
ifmq2ha2

ifmq2ha21#

掩模可能在边缘有问题,并且没有覆盖所有红色像素。您可以扩大面罩:

import cv2
import numpy as np
import sys

input = sys.argv[1]

# Load the image
img = cv2.imread(input)

# Define the color range of the object to be removed
lower_red = np.array([0, 0, 150])
upper_red = np.array([100, 100, 255])

# Create a mask that identifies the object to be removed
mask = cv2.inRange(img, lower_red, upper_red)

# Inpaint the object area using the TELEA algorithm
kernel = np.ones((5,5),np.uint8)
dilated_mask = cv2.dilate(mask,kernel,iterations = 1)
inpaint = cv2.inpaint(img, dilated_mask, 3, cv2.INPAINT_TELEA)

# Save the inpainted image
cv2.imwrite('out_'+input, inpaint)
des4xlb0

des4xlb02#

首先,您需要将您的蒙版保存为图像,以便检查它选择红色的效果:

cv2.imwrite("mask.png", mask)

其次,你可能会发现在HSL/HSV颜色空间中定位高度饱和的红色(和绿色等)更准确。所以,看看其他标记为opencv并包含“HSV”的答案。你需要

HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

并且要知道红色围绕零缠绕,因为色调轮是循环的。参见示例here

相关问题