openCV---边缘检测

来源:互联网 发布:淘宝可以贷款么 编辑:程序博客网 时间:2024/06/06 00:10

原理:
http://www.tuicool.com/articles/Y3q2Mf

#include <opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){    Mat img = imread("F://Visual_Studio项目//openCV//kn.jpg");  // 读入一张图片    if(!img.data)        cout << "图片不存在!" << endl;    imshow("原图片", img);                  // 显示图片    Mat edge, grayImage, dst, abs_dst, grad_x, grad_y, abs_grad_x, abs_grad_y;    cvtColor(img, grayImage, CV_BGR2GRAY);  // 转为灰度图像    blur(grayImage, edge, Size(3, 3));      // 均值滤波操作,使用3*3内核降噪    Canny(edge, edge, 150, 100, 3);         // 运行Canny算子    imshow("Canny边缘检测", edge);      Laplacian( grayImage, dst, CV_16S, 1, 1, 0, BORDER_DEFAULT);  // 运行Laplacian算子     convertScaleAbs( dst, abs_dst );        // 计算绝对值,并将结果转换成8位    imshow("Laplacian变换", abs_dst);     Sobel( img, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT );  // x方向梯度    convertScaleAbs( grad_x, abs_grad_x );      Sobel( img, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT );  // y方向梯度    convertScaleAbs( grad_y, abs_grad_y );      addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, dst );      // 合并梯度    imshow("Sobel边缘检测", dst);    waitKey(0);                     // 等待任意键按下    return 0;}

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

原创粉丝点击