Python 3.x 去除OpenCV Python中的行
就像我将原始输入转换为HSV颜色空间图像并应用INRANGE函数,找到了绿色和蓝色线条&现在我想去掉它们,我想让图像在输出中看起来像……现在我该如何去掉线条并用背景色替换它们 代码段: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 =
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)
这里有一个快速而肮脏的解决方案
通过对掩模执行形态学操作以去除线条,解决方案肯定可以得到改进。分享你的想法像@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)