OpenCV 计算机视觉(十三)各种卷积算子及自定义算子

Robert算子:

  Robert X 算子:                                           Robert Y 算子:

代码:

#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 算子:

    Sobel X 算子:                      Sobel Y 算子:

代码:

#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;
}

猜你喜欢

转载自www.cnblogs.com/haiboxiaobai/p/11241112.html
今日推荐