当前位置:主页 > 软件编程 > C代码 >

opencv3/C++图像边缘提取方式

时间:2021-04-09 08:59:37 | 栏目:C代码 | 点击:

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;
}

您可能感兴趣的文章:

相关文章