Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Can you post an example image? Maybe you can use some contraints if you know the position of the camera/cube.

Anyway, I see a few problems with your approach:

  • you use the hsv image for the edge detection. The hsv image has less contrast than the original one, so it isn't a good starting point. Worse, you'll have false boundaries when the hue for red goes from 179->0.
  • To make things worse, you blur the image to have even less edges...
  • not related, but you should keep color_mask and mask in grayscale (no need to cv2.merge(...) and mask=cv2.cvtcolor(...).

I suggest to apply edge detection (laplacian, canny or sobel) on the color_mask for each color, then merge these "color edges" too. Something like:

...
edges = np.zeros(hsv.shape, dtype=np.float64)
for color, (lower, upper) in colors.items():
    ...
    color_edge = cv2.Laplacian(color_mask,cv2.CV_64F)
    edges = edges + color_edge
    mask = mask + color_mask