您的位置:首页 > 运维架构

opencv轮廓检测之椭圆检测-----算法篇(2)---Sobel---自定义卷积核问题

2016-04-06 10:38 471 查看
上一篇讲了怎么用sobel算子获取边缘, 本篇要讲opencv怎么自定义卷积核

还是以sobel算子为例

sobel算子

#include <opencv2/opencv.hpp>

#include <iostream>


int main(int argc, char ** argv)

{

if(argc < 2)

return -1;

cv::Mat img = cv::imread(argv[1],0);

cv::Mat dst1 , dst2, dst3 ,dst;

cv::Matx33f kernelx(-1,0,1,

-2,0,2,

-1,0,1);

cv::Matx33f kernely(-1,-2,-1,

0, 0, 0,

1, 2, 1);

cv::flip(kernelx,kernelx,-1);

cv::flip(kernely,kernely,-1);

//    cv::sepFilter2D(img,dst,CV_64F,kernelx,kernely);//kernel.type() == DataType<DT>::type && (kernel.rows == 1 || kernel.cols == 1)) in RowFilter

cv::filter2D(img,dst1,-1,kernelx);

cv::filter2D(img,dst2,-1,kernely);

dst3 = dst1 + dst2;

cv::filter2D(img,dst,-1,kernelx);

cv::filter2D(dst,dst,-1,kernely);


cv::namedWindow("dst1",cv::WINDOW_AUTOSIZE);

cv::namedWindow("dst2",cv::WINDOW_AUTOSIZE);


cv::namedWindow("dst3",cv::WINDOW_AUTOSIZE);

cv::namedWindow("dst",cv::WINDOW_AUTOSIZE);

cv::imshow("dst1",dst1);

cv::imshow("dst2",dst2);

cv::imshow("dst3",dst3);

cv::imshow("dst",dst);

for(;;)

{

if(cv::waitKey(0)==27)

break;


}

return 0;


}











可以看到效果和算法篇(1)有些相反

而我在定义卷积核后进行了180度旋转.

而根据这个教程http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html

判断使用 卷积核 在使用前并不需要进行翻转

(上一篇中处理dst有错误, 请忽略吧)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: