Ask Your Question

Separate Color regions using Open CV [closed]

asked 2018-03-14 08:20:57 -0500

I have a set of vector images of same object:

Outline Image 1

Outline Image 1

Outline Image 2

Outline Image 2

Fill Image

Fill Image

What I want to do is identify background, all outlines and all fill areas based on color values and save the information in another file (may be an image). And then provide a tool that will allow the end user to customize the icon however they want by assigning custom colors to all these areas.

What I have done:

I have separated colors and assigned them categories from 0-1 (e.g. 0 to background, 0.1 to first outline and so on), created another image and assigned these categories to alpha channel of the respective pixels. And then in my custom image generation tool, I check for those categories to identify regions and assign them new colors.


The main (or I think the only) problem is the smoothing pixels (pixels that blend between two colors).

  • At first I planned to group the smoothing pixels to the outline color and then apply smoothing after the final image made by image generation tool. with single outline color it worked fine, but with multiple outline colors (as in "Outline Image 2") I am unable to find a decent solution to figure out how to categorize those pixels with there respective outline category. (Obviously the same problem comes up if I merge smoothing pixels with fill colors)
  • Then I decided to assign separate category to smoothing pixels, but then I was unable to figure out what two colors should be used to calculate the smoothing value for the selected smoothing pixel.
    • e.g. In "Fill Image" a smoothing pixel can lie between outline and background, and outline and fill color, but when reading those pixels I cannot tell the two colors surrounding the pixel.
  • I have also tried reading hue value from HSV image hoping that the smoothing pixels might have same hue as any of the two colors but to no use. smoothing pixels have separate hue value.


  1. Is there any way to resolve this smoothing pixels problem?
  2. Is there any other way to achieve the end goal? (Other than the ones I have tried)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Anas Iqbal
close date 2018-03-27 04:57:21.720507


I don't understand where the smoothing problem appears. Are you grouping the pixel simply by color? E.g. all black pixels (value 0) belong to the an outline, in this case I guess the edges are not properly grouped. If this is the case I think you need a region growing algorithm to solve the problem which groups neighbor pixels accordingly to the color (and some more features) applying some threshold. If you set properly the threshold you solve the smoothing pixel problem. Check this tutorial:

Caba gravatar imageCaba ( 2018-03-15 05:09:24 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-03-27 04:56:47 -0500

So here is the solution: Using the first image

  1. Find edges using cv::Canny
  2. Find Contour point from these edges using cv::findContours with CV_RETR_CCOMP flag (this method also returns a hierarchy array)
  3. For all contours ignore if the contour is an edge (hierarchy[3] != -1)
  4. For all valid contours find the color value in the second and third image posted in question and categorize them (use your own data structure to save that information)
  5. Assign new colors to pixels inside contours based on their cattegory
  6. Apply smoothing and you final image is ready
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-03-14 08:20:57 -0500

Seen: 700 times

Last updated: Mar 27 '18