【问题标题】:OpenCv image inpaint left some marks of inpainted areasOpenCv 图像修复留下了一些修复区域的痕迹
【发布时间】:2021-08-06 04:49:20
【问题描述】:

我有一个带有组件的图像,我创建了一个蒙版并从中提取了红色区域,然后对具有某些阈值大小的组件应用了一些修复... 但是当我应用 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)

这是输入图像:

输出图像:

【问题讨论】:

    标签: python image opencv image-processing computer-vision


    【解决方案1】:

    红色区域周围的蒙版太紧,因此inpaint 采用周边的红黄色。

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

    lower_redupper_red的范围太小,你也可以dilateinpaint_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 以在掩码中包含一些周围区域:

       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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2021-05-20
      • 2011-09-09
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 2013-09-07
      相关资源
      最近更新 更多