7
votes

When i use the canny edge algorithm, it produces the 2 edges opposite the thick colored line as expected, but i want only one edge to be displayed so as to make my line and curve detection algorithm much less complicated, any ideas on how i can make that happen ?

enter image description here

Here is the code :

bool CannyEdgeDetection(DataStructure& col)
{

Mat src, src_gray;
Mat dst, detected_edges, fin;
int WhiteCount = 0, BCount = 0;

char  szFil1[32] = "ocv.bmp";
char  szFil2[32] = "dst.bmp";
src = imread(szFil1);
dst = imread(szFil1);
blur( src_gray, detected_edges, Size(3,3) );
Canny( src, dst, 100, 200, 3 );
imwrite(szFil2, dst );

IplImage* img = cvLoadImage(szFil2);
int height    = img->height;
int width     = img->width;
int step      = img->widthStep;
int channels  = img->nChannels;
uchar * datau      = (uchar *)img->imageData;

for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
for(int k=0;k<channels;k++){
datau[i*step+j*channels+k] = 255 - datau[i*step+j*channels+k];   
if (datau[i*step+j*channels+k]==0){
WhiteCount++;
col.pixel_col [i][j] = 2;
}
else{BCount++;
col.pixel_col[i][j] = 0;
}
}
}
}

cvSaveImage("img.bmp" ,img);

return 0;

}

This is not the original image but similar :

enter image description here

Which part do i comment out to be able to read black images in white backgrounds ? or any colored image ?

bool done;
do
{
  cv::morphologyEx(img, temp, cv::MORPH_OPEN, element);
  cv::bitwise_not(temp, temp);
  cv::bitwise_and(img, temp, temp);
  cv::bitwise_or(skel, temp, skel);
  cv::erode(img, img, element);

  double max;
  cv::minMaxLoc(img, 0, &max);
  done = (max == 0);
} while (!done);
1
What have you tried so far? Can you post some code? Or at least the input image?dom
can you upload a link to your original image? May be other people can provide better methods.Abid Rahman K
The dumb-simple method to do it is to resize the image - make it small enough that the edges will be 1-2 pixels. You will have both 1-pixel canny edges and an instant performance boost.Sam

1 Answers

3
votes

That process is called skeletonization or thinning. You can google for that.

Here is a simple method for skeletonization : skeletonization OpenCV In C#

Below is the output I got when applied above method to your image ( Image is inverted before skeletonization because above method work for white images in black background, just opposite case of your input image).

enter image description here