Opencv3 C++ VS2017 study notes 14 Sobel\Laplance operator

  • Convolution application- image edge extraction
    • Edge: is where the pixel value transitions
    • How to capture the edge: Find the first derivative of the image pixel. Obviously there is f(x, y) here. Find the partial derivative of x. The larger the partial derivative, the greater the change of the pixel in the X axis direction, and the stronger the edge possibility
  • Sobel operator : It is a discrete differential operator, used to calculate the approximate gradient of the image gray level, and is a first-order differential operator
    • The Sobel operator features a collection of Gaussian smoothing and differential derivation. With the Sobel operator, I don’t need to use the higher number method to find the derivative.
      • \large G_{x}=\begin{bmatrix} -1 &0 &1\\ -2& 0&2\\ -1& 0 &1 \end{bmatrix}*A         \large G_{y}=\begin{bmatrix} 1 & 2&1 \\ 0& 0 &0 \\ -1& -2 &-1 \end{bmatrix}*A
      • A is the original image, and Gx&Gy represents the image with the pixel derivatives in the two directions of X and Y. The calculation method is to multiply the corresponding pixels of the two images and then sum them.
      • After getting Gx and Gy, you need to mix Gx and Gy into a graph Gxy,
        • theory: \large G_{xy}=\sqrt{G_{x}^2+G_{y}^2}
        • practice: \large G_{xy}= \left|G_{x}\right|+\left|G_{y}\right|
    • Sobel does not require high accuracy
  • API
    • Sobel(src, dst, depth, dx,dy,kernelSize,scale, delta,borderType)
    • Parameters: source image, output image, depth, x derivative order, y derivative order, Sobel operator dimension, 1, 3, 5, 7, scale, delta, edge processing type
    • Calculate the absolute value convertScaleAbs(A,B), calculate A, and output to B
  • Gaussian blur-turn to gray-Sobel operator derivative-XY composition
    • GaussianBlur is to remove small noises, so the filter\kernel of GaussianBlur does not need to be too large, 3*3 is fine
    • Turn to gray because Sobel is for gray value

 

#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;
using namespace cv;
int main(int argc, char ** argv)
{
	Mat src, gray, bin, dst;
	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);
	imshow("src_image", src);

	GaussianBlur(src, dst, Size(3, 3), 0, 0);
	Mat dst_gray;
	cvtColor(dst, dst_gray, CV_BGR2GRAY);
	imshow("dst__gray_image", dst_gray);
	Mat xgrad, ygrad;
	//同样可使用Scharr算子,即Sobel算子的升级版
	Scharr(dst_gray, xgrad, CV_16S, 1, 0, 3);
	Scharr(dst_gray, ygrad, CV_16S, 0, 1, 3);
	//Sobel(dst_gray, xgrad, CV_16S, 1, 0, 3);
	//Sobel(dst_gray, ygrad, CV_16S, 0, 1, 3);
	convertScaleAbs(xgrad, xgrad);
	convertScaleAbs(ygrad, ygrad);   //Abs绝对值
	imshow("xgrad", xgrad);
	imshow("ygrad", ygrad);
	imshow("dst_image", dst);
	Mat dst_xygrad;
	//XY梯度混合
	//addWeighted(xgrad, 0.5, ygrad, 0.5, 0, dst_xygrad);
	//不适用API来实现
	dst_xygrad = Mat(xgrad.size(), xgrad.type());
	int width = dst_xygrad.cols;
	int height = dst_xygrad.rows;
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			int xg = xgrad.at<uchar>(row, col);
			int yg = ygrad.at<uchar>(row, col);
			int xy = xg + yg;
			dst_xygrad.at<uchar>(row, col) = saturate_cast<uchar>(xy);
		}
	}
	imshow("dst_XYimage", dst_xygrad);
	waitKey(0);
	return 0;
}
  • Laplance operator
    • For the second derivative, the peak point of the first derivative corresponds to the second derivative of 0, and the edge pixel position is extracted according to the second derivative of 0
      • \large Laplace(f)=\frac{\partial ^2f}{\partial x^2}+\frac{\partial ^2f}{\partial y^2}
    • The Laplacian() function obtains the result of the Laplace transform by adding the derivatives in the x and y directions obtained by the Sobel operator, so there is no need to separately find the derivatives and then add them.
      • \large dst=\Delta src=\frac{\partial ^2src}{\partial x^2}+\frac{\partial ^2src}{\partial y^2}
    • API
      • Laplacian(src, dst, depth, ksize, scale, delta,borderType)
      • Parameters: source image, target image, image depth, kernel size, scale factor, delta, boundary type
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;
using namespace cv;
int main(int argc, char ** argv)
{
	Mat src, gray, bin, dst;
	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);
	imshow("src_image", src);
	GaussianBlur(src, dst, Size(3, 3), 0, 0);
	imshow("dst_image", dst);
	Mat dst_gray;
	cvtColor(dst, dst_gray, CV_BGR2GRAY);
	namedWindow("dstGray_image", WINDOW_AUTOSIZE);
	imshow("dstGray_image", dst_gray);
	Mat dstGray, edgImage;
	GaussianBlur(src, dst, Size(3, 3), 0, 0);
	cvtColor(dst, dstGray, COLOR_BGR2GRAY);
	Laplacian(dstGray, edgImage, CV_16S, 3);
	convertScaleAbs(edgImage, edgImage);
	namedWindow("Lap_image", WINDOW_AUTOSIZE);
	imshow("Lap_image", edgImage);
	waitKey(0);
	return 0;
}

 

Guess you like

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