1. 示例代码
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <stdio.h>
using namespace cv;
using namespace std;
int edgeThresh = 1;
int edgeThreshScharr = 1;
Mat image, gray, blurImage, edge1, edge2, cedge;
const char* window_name1 = "Edge map : Canny default (Sobel gradient)";
const char* window_name2 = "Edge map : Canny with custom gradient (Scharr)";
static void onTrackbar(int, void*)
{
blur(gray, blurImage, Size(3, 3));
Canny(blurImage, edge1, edgeThresh, edgeThresh * 3, 3);
cedge = Scalar::all(0);
image.copyTo(cedge, edge1);
imshow(window_name1, cedge);
Mat dx, dy;
Scharr(blurImage, dx, CV_16S, 1, 0);
Scharr(blurImage, dy, CV_16S, 0, 1);
Canny(dx, dy, edge2, edgeThreshScharr, edgeThreshScharr * 3, true);
cedge = Scalar::all(0);
image.copyTo(cedge, edge2);
imshow(window_name2, cedge);
}
static void help()
{
printf("\nThis sample demonstrates Canny edge detection\n"
"Call:\n"
" /.edge [image_name -- Default is fruits.jpg]\n\n");
}
const char* keys =
{
"{help h||}{@image |fruits.jpg|input image name}"
};
int main(int argc, const char** argv)
{
help();
CommandLineParser parser(argc, argv, keys);
string filename = parser.get<string>(0);
filename = "right.jpg";
image = imread(samples::findFile(filename), IMREAD_COLOR);
if (image.empty())
{
printf("Cannot read image file: %s\n", filename.c_str());
help();
return -1;
}
cedge.create(image.size(), image.type());
cvtColor(image, gray, COLOR_BGR2GRAY);
namedWindow(window_name1, 1);
namedWindow(window_name2, 1);
createTrackbar("Canny threshold default", window_name1, &edgeThresh, 100, onTrackbar);
createTrackbar("Canny threshold Scharr", window_name2, &edgeThreshScharr, 400, onTrackbar);
onTrackbar(0, 0);
waitKey(0);
return 0;
}
2.示例图像
![在这里插入图片描述](https://img-blog.csdnimg.cn/24a053b291064c4eac5d6b22201f7117.jpg?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pio5pel44CB5LuK5pel44CB5piO5pel,size_15,color_FFFFFF,t_70,g_se,x_16#pic_center)
3.检测结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/0123708aa66043fca136e9e552331306.PNG?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pio5pel44CB5LuK5pel44CB5piO5pel,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)