处理过程:
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
int main()
{Mat src = imread("E:\\vs2015\\opencvstudy\\1.jpg", 1);if (src.empty()){cout << "could not load the src image!" << endl;return -1;}char *input_title = "input Image";imshow(input_title, src);Mat blur_image;GaussianBlur(src, blur_image, Size(3, 3), 0, 0);Mat gray_image;cvtColor(blur_image, gray_image,CV_BGR2GRAY);imshow("gray_image", gray_image);Mat xgrad_sobel, ygrad_sobel;Sobel(gray_image, xgrad_sobel, CV_16S, 1, 0, 3); //x方向梯度Sobel(gray_image, ygrad_sobel, CV_16S, 0, 1, 3); //y方向梯度convertScaleAbs(xgrad_sobel, xgrad_sobel);convertScaleAbs(ygrad_sobel, ygrad_sobel);imshow("xgrad_sobel", xgrad_sobel);imshow("ygrad_sobel", ygrad_sobel);Mat xygrad_sobel;addWeighted(xgrad_sobel, 0.5, ygrad_sobel, 0.5, 0, xygrad_sobel);imshow("xygrad_sobel", xygrad_sobel);Mat xygrad_sobel2 = Mat(xgrad_sobel.size(), xgrad_sobel.type());int width = xgrad_sobel.cols;int height = xgrad_sobel.rows;for (int row = 0; row < height; row++){for (int col = 0; col < width; col++){int xg = xgrad_sobel.at<uchar>(row, col);int yg = ygrad_sobel.at<uchar>(row, col);int xy = xg + yg;xygrad_sobel2.at<uchar>(row, col) = saturate_cast<uchar>(xy);}}imshow("xygrad_sobel2", xygrad_sobel2);//Scharr算子//Mat x2grad_sobel, y2grad_sobel;//Scharr(gray_image, x2grad_sobel, CV_16S, 1, 0); //x方向梯度//Scharr(gray_image, y2grad_sobel, CV_16S, 0, 1); //y方向梯度//convertScaleAbs(x2grad_sobel, x2grad_sobel);//convertScaleAbs(y2grad_sobel, y2grad_sobel);//imshow("x2grad_sobel", x2grad_sobel);//imshow("y2grad_sobel", y2grad_sobel);//Mat xy2grad_sobel;//addWeighted(x2grad_sobel, 0.5, y2grad_sobel, 0.5, 0, xy2grad_sobel);//imshow("xy2grad_sobel", xy2grad_sobel);waitKey(0);return 0;}