边缘检测:
边缘检测算子有很多,Sobel、Laplace、Prewitt、Canny、Marr-Hildresh。
Sobel算子:
Sobel算子是主要用于边缘检测的离散微分算子,它结合了高斯平滑和微分求导,用于计算图像灰度函数的近似梯度。
void Sobel(InputArray src, OutputArray dst, int ddepth, int xorder, int yorder, int ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT )
- src – Source image.
- dst – Destination image of the same size and the same number of channels as src .
- ddepth – Destination image depth.
- xorder – Order of the derivative x.
- yorder – Order of the derivative y.
- ksize – Size of the extended Sobel kernel. It must be 1, 3, 5, or 7.
- scale – Optional scale factor for the computed derivative values. By default, no scaling is applied. SeegetDerivKernels() for details.
- delta – Optional delta value that is added to the results prior to storing them in dst .
- borderType – Pixel extrapolation method. See borderInterpolate() for details.
一般情况下,都是使用ksize*ksize内核计算导数的。当ksize=1时,往往使用3x1或者1x3的内核。
程序实现:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat gray_x, gray_y;
Mat abs_gray_x, abs_gray_y, dst;
Mat srcImage = imread("D:\\3.jpg");
if (!srcImage.data)
return -1;
Mat srcGray;
cvtColor(srcImage, srcGray, CV_RGB2GRAY);
imshow("srcGray", srcGray);
Sobel(srcGray, gray_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
convertScaleAbs(gray_x, abs_gray_x);
im