Opencv2 computer vision application programming cookbook<五>

第六章介绍图像滤波

主要介绍图像的laplace及利用laplace求图像边缘

#include "stdafx.h"
#include<iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;


class LaplacianZC
{
private:
//原图
Mat img;
//包含 Laplacian的32位浮点图像
Mat laplace;
//laplacian总积核的大小
int aperture;
public:
LaplacianZC():aperture(3){};
//设置卷积核的大小
void SetAperture(int a)
{
aperture=a;
}
//计算浮点数Laplacian
Mat ComputeLaplacian(const Mat & image)
{
//计算Laplacian
Laplacian(image,laplace,CV_32F,aperture);
//保留图像的局部备份(用于零点交叉)
img=image.clone();
return laplace;
}
//返回8位图像存储的laplacian结果
//零点交叉于灰度值128
//如果没有指定scale参数,那么最大值将缩放至强度255
//你必须在调用它之前调用computeLaplacian
Mat GetLaplaceianImage(double scale=-1.0)
{
if(scale<0)
{
double lapmin,lapmax;
minMaxLoc(laplace,&lapmin,&lapmax);
scale=127/max(-lapmin,lapmax);
}
Mat laplaceImage;
laplace.convertTo(laplaceImage,CV_8U,scale,128);
return laplaceImage;
}
//得到零点交叉的二值图像
//如果相邻像素的乘积小于threshold
//那么零点交叉将被忽略
Mat GetZeroCrossings(float threshold=1.0)
{
//创建迭代器
Mat_<float>::const_iterator it=laplace.begin<float>()+laplace.step1();
Mat_<float>::const_iterator itend=laplace.end<float>();
Mat_<float>::const_iterator itup=laplace.begin<float>();
//初始化为白色的二值图像
Mat binary(laplace.size(),CV_8U,Scalar(255));
Mat_<uchar>::iterator itout=binary.begin<uchar>()+binary.step1();
//对输入阈值取反
threshold*=-1.0;
for(;it!=itend;++it,++itup,++itout)
{
//如果相邻像素的乘积为负数,那么符号发生改变
if(*it**(it-1)<threshold)
*itout=0;//水平方向零点交叉
else if(*it**itup<threshold)
*itout=0;//垂直方向零点交叉


}
return binary;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
Mat image=imread("lena512color.tiff",0);
Mat result;
//blur(image,result,Size(5,5));
GaussianBlur(image,result,Size(5,5),1.5);
Mat reducedImage;
pyrDown(image,reducedImage);
pyrUp(reducedImage,image);


Mat resizedImage;
resize(image,resizedImage,Size(image.cols/3,image.rows/3));


medianBlur(image,result,5);


Mat sobelX,sobelY;
Sobel(image,sobelX,CV_8U,1,0,3,0.4,128);
Sobel(image,sobelY,CV_8U,0,1,3,0.4,128);


//计算sobel范式
Sobel(image,sobelX,CV_16S,1,0);
Sobel(image,sobelY,CV_16S,0,1);
Mat sobel;
sobel=abs(sobelX)+abs(sobelY);


//搜索 Sobel极大值
double sobmin,sobmax;
minMaxLoc(sobel,&sobmin,&sobmax);
//变换为8位图像
//sobelImage=-alpha*sobel+255
Mat sobelImage;
sobel.convertTo(sobelImage,CV_8U,-255./sobmax,255);

threshold(sobelImage,sobelImage,200,255,THRESH_BINARY);


//图像的laplacian
LaplacianZC laplacian;
laplacian.SetAperture(7);
Mat flap=laplacian.ComputeLaplacian(image);
Mat laplace;
laplace=laplacian.GetLaplaceianImage();
//检查边缘
//laplace=laplacian.GetZeroCrossings();

imshow("lena",laplace);




waitKey();
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值