首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OpenCv图像内画留下了一些内画区域的痕迹

OpenCv图像内画留下了一些内画区域的痕迹
EN

Stack Overflow用户
提问于 2021-05-17 03:50:04
回答 1查看 209关注 0票数 1

我有一个带有组件的图像,我已经创建了一个蒙版,并从中提取了红色区域,然后在组件上应用了一些修复与一些阈值大小的组件…但是当我应用cv2.inaint时,它会留下一些红色的痕迹...我不知道为什么..。下面是代码。

代码语言:javascript
运行
复制
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)

以下是输入图像:

输出图像:

EN

回答 1

Stack Overflow用户

发布于 2021-05-17 05:32:30

遮罩在红色区域周围太紧,因此inpaint采用周边的红黄颜色。

放大以查看边界与黄色混合在一起:

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

完整的代码示例:

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67560971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档