-1

Is there a way to reliably segment product images similar to this one? Even just separating the 3 variations from the border would be great. The problem is that the image touches the border, and I don't know how to deal with that! Not all images are alike, so I need something highly adaptable.

enter image description here

These were the results I achieved using https://docs.opencv.org/master/d3/db4/tutorial_py_watershed.html. My code is identical to the tutorial.

Underwear:

enter image description here

Camera:

enter image description here

What I expected to achieve instead, at least for the image containing the underwear and camera equipment, since the other one is a lot more complex, is for every single object in the image that is not touching another object to be selected separately and outlined in blue. It seems some of the underwear were properly selected as I expected (the first one minus the elastic band) and the first one in the second row (perfectly).

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
José Guedes
  • 359
  • 3
  • 13
  • 1
    Have you tried anything already, made any research? Can you show it? – NuLo Sep 10 '21 at 17:55
  • Hi! Thanks a lot for replying. Sorry, I forgot I should describe my attempts. I attempted multiple things, I've lost count by now. The next to last one was this https://stackoverflow.com/questions/60302695/removing-background-color-from-image-opencv-python and the very last one was this https://docs.opencv.org/master/d3/db4/tutorial_py_watershed.html. I thought the last one would work but it didn't. Some parts were correctly detected while most others weren't such as the white bin. Unfortunately I don't understand much theory so I feel like I'm flying completely blind – José Guedes Sep 10 '21 at 18:44
  • can you post some code and the results so that everyone has a better starting point? Or are you only looking for generic pointers to a solution? – NuLo Sep 10 '21 at 18:50
  • Anything that will lead me to the solution, really. As many different algorithms that could work as possible, maybe. I am still trying to make the watershed algorithm work. I already erased the code from the previous attempts but right now my code is identical to the reference link for the watershed algo and it's not working for any of the images I have. Examples: https://ibb.co/5Gg99h3 https://ibb.co/vPYCMcw https://ibb.co/5MHNRdh – José Guedes Sep 10 '21 at 19:21
  • you posted three images there, can you put those in the post with expected results (done by hand)? – NuLo Sep 10 '21 at 20:36
  • Thanks for your guidance! I have amended my post – José Guedes Sep 10 '21 at 20:50
  • Sorry to nitpick, but if you can make the images smaller and post the original image, the expected result and the difference you are having. – NuLo Sep 10 '21 at 20:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236986/discussion-between-nulo-and-jose-guedes). – NuLo Sep 10 '21 at 20:56

1 Answers1

1

You can use contour as you were going for and take it from the outside. Since the borders are white you invert the threshold so you'll have something like this:


import numpy as np
import cv2 as cv

im = cv.imread('5zdA0.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)

cv.imshow('image', imgray)
cv.waitKey(0)

ret, thresh = cv.threshold(imgray, 160, 255, 1)
contours, hierarchy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)


cv.drawContours(imgray, contours, -1, (0,255,0), 3)
cv.imshow('image', imgray)
cv.waitKey(0)

You'll have to tune these parameters for your images but this should get you going

NuLo
  • 1,298
  • 1
  • 11
  • 16