Python OpenCv图像修复留下了一些修复区域的标记

Python OpenCv图像修复留下了一些修复区域的标记,python,image,opencv,image-processing,computer-vision,Python,Image,Opencv,Image Processing,Computer Vision,我有一个带有组件的图像,我创建了一个遮罩并从中提取出红色区域,然后用一些阈值大小的组件对组件进行修复。。。 但当我使用cv2.inpaint时,它会留下一些红色的痕迹。。。。我不知道为什么。。。 这是代码 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

我有一个带有组件的图像,我创建了一个遮罩并从中提取出红色区域,然后用一些阈值大小的组件对组件进行修复。。。 但当我使用cv2.inpaint时,它会留下一些红色的痕迹。。。。我不知道为什么。。。 这是代码

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)
lower_red=np.array([0220,0])#红色下限
上限=np.数组([100255255])上限
遮罩=cv2。在范围内(hsv,下部红色,上部红色)#遮罩红色区域
nb#U组件,输出,统计,质心=cv2。连接的组件swithstats(掩码,8)#查找组件
大小=统计数据[1:,-1];nb#U组件=nb#U组件-1#获取每个组件的面积
最小尺寸=800#组件尺寸阈值
inpaint_mask=np.zeros((output.shape))
对于范围内的i(0,nb_分量):

如果尺寸[i]则遮罩在红色区域周围太紧,因此
inpaint
采用周长的红黄色

放大以查看周长与黄色混合:

lower\u red
upper\u red
的范围太小,您也可以
inpaint\u mask
以包括周围区域(包括mask中的所有红黄色周长)

您没有发布代码的第一部分。
我假设
hsv
是转换为hsv的
rgb
图像

  • 增加范围以包括所有红色像素,以包括更多的红黄色像素:

     lower_red = np.array([0, 150, 0])  # lower red color limit
     upper_red = np.array([200, 255, 255])  # upper limit
    
  • 扩展
    修复遮罩
    以包括遮罩中的一些周围区域:

     inpaint_mask = cv2.dilate(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()
导入cv2
将numpy作为np导入
bgr=cv2.imread('input.png')#以后使用bgr代替RGB,使用cv2.imshow代替plt.imshow
hsv=cv2.CVT颜色(bgr,cv2.COLOR_BGR2HSV)
#红色下限=np.数组([0,220,0])红色下限
#上限=np.数组([100255255])上限
红色下限=np.数组([0,150,0])#红色下限
上限=np.数组([200,255,255])上限
遮罩=cv2。在范围内(hsv,下部红色,上部红色)#遮罩红色区域
nb#U组件,输出,统计,质心=cv2。连接的组件swithstats(掩码,8)#查找组件
大小=统计数据[1:,-1];nb#U组件=nb#U组件-1#获取每个组件的面积
最小尺寸=800#组件尺寸阈值
inpaint_mask=np.zeros((output.shape))
对于范围内的i(0,nb_分量):
如果尺寸[i]