使用Scharr滤波器运算符计算x或y方向的图像差分。其实它的参数变量和Sobel基本上是一样的,除了没有ksize核的大小。
Scharr(src, grad_y, CV_16S, 0, 1, 1, 0, BORDER_DEFAULT);
- 第一个参数,InputArray 类型的src,为输入图像,填Mat类型即可。
- 第二个参数,OutputArray类型的dst,即目标图像,函数的输出参数,需要和源图片有一样的尺寸和类型。
- 第三个参数,int类型的ddepth,输出图像的深度,支持如下src.depth()和ddepth的组合:
-
- 若src.depth() = CV_8U, 取ddepth =-1/CV_16S/CV_32F/CV_64F
- 若src.depth() = CV_16U/CV_16S, 取ddepth =-1/CV_32F/CV_64F
- 若src.depth() = CV_32F, 取ddepth =-1/CV_32F/CV_64F
- 若src.depth() = CV_64F, 取ddepth = -1/CV_64F
- 第四个参数,int类型dx,x方向上的差分阶数。
- 第五个参数,int类型dy,y方向上的差分阶数。
- 第六个参数,double类型的scale,计算导数值时可选的缩放因子,默认值是1,表示默认情况下是没有应用缩放的。我们可以在文档中查阅getDerivKernels的相关介绍,来得到这个参数的更多信息。
- 第七个参数,double类型的delta,表示在结果存入目标图(第二个参数dst)之前可选的delta值,有默认值0。
第八个参数, int类型的borderType,边界模式,默认值为BORDER_DEFAULT。
opencv实例代码如下
#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat src,src1,dst,grad_x, grad_y, abs_grad_x, abs_grad_y;
src1 = imread("1.jpg");
imshow("【原始图】", src1);
resize(src1, src, Size(src1.cols / 2, src1.rows / 2), (0, 0), (0, 0), 3);
Scharr(src, grad_x, CV_16S, 1, 0, 1, 0, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);
imshow("【效果图】X方向的Scharr", abs_grad_x);
Scharr(src, grad_y, CV_16S, 0, 1, 1, 0, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);
imshow("【效果图】Y方向的Scharr", abs_grad_x);
addWeighted(abs_grad_x, 0.5,
abs_grad_y, 0.5,
0,//图1与图2作和后添加的数值
dst);
imshow("【效果图】合并梯度后的Scharr", dst);
waitKey(0);
return 0;
}