Question

Here's an image:

enter image description here

I would like to know how i can set the black circle to white and the rest to black.

(So segment the black circle within the white area).

I know i can invert the image and the circle will be white ... but so will the entire black part that is seen in this image.

If i would have to do this in matlab i would do a connected component operation and check the circularity of the BLOBs. Though I have to do this in opencv (javacv to be precise.)

Is there an easy way of doing this in opencv (javacv).

Thx in advance

Was it helpful?

Solution

There is a simple way in OpenCV using findContours() and drawContours(). If you use the hierarchical version of findContours(), you can then look through the hierarchy and draw (fill) the child contour of the white quad only. This has the additional advantage that you can do some sanity checks (e.g. checking the size of the contour to see if it is approximately the size you expect) if necessary. I don't know anything about java or javacv, but maybe you can check out the c++ example for findcontours included with opencv for inspiration?

OTHER TIPS

You can detect image objects on images by using openCV library(through java adapter); for it you will need to train network for circles.

Regarding exactly your case(probably this solution will not be generic) you can split your image on segments, and using as condition - color changing, see pseudo code below:

//build color switching list
List<Point> colorSwitches = ...
for(each horizontal line from image){
    for(each pixel from line){
        if(color of previous pixel != color of current pixel){
            colorSwitches.add(currentPoint)
        }
    }    
}
// so, you have detected margins of your image objects; now we need to merge neighbor pixels into image objects, where image object is collection of margin points(you should create this class)
List<ImageObject> imageObjects = ...
for(each color switch){
    if(current pixel is connected with pixels from existing image objects){
        // returns image object neared with current point
        getRelatedImageObject(imageObjects).add(currentPoint);
    }else{
        imageObjects.add(new ImageObject(currentPixel));
    }
}
// now we have list of objects from image, and we need to match objects

Ok, its general lines how to do what you need, if you will need more exactly I will try to explain more detailed. Also you can contact me directly

Hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top