Ask Your Question

Revision history [back]

i am not sure if i understand rigth but maybe cv::erode is solve your problem

image description

image description

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;

int main( int, char** argv )
{
  src = imread("25912.png");
  if (src.empty())
  {
    cerr << "No image supplied ..." << endl;
    return -1;
  }
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );
  const char* source_window = "Source";
  namedWindow( source_window, WINDOW_AUTOSIZE );


  Mat canny_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  // you can change parameters to your result 
  erode(src_gray,src_gray, Mat(), Point(-1,-1),5);

  Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  for( size_t i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( 0,255,0 );
       drawContours( src, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
     }
  imshow( source_window, src );

  waitKey(0);
  return(0);
}

i am not sure if i understand rigth but maybe cv::erode is helps to solve your problem

image description

image description

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;

int main( int, char** argv )
{
  src = imread("25912.png");
  if (src.empty())
  {
    cerr << "No image supplied ..." << endl;
    return -1;
  }
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );
  const char* source_window = "Source";
  namedWindow( source_window, WINDOW_AUTOSIZE );


  Mat canny_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  // you can change parameters to your result 
  erode(src_gray,src_gray, Mat(), Point(-1,-1),5);

  Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  for( size_t i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( 0,255,0 );
       drawContours( src, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
     }
  imshow( source_window, src );

  waitKey(0);
  return(0);
}

i am not sure if i understand rigth but maybe cv::erode helps to solve your problem

image description

image description

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;

int main( int, char** argv )
{
  src = imread("25912.png");
  if (src.empty())
  {
    cerr << "No image supplied ..." << endl;
    return -1;
  }
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );
  const char* source_window = "Source";
  namedWindow( source_window, WINDOW_AUTOSIZE );


  Mat canny_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  // you can change parameters to your result 
  erode(src_gray,src_gray, Mat(), Point(-1,-1),5);

  Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  for( size_t i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( 0,255,0 );
       drawContours( src, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
     }
  imshow( source_window, src );

  waitKey(0);
  return(0);
}

your image's result: image description