sobel算子
可以使用扩展的sobel算子,计算一阶,二阶,三阶,或者混合图像差分。
-
C++:
void
Sobel
(InputArray
src, OutputArray
dst, int
ddepth, int
dx, int
dy, int
ksize=3, double
scale=1, double
delta=0, int
borderType=BORDER_DEFAULT
)
-
Python:
cv2.
Sobel
(src, ddepth, dx, dy
[, dst
[, ksize
[, scale
[, delta
[, borderType
]
]
]
]
]
) → dst
-
C:
void
cvSobel
(const CvArr*
src, CvArr*
dst, int
xorder, int
yorder, int
aperture_size=3
)
-
Python:
cv.
Sobel
(src, dst, xorder, yorder, apertureSize=3
) → None
参数:
src:输入图像。
dst:和输入图像相同尺寸,相同通道图像。
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
dx:X方向差分。
dy:Y方向差分。
ksize:扩展sobel核的尺寸,必须是:1,3,5,7.
剩下三个参数可选。
示例代码
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
Mat img = imread("Lena.jpg");
Mat grey;
cvtColor(img, grey, CV_BGR2GRAY);
Mat sobelx;
Sobel(grey, sobelx, CV_32F, 1, 0);
double minVal, maxVal;
minMaxLoc(sobelx, &minVal, &maxVal); //find minimum and maximum intensities
Mat draw;
sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
namedWindow("img", CV_WINDOW_AUTOSIZE);
imshow("img", img);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", draw);
waitKey();//等待按键
return 0;
}
using namespace cv;
int main()
{
Mat img = imread("Lena.jpg");
Mat grey;
cvtColor(img, grey, CV_BGR2GRAY);
Mat sobelx;
Sobel(grey, sobelx, CV_32F, 1, 0);
double minVal, maxVal;
minMaxLoc(sobelx, &minVal, &maxVal); //find minimum and maximum intensities
Mat draw;
sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
namedWindow("img", CV_WINDOW_AUTOSIZE);
imshow("img", img);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", draw);
waitKey();//等待按键
return 0;
}
结果:
原图像,
处理后,