在Canny边算法中只需要一个边 [英] Need only one edge in Canny edge algorithm

查看:289
本文介绍了在Canny边算法中只需要一个边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用canny边算法时,它产生与预期的粗彩色线相对的2条边,但是我只想显示一条边,以使我的线和曲线检测算法复杂得多,任何想法我可以如何做到这一点?

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 ?

这是代码:

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


推荐答案

这个过程称为 thinning

这是一个简单的骨架方法骨架化OpenCV在C#

以下是 output 当应用上面的方法到你的图像时(图像在骨架化之前被反转),因为上述方法适用于 code>,正好与您的输入图片的情况相反)。

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

这篇关于在Canny边算法中只需要一个边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆