表示微分最常用的算子是sobel導數算子,sobel算子可以實現任意階導數和混合偏導數
src:源圖像
dst:目標圖像
ddepth:確定目標圖像的深度或類型
xorder,yorder:求導順序,x和y的取值爲(0,1,2),不能同時取0
ksize:表示調用濾波器的寬和高,是一個奇數(目前最大支持到31)
scale:閾值
delta:偏移
void cv::Sobel(src,dst,ddepth,xorder,yorder,ksize,scale,delta,bordertype)
sobel算子的好處:可以定義核的大小,並且可以快速、迭代地構造這些核。壞處是核比較小的時候,準確度不高,此時可以使用scharr濾波器,利用ksize=cv::scharr,即可消除小圖造成的誤差。
代碼實現:
#include<iostream>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img, imgGray,result,dst;
img = imread("C:\\Users\\shawn\\Pictures\\Saved Pictures\\2.JPG");
imshow("原圖", img);
cvtColor(img, imgGray, CV_BGR2GRAY); //必須經過灰度處理,才能進行導數操作
/*imshow("灰度圖", imgGray);
threshold(imgGray, result, 100, 255, CV_THRESH_BINARY);
imshow("二值化圖", result);//二值化
GaussianBlur(img, dst, Size(5, 5), 0, 0);//平滑操作
//顯示效果圖
imshow("高斯濾波【效果圖】", dst);*/
Sobel(imgGray, dst, -1,1,0,3);
imshow("導數【效果圖】", dst);
waitKey();
destroyAllWindows();
return 0;
}