在OpenCV-Python中摆脱线条

时间:2018-05-08 03:46:39

标签: python-3.x opencv image-processing computer-vision opencv3.1

INPUT

TARGET OUTPUT

MY WORK

就像我将原始输入转换为HSV色彩空间图像&应用了INRANGE功能,找到了绿色&蓝线&现在我想摆脱它们,我希望图像看起来像输出....我现在怎么摆脱线条&用背景颜色替换它们?

代码段:

@property

3 个答案:

答案 0 :(得分:1)

这是一个快速而肮脏的解决方案。

  1. 从包含线条(蒙版1)的手动阈值图像创建蒙版
  2. enter image description here

    1. 同时创建此蒙版的二进制反转图像(蒙版2)
    2. enter image description here

      1. 用面具1掩饰衬衫的图像
      2. enter image description here

        1. 使用遮罩2
        2. 修复上面的图像

          enter image description here

          通过在蒙版上执行形态学操作来移除线条,可以明显改善解决方案。分享你的想法

答案 1 :(得分:0)

通过像@ jeru-luke这样的话说,结果将是这样的:

import cv2 as cv
import numpy as np

img = cv.imread('z12.png', 1)


hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
lower_green = np.array([30, 70, 20])
upper_green = np.array([70, 255, 255])
lower_blue = np.array([95, 110, 20])
upper_blue = np.array([135, 255, 255])
mask = cv.inRange(hsv, lower_green, upper_blue)

mask = cv.bitwise_not(mask)
bk = np.full(img.shape, 255, dtype=np.uint8)  # white bk

fg_masked = cv.bitwise_and(img, img, mask=mask)

# get masked background, mask must be inverted
mask = cv.bitwise_not(mask)
bk_masked = cv.bitwise_and(bk, bk, mask=mask)

# combine masked foreground and masked background
final = cv.bitwise_or(fg_masked, bk_masked)
cv.imwrite('out_put.png', final)
cv.imshow('final', final), cv.waitKey(0)

enter image description here

答案 2 :(得分:0)

import cv2 as cv
import numpy as np
img= cv.imread(r'input.png',1)

hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV)
h,s,v = cv.split(hsv)
th, threshed = cv.threshold(s, 100, 255, cv.THRESH_OTSU|cv.THRESH_BINARY) #black background
mask_w = cv.bitwise_not(threshed)     #white background
fg_masked = cv.bitwise_and(v, v, mask=mask_w) #masking the image of shirt with mask_w
dst = cv.inpaint(fg_masked,threshed,3, cv.INPAINT_NS) #inpainting 

#Dilation & Erosion.
kernel = np.ones((4, 4),np.uint8)
dilation = cv.dilate(dst,kernel,iterations = 2)
erosion = cv.erode(dilation, kernel, iterations=1)
dilation2= cv.dilate(erosion,kernel,iterations = 1)
dilation3= cv.dilate(dilation2,kernel,iterations = 1)
erosion_final = cv.erode(dilation3, kernel, iterations=3)
cv.imwrite("output_2 [improved].png", erosion_final)