OpenCV中自定义模板算子并做卷积运算
0赞
发表于 6/23/2018 12:02:37 AM
阅读(2213)
相关测试代码:
#include<opencv2\opencv.hpp> #include<iostream> using namespace cv; using namespace std; int main() { //【1】以灰度格式载入图片并显示 Mat srcImage = imread("picture1.jpg",0); if(srcImage.empty()) { cout << "图片加载失败!!!" << endl; system("pause"); return -1; } imshow("【原始图】",srcImage); //【2】水平方向上的sobel边缘检测并显示 Mat sobelImageX; Mat kernelX = (Mat_<float>(3,3)<<-1,0,1,-2,0,2,-1,0,1); filter2D(srcImage,sobelImageX,CV_8U,kernelX); imshow("【水平方向上的sobel边缘检测图】",sobelImageX); //【3】垂直方向上的sobel边缘检测并显示 Mat sobelImageY; Mat kernelY = (Mat_<float>(3,3)<<-1,-2,-1,0,0,0,1,2,1); filter2D(srcImage,sobelImageY,CV_8U,kernelY); imshow("【垂直方向上的sobel边缘检测图】",sobelImageY); //【4】整体方向上的sobel边缘检测并显示 Mat sobelImage; addWeighted(sobelImageX,0.5,sobelImageY,0.5,0,sobelImage); imshow("【整体方向上的sobel边缘检测图】",sobelImage); //【5】等待键盘任何键按下退出 waitKey(0); return 0; }
测试结果: