Opncv学习之使用方向滤波器检测边缘

来源:互联网 发布:java map实现内存缓存 编辑:程序博客网 时间:2024/06/07 13:18

Sobel滤波器

void Sobel( InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1, double delta = 0, int borderType = BORDER_DEFAULT ); 

用索贝尔算子进行边缘检测:
计算X和Y方向上的导数并进行相加得到Sobel滤波器的范式
找到范式的最大值和最小值
将范式转换成8位图像,并进行二值化的到边缘

#include <iostream>#include <iomanip>#include <opencv2/core/core.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>int main(){    // Read input image    cv::Mat image = cv::imread("D:/1.jpg", 0);    if (!image.data)        return 0;    // Display the image    cv::namedWindow("Original Image");    cv::imshow("Original Image", image);    // Compute Sobel X derivative    cv::Mat sobelX;    cv::Sobel(image, sobelX, CV_8U, 1, 0, 3, 0.4, 128);    // Display the image    cv::namedWindow("Sobel X Image");    cv::imshow("Sobel X Image", sobelX);    // Compute Sobel Y derivative    cv::Mat sobelY;    cv::Sobel(image, sobelY, CV_8U, 0, 1, 3, 0.4, 128);    // Display the image    cv::namedWindow("Sobel Y Image");    cv::imshow("Sobel Y Image", sobelY);    // Compute norm of Sobel    cv::Sobel(image, sobelX, CV_16S, 1, 0);    cv::Sobel(image, sobelY, CV_16S, 0, 1);    cv::Mat sobel;    //compute the L1 norm    sobel = abs(sobelX) + abs(sobelY);    double sobmin, sobmax;    cv::minMaxLoc(sobel, &sobmin, &sobmax);    // Conversion to 8-bit image    // sobelImage = -alpha*sobel + 255    cv::Mat sobelImage;    sobel.convertTo(sobelImage, CV_8U, -255. / sobmax, 255);    // Display the image    cv::namedWindow("Sobel Image");    cv::imshow("Sobel Image", sobelImage);    // Apply threshold to Sobel norm (low threshold value)    cv::Mat sobelThresholded;    cv::threshold(sobelImage, sobelThresholded, 190, 128, cv::THRESH_BINARY);    // Display the image    cv::namedWindow("Binary Sobel Image (low)");    cv::imshow("Binary Sobel Image (low)", sobelThresholded);    // Apply threshold to Sobel norm (high threshold value)    cv::threshold(sobelImage, sobelThresholded, 190, 255, cv::THRESH_BINARY);    // Display the image    cv::namedWindow("Binary Sobel Image (high)");    cv::imshow("Binary Sobel Image (high)", sobelThresholded);    cv::waitKey();    return 0;}
原创粉丝点击