0

I have the following image

enter image description here

I want to identify the main object in this image, which is the blue couch, and remove the background (preferably turn it into white). I use the code below, but it's not really doing the job as You can see in the second image. Is there better ways to do this rather than using opencv?

import cv2
import numpy as np
import matplotlib.pyplot as plt

image_bgr = cv2.imread('path/couch.PNG')
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)

rectangle = (0,0,800,800)

mask = np.zeros(image_rgb.shape[:2], np.uint8)

bgdModel = np.zeros((1,65), np.float64)
fgdModel = np.zeros((1,65), np.float64)

cv2.grabCut(image_rgb, mask, rectangle, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)

mask_2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')

image_rgd_nobg = image_rgb * mask_2[:,:, np.newaxis]

plt.imshow(image_rgd_nobg)
plt.axis('off')
plt.show()

result:

enter image description here

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Parseval
  • 503
  • 1
  • 4
  • 14
  • One approach might be the inverse of the current approach. Perhaps convert the image to HSV and extract the sofa as a mask, using the blue colour range. With a bit of thresholding, this mask can be applied back to the original image to highlight the sofa. Granted, a bit more work will be involved, but that’s one viable approach. And yes, cv2 is the right tool. – S3DEV Jan 17 '23 at 21:25
  • 2
    it's grabcut. it's a low level operation. if you expect AI results, use AI. – Christoph Rackwitz Jan 17 '23 at 21:46
  • Your image is 511x366 or so and your rectangle is 800x800 (larger than your image), so you are not identifying the region of the object relative to your actual image size. – fmw42 Jan 17 '23 at 23:35
  • @S3DEV: Thanks. However, you tell me to use blue color range, but I have many images like this where the sofas (or other furniture) are of different colors. If it's a pillow and a couch on the photo, how does the program know which object to mask? I'm not sure it's that simple to fix if the images vary. – Parseval Jan 18 '23 at 08:04
  • @ChristophRackwitz: What methods/algorithms do you suggest for this? Something like U2NET? Semantic segmentation? – Parseval Jan 18 '23 at 08:05
  • @fmw42: Yes I actually tested several different sizes of that rectangle. But the problem is that I have lot's of images that I need to do this for and sometimes it's the pillow on the couch, sometimes the couch is grey, green etc. – Parseval Jan 18 '23 at 08:07

0 Answers0