使用核的图像卷积----水平直线的检测

来源:互联网 发布:淘宝怎么做全屏导航 编辑:程序博客网 时间:2024/05/05 10:28

使用核的图像卷积----水平直线的检测

//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 edgesfloat 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 edgesfloat 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 filterfilter2D(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;}

原始图像:


处理后的效果图(水平直线检测):


垂直直线的检测:


0 0