1. 程式人生 > 程式設計 >opencv3/C++影象邊緣提取方式

opencv3/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",&s2,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",src); 

 Mat kernel = (Mat_<int>(2,2)<<0,1,-1,0);
 filter2D(src,kernel,Point(-1,-1),0.0);
 Mat xgrad,ygrad;
 Sobel(src,xgrad,CV_16S,3);
 Sobel(src,ygrad,3);
 convertScaleAbs(xgrad,xgrad);
 convertScaleAbs(ygrad,ygrad);

 addWeighted(xgrad,0.5,dst);
 //addWeighted(dst,dst);
 cvNamedWindow("output",CV_WINDOW_AUTOSIZE);
 imshow("output",dst);
 waitKey();
 return 0;
}

以上這篇opencv3/C++影象邊緣提取方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。