我有一个带有组件的图像,我已经创建了一个蒙版,并从中提取了红色区域,然后在组件上应用了一些修复与一些阈值大小的组件…但是当我应用cv2.inaint时,它会留下一些红色的痕迹...我不知道为什么..。下面是代码。
lower_red = np.array([0,220,0]) #lower red color limit
upper_red = np.array([100,255,255]) # upper limit
mask = cv2.inRange(hsv,lower_red,upper_red) # masking red colored areas
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(mask, 8) # finding components
sizes = stats[1:, -1]; nb_components = nb_components - 1 # getting areas of each component
min_size = 800 # threshold of component size
inpaint_mask = np.zeros((output.shape))
for i in range(0, nb_components):
if sizes[i] <= min_size:
inpaint_mask[output == i + 1] = 255
inpaint_mask = inpaint_mask.astype('uint8')
dst = cv2.inpaint(src = rgb,inpaintMask=inpaint_mask,inpaintRadius=10, flags = cv2.INPAINT_TELEA)
plt.figure(figsize=(20,20),dpi=80)
plt.imshow(dst)以下是输入图像:

输出图像:

发布于 2021-05-17 05:32:30
遮罩在红色区域周围太紧,因此inpaint采用周边的红黄颜色。
放大以查看边界与黄色混合在一起:

lower_red和upper_red的范围太小,您还可以使用dilate inpaint_mask来包含周围区域(包括遮罩中所有的红黄边界)。
您没有发布代码的第一部分。
我假设hsv是转换为HSV的rgb图像。
lower_red = np.array(0,150,0) #红色下限upper_red = np.array(200,255,255) #上限
inpaint_mask以在遮罩中包含一些周围区域:inpaint_mask =cv2.diplate(inpaint_mask,np.ones((5,5)
完整的代码示例:
import cv2
import numpy as np
bgr = cv2.imread('input.png') # Use BGR instead of RGB for later using cv2.imshow instead of plt.imshow
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
#lower_red = np.array([0, 220, 0]) #lower red color limit
#upper_red = np.array([100, 255, 255]) # upper limit
lower_red = np.array([0, 150, 0]) #lower red color limit
upper_red = np.array([200, 255, 255]) # upper limit
mask = cv2.inRange(hsv, lower_red, upper_red) # masking red colored areas
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(mask, 8) # finding components
sizes = stats[1:, -1]; nb_components = nb_components - 1 # getting areas of each component
min_size = 800 # threshold of component size
inpaint_mask = np.zeros((output.shape))
for i in range(0, nb_components):
if sizes[i] <= min_size:
inpaint_mask[output == (i + 1)] = 255
inpaint_mask = inpaint_mask.astype('uint8')
# Dilate the mask
inpaint_mask = cv2.dilate(inpaint_mask, np.ones((5, 5)))
dst = cv2.inpaint(src=bgr, inpaintMask=inpaint_mask, inpaintRadius=10, flags=cv2.INPAINT_TELEA)
cv2.imshow("dst", dst)
cv2.waitKey()
cv2.destroyAllWindows()https://stackoverflow.com/questions/67560971
复制相似问题