就像我将原始输入转换为HSV色彩空间图像&应用了INRANGE功能,找到了绿色&蓝线&现在我想摆脱它们,我希望图像看起来像输出....我现在怎么摆脱线条&用背景颜色替换它们?
代码段:
@property
答案 0 :(得分:1)
这是一个快速而肮脏的解决方案。
答案 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)
答案 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)