18.Canny边缘检测

时间:2023-03-08 17:18:01

  Canny边缘检测算法以Canny的名字命名,其中Canny的目标是找到一个最优的边缘检测算法,其有三种衡量标准:

  1. 低错误率:标识出尽可能多的实际边缘,同时尽可能的减少噪声产生的误报
  2. 高定位性:标识出的边缘要与图像中的图像中的实际边缘尽可能接近
  3. 最小响应:图像中的边缘只能标识一次,并且可能存在的图像噪声不应标识为边缘
//Canny
int g_nCannySize;
int g_nCannyThresHoldValue;
int g_nSobelValue;
Mat g_srcImage,g_dstImage,g_grayImage,g_edgeImage;
void on_ChangeThresHoldAndCannySize(int,void*)
{
if(g_nSobelValue>2 && g_nSobelValue%2==1)
{
blur(g_grayImage,g_edgeImage,Size(g_nCannySize,g_nCannySize));
Canny(g_edgeImage,g_edgeImage,1,g_nCannyThresHoldValue,3);
g_dstImage=Scalar::all(0);
g_srcImage.copyTo(g_dstImage,g_edgeImage);
imshow("Canny图",g_dstImage);
}
}
void Test_Canny()
{
g_srcImage=imread("D:\\OpenCV Projects\\OpenCV_Test_Image\\7.jpg");
g_dstImage.create(g_srcImage.size(),g_srcImage.type());
cvtColor(g_srcImage,g_grayImage,COLOR_BGR2GRAY);//得到灰度图
namedWindow("原图");
namedWindow("Canny图");
g_nCannySize=1;
g_nCannyThresHoldValue=3;
//g_nSobelValue=3;
createTrackbar("size","Canny图",&g_nCannySize,30,on_ChangeThresHoldAndCannySize);
//createTrackbar("sobel","Canny图",&g_nSobelValue,9,on_ChangeThresHoldAndCannySize);
createTrackbar("value","Canny图",&g_nCannyThresHoldValue,80,on_ChangeThresHoldAndCannySize);
on_ChangeThresHoldAndCannySize(g_nCannySize,0);
on_ChangeThresHoldAndCannySize(g_nCannyThresHoldValue,0);
//on_ChangeThresHoldAndCannySize(g_nSobelValue,0);
imshow("原图",g_srcImage);
}

18.Canny边缘检测