Search code examples
javaopencvjavacv

identify contours in a image which are having same color using opencv or javacv?


This question is related to my previous question in that question I used color image as input and it identify by using line color but I like to know how to identify that kind of image using gray-scale image. This is the gray-scale input image and have to identify

enter image description here

And I need to identify following objects with its positions (x and y coordinates).

enter image description here

Please can some one explain with simple code example to identify those objects and I need to identify connected lines of those objects as well (As shown in following image).

enter image description here

Please be kind enough to explain this using simple code example.


Solution

  • The concept of solution is the same as with previous question - use dilate and erode:

    Mat src = imread("input.jpg"), tmp;
    
    cvtColor(src, tmp, CV_BGR2GRAY);
    threshold(tmp, tmp, 200, 255, THRESH_OTSU);
    
    Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1, 1));
    dilate(tmp, tmp, element);
    erode(tmp, tmp, element);
    

    Result:

    enter image description here