Need only one edge in Canny edge algorithm

2019-03-16 05:22发布

问题:

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 ?

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 :

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:

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).