1

I'm working on a software using OpenCV for circles detection. I think that the most important problem is the image. Previously I try to detect circle by HoughCircles with bad results. After, I try to follow the instructions in this post but it doesn't work. Maybe I need some help to pre-processing image. Do anyone have any other ideas for detecting edges?

Original Image :

enter image description here

others similar images: https://i.stack.imgur.com/u5V6l.jpg

Below I have posted the code:

//Global variables
Mat src; Mat src_gray, threshold_output, element,dilated,eroded1, eroded2;
int thresh = 125;
int const max_value = 255;
int const max_BINARY_value = 255;
RNG rng(12345);
int s_ero1 =1;
int s_dil = 2;
int s_ero2 = 1;
int max_s = 50;
string source_window = "Thresh";
string TrackbarName = "Dilated";
string TrackbarName1 = "Eroded1";
string TrackbarName2 = "Eroded2";
/// Function header
void thresh_callback(int, void* );
void dilate_trackbar(int, void*);
void erode_trackbar1(int,void*);
void erode_trackbar2(int,void*);

int main( int, char** argv )
{
    /// Load source image and convert it to gray
    src = imread( "/media/Dati/image01.tif", 1 );


    /// Convert image to gray and blur it
    cvtColor( src, src_gray, COLOR_BGR2GRAY );
    blur( src_gray, src_gray, Size(3,3) );




    /// Create Window

    namedWindow( "source", WINDOW_NORMAL );
    imshow( "source", src );
    waitKey();

    namedWindow( source_window, WINDOW_NORMAL );
    //Create trackbar threshold
    createTrackbar( " Threshold:", source_window, &thresh, max_value, thresh_callback );
    thresh_callback( 0, 0 );
    waitKey();

    namedWindow( TrackbarName1, WINDOW_NORMAL );
    createTrackbar( "Size: ",  TrackbarName1, &s_ero1, max_s, erode_trackbar1);
    erode_trackbar1(0,0);
    waitKey();


    namedWindow( TrackbarName, WINDOW_NORMAL );
    createTrackbar( "Size: ",  TrackbarName, &s_dil, max_s, dilate_trackbar);
    dilate_trackbar(0,0);
    waitKey();

    namedWindow( TrackbarName2, WINDOW_NORMAL );
    createTrackbar( "Size: ",  TrackbarName2, &s_ero2, max_s, erode_trackbar2);
    erode_trackbar2(0,0);
    waitKey();



    return(0);
}


/**
* @function bounding_box
*/
void bounding_box(Mat m){
    int max_point_pos = 0;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    // Find contours
    findContours( m, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );

    cout<<"Numero di blob: "<< contours.size()<<endl;
    for(int i = 1; i < contours.size(); i++){
             max_point_pos = contours[max_point_pos].size() > contours[i].size()? max_point_pos  : i;

    }

    int max_point = contours[max_point_pos].size();
    cout<< "il blob con più punti è associato alla posizione : " << max_point_pos << " con " << max_point << " punti"<< endl;

    /// Approximate contours to polygons + get bounding rects and circles
    vector<vector<Point> > contours_poly( contours.size() );
    vector<Rect> boundRect( contours.size() );
    vector<Point2f>center( contours.size() );
    vector<float>radius( contours.size() );

    for( int i = 0; i < contours.size(); i++ )
     { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
       boundRect[i] = boundingRect( Mat(contours_poly[i]) );
       minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
     }


    /// Draw polygonal contour + bounding rects + circles
    Mat drawing = src.clone();
    for( size_t i = 0; i< contours.size(); i++ )
     {
        if(contours[i].size() > 0.6*max_point){
        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
       //drawContours( drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
       //rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
       circle( drawing, center[i], (int)radius[i], color, 7, 8, 0 );
        }
     }

    /// Show in a window
    namedWindow( "Contours", WINDOW_NORMAL );
    imshow( "Contours", drawing );

}



/**
* @function thresh_callback
*/
void thresh_callback(int, void* )
{


    /// Detect edges using Threshold
    threshold( src_gray, threshold_output, thresh, max_BINARY_value, THRESH_BINARY_INV);
    imshow(source_window, threshold_output);

}


/**
* @function dilate_trackbar
* @brief Callback for trackbar
*/
void dilate_trackbar( int, void* )
{
    dilated = threshold_output.clone();
    element = getStructuringElement(MORPH_ELLIPSE,Size(s_dil, s_dil) , Point(-1,-1));
    dilate(dilated,dilated,element,Point(-1,-1),1);
    imshow(TrackbarName, dilated);
}

/**
* @function erode_trackbar
* @brief Callback for trackbar
*/
void erode_trackbar1( int, void*)
{
    eroded1 = threshold_output.clone();
    element = getStructuringElement(MORPH_ELLIPSE,Size(s_ero1, s_ero1) , Point(-1,-1));
    erode(eroded1,eroded1,element,Point(-1,-1),1);
    imshow(TrackbarName1, eroded1);

}
Community
  • 1
  • 1
C. Ro
  • 11
  • 2
  • What is your question? – dfrib Dec 07 '15 at 16:28
  • are those objects under water? do you have access to multiple images from fixed camera position (for example a video)? – Micka Dec 07 '15 at 16:31
  • @dfri Any others ideas to detect the edges? – C. Ro Dec 07 '15 at 16:41
  • @Micka Unfortunately my only information is the image, I don't know if those object are under water. I dont' have access to multiple images from fixed camera position. – C. Ro Dec 07 '15 at 16:45
  • @C.Ro Sorry, I didn't mean to sound sharp. But it's always good to specify what specifically you want to attain with your questions, here at SO. Adding something along the lines "Do anyone have any other ideas for detecting edges?" prior to your image could be appropriate to edit into your post. – dfrib Dec 07 '15 at 16:47
  • @dfri my bad,you're right. I edited the post. – C. Ro Dec 07 '15 at 16:53
  • so if it's just about detecting circles in that single image I would do it manually... please post a second image of another scene to get a better feeling for the content – Micka Dec 07 '15 at 17:23
  • @Micka I have to detect circles necessarily with OpenCV – C. Ro Dec 07 '15 at 17:30
  • 1
    do you have additional images of different scenes do get a feeling for that content? – Micka Dec 07 '15 at 17:31
  • @Micka I've uploaded additional images. Thanks for the help. – C. Ro Dec 07 '15 at 17:54
  • any speed dependencies? maybe you could adapt hough circle detection by utilizing a high number of different directional gradients – Micka Dec 07 '15 at 18:37
  • @Micka No. I have only this images. – C. Ro Dec 07 '15 at 19:13
  • Do you need to simply classify 'circles present in image?' - or actually locate circle centres? If it's the simple classification case, I'd suggest training some kind of machine learning classifier... – Lamar Latrell Dec 08 '15 at 08:09
  • @LamarLatrell What do you mean exactly when you talk about "classify circles"? – C. Ro Dec 08 '15 at 09:36
  • @C.Ro, well, strictly speaking I never said "classify circles"... I'll try to ask it another way: Do you want a findCircles function like this: bool circlesPresent findCircles(Mat image){}? Or like this: vector circleCoordinates findCircles(Mat image{} ? – Lamar Latrell Dec 11 '15 at 23:46

0 Answers0