canny算子实现
使用track bar 调整canny算子参数,提取到合适的图像边缘。
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
void trackBar(int, void*);
int s1=0,s2=0;
Mat src, dst;
int main()
{
src = imread("E:/image/image/daibola.jpg");
if(src.empty())
{
printf("can not load image \n");
return -1;
}
cvNamedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
dst = src.clone();
cvNamedWindow("output", CV_WINDOW_AUTOSIZE);
createTrackbar("canny1", "output", &s1, 255, trackBar);
createTrackbar("canny2", "output", &s2, 255, trackBar);
GaussianBlur(src,src,Size(3,3),0);
waitKey();
return 0;
}
void trackBar(int, void*)
{
Canny(src,dst,s1,s2,3);
imshow("output", dst);
}
Sobel算子实现
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat src, dst;
src = imread("E:/image/image/daibola.jpg");
if(src.empty())
{
printf("can not load image \n");
return -1;
}
dst = Mat::zeros(src.size(), src.type());
cvNamedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
Mat kernel = (Mat_<int>(2,2)<<0,1,-1,0);
filter2D(src,dst,-1,kernel,Point(-1,-1),0.0);
Mat xgrad, ygrad;
Sobel(src,xgrad,CV_16S,1,0,3);
Sobel(src,ygrad,CV_16S,0,1,3);
convertScaleAbs(xgrad,xgrad);
convertScaleAbs(ygrad,ygrad);
addWeighted(xgrad,0.5,ygrad,0.5,0,dst);
//addWeighted(dst,0.5,src,1,0,dst);
cvNamedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("output", dst);
waitKey();
return 0;
}
以上这篇opencv3/C++图像边缘提取方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。
您可能感兴趣的文章:C++利用opencv实现人脸检测opencv3/C++图像像素操作详解opencv3/C++ 直方图反向投影实例opencv3/C++关于移动对象的轮廓的跟踪详解opencv3/C++实现霍夫圆/直线检测opencv3/C++图像滤波实现方式opencv3/C++ PHash算法图像检索详解opencv3/C++ 平面对象识别&透视变换方式基于C++实现kinect+opencv 获取深度及彩色数据opencv3/C++视频中叠加透明图片的实现C++使用OpenCV实现证件照蓝底换成白底功能(或其他颜色如红色)详解