OpenCV computer vision (xiii) various convolution operators and custom operators

Robert operator:

  Robert X operator:                                            Robert the Y-operators:

Code:

#include<opencv2/opencv.hpp>	
#include<iostream>		

using namespace std;
using namespace cv;

Mat src, robertx, roberty;

int main(int argc, char** argv) {
	src = imread("D:/OpenCVprj/image/test3.jpg");
	imshow("src", src);
	//Robert X 算子
	Mat robert_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);
	filter2D(src, robertx, -1, robert_x, Point(-1, -1), 0.0);
	imshow("robertx", robertx);

	//Robert Y 算子
	Mat robert_y = (Mat_<int>(2, 2) << 0, 1, -1, 0);
	filter2D(src, roberty, -1, robert_y, Point(-1, -1), 0.0);
	imshow("roberty", roberty);

	waitKey(0);
	return 0;
}

 

 

Sobel operator:

    Sobel X operator:                      the Sobel operator the Y:

Code:

#include<opencv2/opencv.hpp>	
#include<iostream>		

using namespace std;
using namespace cv;

Mat src, robertx, roberty, sobel_x, sobel_y;

int main(int argc, char** argv) {
	src = imread("D:/OpenCVprj/image/test3.jpg");
	imshow("src", src);
	//Robert X 算子
	//Mat robert_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);
	//filter2D(src, robertx, -1, robert_x, Point(-1, -1), 0.0);
	//imshow("robertx", robertx);
	//Robert Y 算子
	//Mat robert_y = (Mat_<int>(2, 2) << 0, 1, -1, 0);
	//filter2D(src, roberty, -1, robert_y, Point(-1, -1), 0.0);
	//imshow("roberty", roberty);

	//Sobel X算子
	Mat sobelx = (Mat_<int>(3, 3) << -1, 0, 1, -2, 0, 2, -1, 0, 1);
	filter2D(src, sobel_x, -1, sobelx, Point(-1, -1), 0.0);
	imshow("sobel_x", sobel_x);
	//Sobel Y算子
	Mat sobely = (Mat_<int>(3, 3) << -1, -2, -1, 0, 0, 0, 1, 2, 1);
	filter2D(src, sobel_y, -1, sobelx, Point(-1, -1), 0.0);
	imshow("sobel_y", sobel_y);

	waitKey(0);
	return 0;
}

 

 

拉普拉斯算子:

代码:

#include<opencv2/opencv.hpp>	
#include<iostream>		

using namespace std;
using namespace cv;

Mat src, robertx, roberty, sobel_x, sobel_y, laplance_image;

int main(int argc, char** argv) {
	src = imread("D:/OpenCVprj/image/test3.jpg");
	imshow("src", src);
	//Robert X 算子
	//Mat robert_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);
	//filter2D(src, robertx, -1, robert_x, Point(-1, -1), 0.0);
	//imshow("robertx", robertx);
	//Robert Y 算子
	//Mat robert_y = (Mat_<int>(2, 2) << 0, 1, -1, 0);
	//filter2D(src, roberty, -1, robert_y, Point(-1, -1), 0.0);
	//imshow("roberty", roberty);

	//Sobel X算子
	//Mat sobelx = (Mat_<int>(3, 3) << -1, 0, 1, -2, 0, 2, -1, 0, 1);
	//filter2D(src, sobel_x, -1, sobelx, Point(-1, -1), 0.0);
	//imshow("sobel_x", sobel_x);
	//Sobel Y算子
	//Mat sobely = (Mat_<int>(3, 3) << -1, -2, -1, 0, 0, 0, 1, 2, 1);
	//filter2D(src, sobel_y, -1, sobelx, Point(-1, -1), 0.0);
	//imshow("sobel_y", sobel_y);

	//拉普拉斯算子
	Mat kernel_laplance = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
	filter2D(src, laplance_image, -1, kernel_laplance, Point(-1, -1), 0.0);
	imshow("laplance_image", laplance_image);
	waitKey(0);
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/haiboxiaobai/p/11241112.html