摆脱 OpenCV 中的线条-Python
Getting Rid of Lines in OpenCV-Python
就像我将原始输入转换为 HSV 颜色 space 图像并应用 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)
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)
就像我将原始输入转换为 HSV 颜色 space 图像并应用 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)
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)