梯度算子
• 梯度通过一个二维列向量来定义
• 向量的模值
实践中通常称为梯度图像。
• 考虑一个3x3的图像区域,z代表 灰度级,上式在点z5的f值可用 数字方式近似。
✓Roberts交叉梯度算子
✓Prewitt梯度算子
✓Prewitt梯度算子
Roberts交叉梯度算子
✓梯度计算由两个模板组成,第一个求 得梯度的第一项,第二个求得梯度的 第二项,然后求和,得到梯度。
✓两个模板称为Roberts 交叉梯度算子
• Prewitt梯度算子——3x3的梯度模 板
• Sobel梯度算子——3x3的梯度模板
代码实现:
/**
* @file Sobel_Demo.cpp
* @brief Sample code uses Sobel or Scharr OpenCV functions for edge detection
* @author OpenCV team
*/
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv,
"{@input |../data/lena.jpg|input image}"
"{ksize k|1|ksize (hit 'K' to increase its value)}"
"{scale s|1|scale (hit 'S' to increase its value)}"
"{delta d|0|delta (hit 'D' to increase its value)}"
"{help h|false|show help message}");
cout << "The sample uses Sobel or Scharr OpenCV functions for edge detection\n\n";
parser.printMessage();
cout << "\nPress 'ESC' to exit program.\nPress 'R' to reset values ( ksize will be -1 equal to Scharr function )";
Mat image,src, src_gray;
Mat grad;
const String window_name = "Sobel Demo - Simple Edge Detector";
int ksize = parser.get
int scale = parser.get
int delta = parser.get
int ddepth = CV_16S;
String imageName = parser.get
image = imread( imageName, IMREAD_COLOR );
if( image.empty() ){
printf("Error opening image: %s\n", imageName.c_str());
return 1;
}
for (;;){
GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
cvtColor(src, src_gray, COLOR_BGR2GRAY);
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
//Generate Gradient X
Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
//Generate Gradient Y
Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);
convertScaleAbs(grad_y, abs_grad_y);
// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
imshow(window_name, grad);
char key = (char)waitKey(0);
if(key == 27){
return 0;
}
if (key == 'k' || key == 'K'){
ksize = ksize < 30 ? ksize+2 : -1;
}
if (key == 's' || key == 'S'){
scale++;
}
if (key == 'd' || key == 'D'){
delta++;
}
if (key == 'r' || key == 'R'){
scale = 1;
ksize = -1;
delta = 0;
}
}
return 0;
}
结果:
按下r键后
教材例子:
原图、sobel图