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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM