一.输入图像
CommandLineParser parser(argc, argv, keys);
string filename = parser.get<string>(0);
image = imread(filename, IMREAD_COLOR);
if(image.empty())
{
printf("Cannot read image file: %s\n", filename.c_str());
help();
return -1;
}
//创建与image同大小和同类型的矩阵
cedge.create(image.size(), image.type());
cvtColor(image, gray, COLOR_BGR2GRAY);
二.核心算法
//使用 3×3 内核降噪
blur(gray, blurImage, Size(3,3));
// Run the edge detector on grayscale
Canny(blurImage, edge1, edgeThresh, edgeThresh*3, 3);
//背景
cedge = Scalar::all(0);
image.copyTo(cedge, edge1);
imshow(window_name1, cedge);
/// Canny detector with scharr
Mat dx,dy;
Scharr(blurImage,dx,CV_16S,1,0);
Scharr(blurImage,dy,CV_16S,0,1);
Canny( dx,dy, edge2, edgeThreshScharr, edgeThreshScharr*3 );
/// Using Canny's output as a mask, we display our result
cedge = Scalar::all(0);
image.copyTo(cedge, edge2);
imshow(window_name2, cedge);
三.输出图像