opencv c++ 2

Custom linear filter

l Convolution is an operation in image processing, which is the operation of the kernel on each pixel of the image.

l Kernel is essentially a fixed-size matrix array, the center point of which is called anchor point

Put the kernel on the pixel array, calculate the sum of the product of the pixels covered around the anchor point (including the anchor point), and replace the pixel value under the anchor point coverage, which is called convolution processing.

 

Robert operator

#include "cv.hpp"
#include<iostream>
#include<ctime>
#include<random>
using namespace cv;
using namespace std;

int main()
{
	Mat src, robertimg;
	src = imread("../0.jpg");
	imshow("src", src);
	//robert x方向
	Mat robert_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);
	filter2D(src, robertimg, -1, robert_x, Point(-1, -1), 0.0);
	imshow("robert_x", robertimg);
	//robert y方向
	Mat robert_y = (Mat_<int>(2, 2) << 0, 1, -1, 0);
	filter2D(src, robertimg, -1, robert_y, Point(-1, -1), 0.0);
	imshow("robert_y", robertimg);
	waitKey();
}

 

Sobel operator

#include<random>
using namespace cv;
using namespace std;

int main()
{
	Mat src, sobelimg;
	src = imread("../0.jpg");
	imshow("src", src);
	//sobel x方向
	Mat sobel_x = (Mat_<int>(3, 3) << -1, 0, 1, -2, 0, 2, -1, 0, 1);
	filter2D(src, sobelimg, -1, sobel_x, Point(-1, -1), 0.0);
	imshow("sobel_x", sobelimg);
	//sobel y方向
	Mat sobel_y = (Mat_<int>(3, 3) << -1, -2, -1, 0, 0, 0, 1, 2, 1);
	filter2D(src, sobelimg, -1, sobel_y, Point(-1, -1), 0.0);
	imshow("sobel_y", sobelimg);

	waitKey();
}

Laplacian

#include "cv.hpp"
#include<iostream>
#include<ctime>
#include<random>
using namespace cv;
using namespace std;

int main()
{
	Mat src, laimg;
	src = imread("../0.jpg");
	imshow("src", src);
	Mat kernel = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
	filter2D(src, laimg, -1, kernel, Point(-1, -1), 0.0);
	imshow("拉普拉斯", laimg);
	waitKey();
}

 

Custom convolution blur

#include "cv.hpp"
#include<iostream>
#include<ctime>
#include<random>
using namespace cv;
using namespace std;

int main()
{
	Mat src, img, kernel;
	src = imread("../0.jpg");
	imshow("src", src);
	
	int c = 0;
	int index = 1;
	int ksize;
	while (true) {
		c = waitKey(500);
		if ((char)c == 27)
		{
			break;
		}
		ksize = 3 + 2 * (index % 9);
		kernel = Mat::ones(Size(ksize, ksize), CV_32F)/(float)(ksize*ksize);
		filter2D(src, img, -1, kernel, Point(-1, -1), 0);
		imshow("自定义卷积模糊", img);
		index++;
	}
	waitKey();
}

Convolutional edge problem

When the image is convolved, the boundary pixels cannot be convolved. The reason is that the boundary pixels do not completely overlap the kernel. Therefore, when the 3x3 filter is used, the edge of 1 pixel is not processed, and the edge of 2 pixels is not processed when the 5x5 filter is used. Be processed.

Before increasing the convolution start edge pixel, the pixel value is filled 0 or RGB black, such as 3x3 in

Fill the edges of 1 pixel each around to ensure that the edges of the image are processed.

Then remove these edges. The default processing method in openCV is: BORDER_DEFAULT , in addition

There are also the following commonly used:

 - BORDER_CONSTANT - filling the edge pixels with a specified value

 - BORDER_REPLICATE - filling the edge pixels with a known value of the edge pixel .

 -BORDER_WRAP - Use the pixels on the other side to compensate for the padding

Add edge API to image

copyMakeBorder (
 - Mat the src, input image //
 - Mat dst, // add edge image
 - int top, // edge length, usually about up and down take the same value,
 - int bottom,
 - int left,
 - int right, 
 - int borderType // edge type
 -Scalar value 
)
 

Guess you like

Origin blog.csdn.net/csdn1126274345/article/details/101226743