Sobel算子
#include <iostream>
#include <string>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
int main(int,char**)
{
std::string imgDir="/home/wang/imagesAndVideos/lena.jpeg";
cv::Mat img=cv::imread(imgDir);
if(!img.data){
printf("img can't load!!!!\n");
return -1;
}
cv::resize(img,img,cv::Size(320,320),cv::INTER_LINEAR);
cv::imshow("raw img",img);
int ddepth=CV_8UC1;//CV_8U,CV_32F
cv::Mat gradX;
cv::Mat gradY;
cv::Mat gradXY;
cv::cvtColor(img,img,cv::COLOR_BGR2GRAY);
//求解X方向梯度
cv::Sobel(img,gradX,ddepth,1,0,3,1,0,cv::BORDER_DEFAULT);
cv::imshow("gradX",gradX);
//求解Y方向梯度
cv::Sobel(img,gradY,ddepth,0,1,3,1,0,cv::BORDER_DEFAULT);
cv::imshow("gradY",gradY);
//合并梯度
cv::addWeighted(gradX,0.5,gradY,0.5,0,gradXY);
cv::imshow("gradXY",gradXY);
cv::waitKey(0);
return 0;
}
代码运行结果: