/*****************************************************
测试程序 Canny 边缘检测
时间:2016年8月27日
******************************************************/
// Global variables
Mat src, src_gray;
Mat dst, detected_edges; //目标,目标边缘
int edgeThresh = 1; //边缘阈值
int lowThreshold; //最低阈值
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3; //核尺寸
const char* window_name = "Edge Map";
static void CannyThreshold(int, void*)
{
//【1、去噪】
blur( src_gray, detected_edges, Size(3,3) );
//【2、Canny】
//参数:src,dst,low,hight,核尺寸
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
//【3、目标初始化】
dst = Scalar::all(0);
src.copyTo( dst, detected_edges);//【4、复制边缘】
imshow( window_name, dst );
}
int main(void)
{
src = imread("YY01.jpg");
dst.create( src.size(), src.type() ); //【dst空间】
cvtColor( src, src_gray, COLOR_BGR2GRAY );//【灰度化】
namedWindow( window_name, WINDOW_AUTOSIZE );
//【创建控制条】
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
// Start
CannyThreshold(0, 0);
waitKey(0);
return 0;
}
这里根据不同的最小阈值参数,获取不同的边缘