//Program to apply a simple filter matrix to an image to detect horizontal edges
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace std; using namespace cv; int main() { Mat img=imread("E:\\图片\\hd1.jpg",CV_LOAD_IMAGE_GRAYSCALE); Mat img_filtered; //Filter kernel for detecting vertical edges float vertical_fk[5][5]={{0,0,0,0,0},{0,0,0,0,0},{-1,-2,6,-2,-1},{0,0,0,0,0},{0,0,0,0,0}}; //Filter kernel for detecting horizontal edges float horizontal_fk[5][5]={{0,0,-1,0,0},{0,0,-2,0,0},{0,0,6,0,0},{0,0,-2,0,0},{0,0,-1,0,0}}; Mat filter_kernel=Mat(5,5,CV_32FC1,horizontal_fk);//检测水平直线 //Mat filter_kernel=Mat(5,5,CV_32FC1,vertical_fk);//检测垂直直线 //apply filter filter2D(img,img_filtered,-1,filter_kernel); namedWindow("Image"); namedWindow("Filtered image"); imshow("Image",img); imshow("Filtered image",img_filtered); while(char (waitKey(1))!='q') { } return 0; }
原始图像:
处理后的效果图(水平直线检测):
垂直直线的检测: