opencv c + + Learning III (fuzzy edge detection, expansion and corrosion)

catalogue

1. Ambiguity

1.1 fuzzy mean

1.2 Gaussian filtering

2.canny edge detection

3. Expansion and erosion

1. Ambiguity

Smooth/Blur is one of the most common and simplest operations in image processing

The main reason for using this operation is to reduce noise during image preprocessing

Simple principle:

1.1 fuzzy mean

API Description:

The parameters are explained in detail as follows:
The first parameter is src of InputArray type. The input image, that is, the source image, can be filled in with the object of Mat class. This function processes the channels independently and can process pictures with any number of channels, but it should be noted that the depth of the picture to be processed should be CV_8U, CV_16U, CV_16S, CV_32F and CV_ One of 64F.
The second parameter, output array type dst, that is, the target image, needs to have the same size and type as the source image. For example, you can use Mat::Clone to initialize the target image with the source image as the template.
The third parameter, ksize of size type (which will be explained later), is the size of the kernel. Generally, write size (w, h) to represent the size of the kernel (where w is the pixel width and h is the pixel height). Size (3,3) represents the core size of 3x3, and size (5,5) represents the core size of 5x5
The fourth parameter, anchor of Point type, represents the anchor Point (that is, the Point to be smoothed). Note that it has the default value of Point(-1,-1). If the Point coordinate is negative, it means that the center of the core is the anchor Point, so the default value Point(-1,-1) means that the anchor Point is in the center of the core.
The fifth parameter, borderType of int type, is used to infer a certain boundary mode of external pixels of the image. There is a default value of BORDER_DEFAULT, we usually don't care about it.

1.2 Gaussian filtering

Principle of Gaussian filtering

API Description:

Parameter interpretation:
. InputArray src: input image, which can be Mat type and the image depth is CV_8U,CV_16U,CV_16S,CV_32F,CV_64F.
. OutputArray dst: the output image has the same type and size as the input image.
. Size ksize: the size of Gaussian kernel, which is different from the size of the previous two filtering kernels, ksize Width and ksize Height can be different, but the two values must be positive and odd. If the two values are 0, their values will be calculated by sigma.
. double sigma X: standard deviation of Gaussian kernel function in X direction
. double Sigma: the standard deviation of the Gaussian kernel function in the Y direction. If Sigma is 0, the function will automatically set the value of sigma to the same value as sigma X. if both sigma X and sigma y are 0, these two values will be determined by ksize Width and ksize Calculated by height. For details, please refer to the getgaussian kernel () function for details. It is recommended to specify size, semax and semay.
. int borderType=BORDER_DEFAULT: a convenient mode for inferring external pixels of an image, with a default value of BORDER_DEFAULT. If there is no special need to change it, please refer to the borderInterpolate() function for details.

int main(int argc, char** argv) {
	/*Show the picture first*/
	string path = "resouces\\test.png";
	Mat src = imread(path);
	if (src.empty()) {
		cout << "could not load image!" << endl;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);

	
	Mat blur_dst,Gaussianblur_dst;
	blur(src, blur_dst, Size(5, 5), Point(-1, -1));                  //Mean fuzzy
	GaussianBlur(src, Gaussianblur_dst, Size(5, 5), 11, 11);         //Gaussian blur
	imshow("blur", blur_dst);
	imshow("Gaussianblur", Gaussianblur_dst);
	waitKey(0);
	return 0;
}

2.canny edge detection

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

/// Basic Functions //

void main() {

	string path = "resouces\\test.png";
	Mat img = imread(path);
	Mat imgGray, imgBlur, imgCanny;

	cvtColor(img, imgGray, COLOR_BGR2GRAY);                         //Change to grayscale
	GaussianBlur(imgGray, imgBlur, Size(7, 7), 5, 0);               //Gaussian blur
	Canny(imgBlur, imgCanny, 25, 75);								//canny edge detection

	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));

	imshow("Image", img);
	imshow("Image Gray", imgGray);
	imshow("Image Blur", imgBlur);
	imshow("Image Canny", imgCanny);
	waitKey(0);
}

 

3. Expansion and erosion

#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<math.h>
#include<iostream>
using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	/*Show the picture first*/
	string path = "resouces\\test.png";
	Mat src = imread(path);
	if (src.empty()) {
		cout << "could not load image!" << endl;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);


	Mat Gray_dst, Gaussianblur_dst,Canny_dst,Dil_dst,Ero_dst,kernel;
	cvtColor(src, Gray_dst, CV_BGR2GRAY);
	GaussianBlur(Gray_dst, Gaussianblur_dst, Size(5, 5), 11, 11);  //Gaussian blur
	Canny(Gaussianblur_dst, Canny_dst, 25, 75);                    //Canny edge test
	kernel = getStructuringElement(MORPH_RECT, Size(3, 3));        //Make a kernel kernel
	dilate(Canny_dst, Dil_dst, kernel);                            //expand
	erode(Dil_dst, Ero_dst, kernel);                               //erode
	imshow("Gaussianblur", Gaussianblur_dst);
	imshow("Canny", Canny_dst);
	imshow("Dil", Dil_dst);
	imshow("Ero", Ero_dst);
	waitKey(0);
	return 0;
}

Keywords: C++ OpenCV image processing

Added by nickcwj on Mon, 31 Jan 2022 03:59:25 +0200