#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespace cv; int main(int argc, char** argv) { Mat src, dst; int ksize = 0; src = imread("L:/5.jpg"); if (!src.data) { printf("could not load image...\n"); return -1; } char INPUT_WIN[] = "input image"; char OUTPUT_WIN[] = "sobel-demo"; namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE); namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE); imshow(INPUT_WIN, src); GaussianBlur(src, dst, Size(3, 3), 0, 0); //高斯平滑,高斯滤波 Mat gray_src; cvtColor(dst, gray_src,CV_BGR2GRAY); imshow("gray image", gray_src); Mat xgrad, ygrad; Sobel(gray_src, xgrad, CV_16S, 1, 0, 3); //X轴方向梯度 Sobel(gray_src, ygrad, CV_16S, 0, 1, 3); //Y轴方向梯度 convertScaleAbs(xgrad, xgrad); //X梯度取绝对值 convertScaleAbs(ygrad, ygrad); //Y梯度取绝对值 imshow("xgrad", xgrad); imshow("ygrad", ygrad); Mat xygrad; addWeighted(xgrad, 0.5, ygrad, 0.5, 0, xygrad); //X、Y梯度各占权重0.5 imshow("Final Result",xygrad); waitKey(0); return 0; }
Y轴方向梯度:
来源:博客园
作者:跟上帝和宇宙玩掷骰子
链接:https://www.cnblogs.com/Jack-Elvis/p/11444617.html