OpenCV學習14--自定義線性濾波
阿新 • • 發佈:2019-01-11
卷積
模糊影象,影象邊緣,增強影象
常見卷積運算元:
Robert、sobel、拉普拉斯運算元
程式碼:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
int main(int argc,char ** argv)
{
Mat src,dst;
Mat kernel;
char outwindow1[] = "out1";
char outwindow2[] = "out2";
char outwindow3[] = "out3";
char outwindow4[] = "out4";
char outwindow5[] = "out5";
char outwindow6[] = "out6";
src = imread("1.jpg");
namedWindow("input window",CV_WINDOW_AUTOSIZE);
imshow("input window",src);
namedWindow(outwindow1,CV_WINDOW_AUTOSIZE);
namedWindow(outwindow2,CV_WINDOW_AUTOSIZE);
namedWindow(outwindow3,CV_WINDOW_AUTOSIZE);
namedWindow(outwindow4,CV_WINDOW_AUTOSIZE);
namedWindow(outwindow5,CV_WINDOW_AUTOSIZE);
namedWindow(outwindow6,CV_WINDOW_AUTOSIZE);
Mat kernel_x = (Mat_<int >(2,2)<<1,0,0,-1);
filter2D(src,dst,-1,kernel_x,Point(-1,-1),0.0);
imshow(outwindow1,dst);
Mat kernel_y = (Mat_<int>(2,2)<<0,1,-1,0);
filter2D(src,dst,-1,kernel_y,Point(-1,-1),0.0);
imshow(outwindow2,dst);
//左右的差異
Mat kernel2_x = (Mat_<int>(3,3)<<-1 ,0,1,-2,0,2,-1,0,1);
filter2D(src,dst,-1,kernel2_x,Point(-1,-1),0.0);
imshow(outwindow3,dst);
//上下差異
Mat kernel2_y = (Mat_<int>(3,3)<<-1,-2,1,0,0,0,1,2,1);
filter2D(src,dst,-1,kernel2_y,Point(-1,-1),0.0);
imshow(outwindow4,dst);
//拉普拉斯運算元
Mat kernel3 = (Mat_<int>(3,3)<<0,-1,0,-1,4,-1,0,-1,0);
filter2D(src,dst,-1,kernel3,Point(-1,-1),0.0);
imshow(outwindow5,dst);
int ksize = 0;
int c = 0;
int index = 0;
while(true)
{
c = waitKey(500);
if((char)c == 27)
{
break;
}
ksize = 4 + (index % 8) * 2 + 1;
Mat kernel_auto = Mat::ones(Size(ksize,ksize),CV_32F)/float(ksize*ksize);
filter2D(src,dst,-1,kernel_auto,Point(-1,-1),0.0);
index ++;
imshow(outwindow6,dst);
}
return 0;
}
效果: