Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 去除OpenCV Python中的行_Python 3.x_Opencv_Image Processing_Computer Vision_Opencv3.1 - Fatal编程技术网

Python 3.x 去除OpenCV Python中的行

Python 3.x 去除OpenCV Python中的行,python-3.x,opencv,image-processing,computer-vision,opencv3.1,Python 3.x,Opencv,Image Processing,Computer Vision,Opencv3.1,就像我将原始输入转换为HSV颜色空间图像并应用INRANGE函数,找到了绿色和蓝色线条&现在我想去掉它们,我想让图像在输出中看起来像……现在我该如何去掉线条并用背景色替换它们 代码段: import cv2 as cv import numpy as np img= cv.imread('C:\input.png',1) hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV) lower_green = np.array([30,70,20]) upper_green =

就像我将原始输入转换为HSV颜色空间图像并应用INRANGE函数,找到了绿色和蓝色线条&现在我想去掉它们,我想让图像在输出中看起来像……现在我该如何去掉线条并用背景色替换它们

代码段:

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

hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV)
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)
res = cv.bitwise_and(img,img, mask= mask)
cv.imwrite("out2.jpg", res)

这里有一个快速而肮脏的解决方案

  • 从包含线条的手动阈值图像创建遮罩(遮罩1)
  • 同时创建此遮罩的二进制反转图像(遮罩2)
  • 使用遮罩1遮罩衬衫的图像
  • 使用遮罩2修复上面的图像

  • 通过对掩模执行形态学操作以去除线条,解决方案肯定可以得到改进。分享你的想法

    像@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)
    

    As,我使用
    cv2。阈值
    S(hsv)
    来检测颜色。然后
    cv.inpaint
    recover
    。这是我的结果。需要一种更好的恢复方法。@signer…你能用我的代码指导我吗,因为我的hsv输出与你的不同,所以你能告诉我颜色范围吗??&&如何设置更好的恢复选项?我认为最好使用
    inpaint
    。只需使用你已经拥有的面具作为修复面具(也许放大它会得到更好的效果)。可能重复@消音器你是如何使画布和画布2如此精确和正确的…就像绿色和蓝色线条周围的阴影没有被保留一样,那么,我应该怎么做才能保留绿蓝线周围的阴影呢?既然白线没有很好地混合,那么如何进一步改进以使白线的混合完全正确呢?@Ayushgadia这是相当困难的。我正在研究它,就像绿色和蓝色线条周围的阴影没有被保留一样,那么我应该怎么做才能保留绿色和蓝色线条周围的阴影呢?因此,白色线条没有很好地混合,如何进一步改进以使白色线条的混合完全正确。
    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)