本章内容:
1. LOG算子-墨西哥草帽算子
2. canny算子
1. LOG算子-墨西哥草帽算子
输出结果
2. canny算子
输出结果
源代码
#include <ostream>
#include <opencv.hpp>int main(int argc, char *argv[])
{
/*
本章内容:
1. LOG算子-墨西哥草帽算子
2. canny算子
*/
cv::String fileName = "/home/wang/dev/Image/cdut.jpeg";
cv::Mat src = cv::imread(fileName);
if(src.data == NULL){
printf("图像读入失败\n");
return -1;
}
/* 1. LOG算子-墨西哥草帽算子
计算过程:
1.图像灰度化
2.高斯模糊
3.拉普拉斯算子
*/
cv::Mat gray;
cv::Mat dstGs;
cv::Mat dstLOG;
cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
cv::GaussianBlur(gray,dstGs,cv::Size(5,5),5.0);
cv::Laplacian(dstGs,dstLOG,-1, 5);
cv::imshow("gray",gray);
cv::imshow("Gaussian blur",dstGs);
cv::imshow("LOG", dstLOG);/*2. canny算子
计算过程:
1.图像灰度化
2.高斯模糊
3.分别计算x,y方向梯度
4. 设定上下阈值,候选区的值若链接上阈值,则认为是边缘。
*/
cv::Mat dstCanny;
cv::Mat dstCanny1;
cv::Mat dstX;
cv::Mat dstY;
/*
CV_EXPORTS_W void Sobel( InputArray src, OutputArray dst, int ddepth,
int dx, int dy, int ksize = 3,
double scale = 1, double delta = 0,
int borderType = BORDER_DEFAULT );
*/cv::Sobel(dstGs,dstX,CV_16SC1,1,0); // 计算x方向sobel梯度, 梯度图像,输出包含符号
cv::Sobel(dstGs,dstY,CV_16SC1,0,1); // 计算y方向 sobel梯度, 梯度图像,输出包含符号
/*
api接口: CV_EXPORTS_W void Canny( InputArray image, OutputArray edges,
double threshold1, double threshold2,
int apertureSize = 3, bool L2gradient = false );CV_EXPORTS_W void Canny( InputArray dx, InputArray dy,
OutputArray edges,
double threshold1, double threshold2,
bool L2gradient = false );参数分析:
@param ddepth output image depth, see @ref filter_depths "combinations"; in the case of
8-bit input images it will result in truncated derivatives.
{
Unsigned 8bits uchar 0~255
Mat: CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4Signed 8bits char -128~127
Mat: CV_8SC1,CV_8SC2,CV_8SC3,CV_8SC4Unsigned 16bits ushort 0~65535
Mat: CV_16UC1,CV_16UC2,CV_16UC3,CV_16UC4Signed 16bits short -32768~32767
Mat: CV_16SC1,CV_16SC2,CV_16SC3,CV_16SC4Signed 32bits int -2147483648~2147483647
Mat: CV_32SC1,CV_32SC2,CV_32SC3,CV_32SC4Float 32bits float -1.18*10-38~3.40*10-38
Mat: CV_32FC1,CV_32FC2,CV_32FC3,CV_32FC4Double 64bits double
Mat: CV_64FC1,CV_64FC2,CV_64FC3,CV_64FC4
}
*/
cv::Canny(dstX,dstY,dstCanny,50,150);
cv::Canny(dstGs, dstCanny1,50,150);
cv::imshow("my Canny", dstCanny);
cv::imshow("sys Canny", dstCanny1);
cv::waitKey(0);
return 1;
}