Introduction to Canny algorithm: cv::Canny

step:

Introduction to Canny algorithm - five steps in CV::Canny
1. Gaussian Blur - GaussianBlur
2. Grayscale conversion - cvtColor
3. Calculate gradient - Sobel/Scharr
4. Non-maximum signal suppression
5. High and low threshold output binary image

Introduction to the Canny algorithm - high and low threshold output binary image
●T1, T2 are thresholds, anything higher than T2 is retained, anything smaller than T1 is discarded, starting from the pixels higher than T2, anything larger than T1 and connected to each other is retained. Finally, an output binary image is obtained.
●The recommended ratio of high and low thresholds is T2: T1 = 3:1/2:1, where T2 is the high threshold and T1 is the low threshold.

API: Cv::Canny
Canny (
InputArray src, // 8-bit input image
OutputArray edges, // Output edge image, usually a binary image, the background is black
double threshold1, // Low threshold, often high 1/2 or 1/3 of the threshold
double threshold2, //high threshold
int aptertureSize, // size of Soble operator, usually 3x3, value 3
bool L2gradient //Select true to indicate L2 for normalization, otherwise use L1 for normalization unified
)

Code:

#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
Mat src, gray_src, dst;
	int t1_val = 50;
	int t1_max = 255;
	const char* outputtitle = "result image";
void Canny_Demo(int, void*);
int main(int argc, char** argv) {
	
	
	src = imread("C:/Users/ThinkPad/Desktop/1.PNG");
	if (!src.data) {
		printf("could not find");
		return -1;
	}
	namedWindow("demo", cv::WINDOW_AUTOSIZE);
	namedWindow(outputtitle, cv::WINDOW_AUTOSIZE);
	imshow("demo", src);
	cvtColor(src, gray_src, COLOR_BGR2GRAY);
	//转换为灰度图
	createTrackbar("Threshold Value", outputtitle, &t1_val, t1_max, Canny_Demo);
	Canny_Demo(0, 0);
	waitKey(0);
	return 0;
}
void Canny_Demo(int, void*) {
	Mat edge_output;
	blur(gray_src, gray_src, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
	Canny(gray_src, edge_output, t1_val, t1_val*2, 3, false);

	//dst.create(src.size(), src.type());
	//将图像的边缘显示,是彩色的边缘。像素为1的时候,将原像素的值copy到上。
	//src.copyTo(dst, edge_output);
	imshow(outputtitle, edge_output);
}

 result:

Guess you like

Origin blog.csdn.net/qq_44808827/article/details/121214287