Opencv3 C++ VS2017 study notes 15 Canny edge detection algorithm

  • Canny algorithm five steps: Canny algorithm is an edge detection algorithm, it is a good edge detector
    • GaussianBlur denoising
    • Grayscale conversion cvtColor
    • Calculate pixel gradient Sobel/Scharr
    • Non-maximum suppression (removal of non-maximum values ​​in x\y direction)
    • High and low threshold output binary image
      • T1, T2 are thresholds, T2: T1 = 3:1 or 2:1, T2 is high threshold, T1 is low threshold
      • >T2 pixels, reserved
      • If the pixel> T1 and the pixel> T2 are connected to each other, the pixel is retained
  • Canny algorithm API
    • Canny(src, edges, threshold1, threshold2, Size, L2gradient);
    • Parameters: original image, image with only edges, low threshold, high threshold, size of Sobel operator (operator for calculating gradient), if L2gradient is true, use L2 to normalize, false to normalize L1
    • Note: Canny() gets a binary image
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;
using namespace cv;
int t1 = 50;
int t2 = t1 * 2;
int max_value = 255;
void cannyCallback(int, void*);
Mat src, gray, bin, dst, canny;
int main(int argc, char ** argv)
{
	
	src = imread("C:\\Users\\xujin\\Desktop\\test1.JPG");
	if (!src.data)
	{
		cout << "no image";
		return -1;
	}
	namedWindow("src_image", WINDOW_AUTOSIZE);
	namedWindow("dst_image", WINDOW_AUTOSIZE);
	namedWindow("dstGray_image", WINDOW_AUTOSIZE);
	namedWindow("canny_image", WINDOW_AUTOSIZE);
	imshow("src_image", src);
	cvtColor(src, gray, COLOR_BGR2GRAY);
	imshow("dstGray_image", gray);
	createTrackbar("cannyT1T2", "canny_image", &t1, max_value, cannyCallback);
	cannyCallback(0, 0);

	waitKey(0);
	return 0;
}
void cannyCallback(int, void*)
{
	blur(gray, bin, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
	Canny(bin, canny, t1,t2, 3, false);
	imshow("canny_image", canny);
}

 

Guess you like

Origin blog.csdn.net/Mrsherlock_/article/details/104594498