2

INPUT

TARGET OUTPUT

MY WORK

Like I converted my original input to HSV color space image & applied the INRANGE function and found the green & blue lines & now i want to get rid of them and I want the image to look like in output....how shall i now get rid of the lines & replace them by the background color??

Code Snippet:

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)
gadia-aayush
  • 53
  • 1
  • 9
  • As [How to detect colored patches in an image using OpenCV](https://stackoverflow.com/questions/47342025/how-to-detect-colored-patches-in-an-image-using-opencv/47343587#47343587), I do `cv2.threshold` on `S(hsv)` to detect the color. Then `cv.inpaint` to `recover`. Here is my result https://i.stack.imgur.com/mung9.jpg. A better recovery method is needed. – Kinght 金 May 08 '18 at 08:19
  • @Silencer....can u guide me more with my code because my hsv output is not as yours so can u tell me the color ranges??? && how shall i put better recover options?? – gadia-aayush May 08 '18 at 08:42
  • I think it's better to use `inpaint` for this purpose. Just use the mask you already have as inpaintMask (maybe dilating it will give better result). – Rayees May 08 '18 at 10:27
  • Possible duplicate of [Image Processing- Removing Lines from Image using OpenCV Python](https://stackoverflow.com/questions/50213985/image-processing-removing-lines-from-image-using-opencv-python) – Dan Mašek May 08 '18 at 11:07
  • @silencer how did u make canvas & canvas2 so precise & correct... – gadia-aayush May 08 '18 at 11:55
  • See the link I Posted. And I use thresh `20` for `S` – Kinght 金 May 08 '18 at 13:57
  • @Silencer....Thanks a lot!!! – gadia-aayush May 08 '18 at 17:21
  • @Silencer- like the Shadows around Green & Blue lines are not getting retained, so what should I do to retain the shadows around the green & blue lines – gadia-aayush May 09 '18 at 11:59
  • @Silencer- also the white lines are not blended well, how to further improve such that blending is completely proper.... – gadia-aayush May 09 '18 at 12:00

3 Answers3

1

Here is a quick and dirty solution.

  1. Create a mask from manually threshold image containing the lines (mask 1)

enter image description here

  1. Also create a binary inverted image of this mask (mask 2)

enter image description here

  1. Mask the image of the shirt with mask 1

enter image description here

  1. Inpaint the image above using mask 2

enter image description here

The solution definitely can be improved by performing morphological operations on the mask to remove the lines. Share your thoughts as well

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • like the Shadows around Green & Blue lines are not getting retained, so what should I do to retain the shadows around the green & blue lines – gadia-aayush May 09 '18 at 12:01
  • also the white lines are not blended well, how to further improve such that blending of white lines are completely proper. – gadia-aayush May 09 '18 at 12:01
0

By doing something like @jeru-luke said, the result will be like this:

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

Salman
  • 924
  • 1
  • 7
  • 20
  • like the Shadows around Green & Blue lines are not getting retained, so what should I do to retain the shadows around the green & blue lines – gadia-aayush May 09 '18 at 12:02
  • also the white lines are not blended well, how to further improve such that blending of white lines are completely proper. – gadia-aayush May 09 '18 at 12:02
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)
gadia-aayush
  • 53
  • 1
  • 9