opencv实现c++的otsu自适应阈值分割的算法描述_第1页
opencv实现c++的otsu自适应阈值分割的算法描述_第2页
opencv实现c++的otsu自适应阈值分割的算法描述_第3页
opencv实现c++的otsu自适应阈值分割的算法描述_第4页
opencv实现c++的otsu自适应阈值分割的算法描述_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、otsu算法选择使类间方差最大的灰度值为阈值,具有很好的效果算法具体描述见otsu论文,或冈萨雷斯著名的数字图像处理那本书这里给出程序流程:1、计算直方图并归一化histogram2、计算图像灰度均值avgValue.3、计算直方图的零阶wi和一级矩ui4、计算并找到最大的类间方差(between-class variance)variancei=(avgValue*wi-ui)*(avgValue*wi-ui)/(wi*(1-wi)对应此最大方差的灰度值即为要找的阈值5、用找到的阈值二值化图像我在代码中做了一些优化,所以算法描述的某些地方跟程序并不一致otsu代码,先找阈值,继而二值化/ i

2、mplementation of otsu algorithm/ author: onezeros()/ reference: Rafael C. Gonzalez. Digital Image Processing Using MATLABvoid cvThresholdOtsu(IplImage* src, IplImage* dst) int height=src-height; int width=src-width; /histogram float histogram256= 0; for(int i=0; iimageData+src-widthStep*i; f

3、or(int j=0; jwidth; j+) histogram*p+; /normalize histogram int size=height*width; for(int i=0; i256; i+) histogrami=histogrami/size; /average pixel value float avgValue=0; for(int i=0; i256; i+) avgValue+=i*histogrami; int threshold; float maxVariance=0; float w=0,u=0; for(int i=0; imaxVariance) max

4、Variance=variance; threshold=i; cvThreshold(src,dst,threshold,255,CV_THRESH_BINARY);更多情况下我们并不需要对每一帧都是用otsu寻找阈值,于是可以先找到阈值,然后用找到的阈值处理后面的图像。下面这个函数重载了上面的,返回值就是阈值。只做了一点改变/ implementation of otsu algorithm/ author: onezeros()/ reference: Rafael C. Gonzalez. Digital Image Processing Using MATLABint

5、 cvThresholdOtsu(IplImage* src) int height=src-height; int width=src-width; /histogram float histogram256= 0; for(int i=0; iimageData+src-widthStep*i; for(int j=0; jwidth; j+) histogram*p+; /normalize histogram int size=height*width; for(int i=0; i256; i+) histogrami=histogrami/size; /average pixel

6、value float avgValue=0; for(int i=0; i256; i+) avgValue+=i*histogrami; int threshold; float maxVariance=0; float w=0,u=0; for(int i=0; imaxVariance) maxVariance=variance; threshold=i; return threshold;我在手的自动检测中使用这个方法,效果很好。下面是使用上述两个函数的简单的主程序,可以试运行一下,如果处理视频,要保证第一帧时,手要在图像中。#include #include #include #p

7、ragma comment(lib,cv210d.lib)#pragma comment(lib,cxcore210d.lib)#pragma comment(lib,highgui210d.lib)#include using namespace std;int main(int argc, char* argv)#ifdef VIDEO /video process CvCapture* capture=cvCreateCameraCapture(-1); if (!capture) coutfailed to open cameraendl; exit(0); int threshold

8、=-1; IplImage* img; while (img=cvQueryFrame(capture) cvShowImage(video,img); cvCvtColor(img,img,CV_RGB2YCrCb); IplImage* imgCb=cvCreateImage(cvGetSize(img),8,1); cvSplit(img,NULL,NULL,imgCb,NULL); if (threshold=2?argv1:cr.jpg); IplImage* img=cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE); cvThreshold

9、Otsu(img,img); cvShowImage( src, img ); char buf256; sprintf_s(buf,256,%s.otsu.jpg,filename); cvSaveImage(buf,img); cvErode(img,img); cvDilate(img,img); cvShowImage( dst, img ); sprintf_s(buf,256,%cessed.jpg,filename); cvSaveImage(buf,img); cvWaitKey(0);#endif return 0;#include #include #i

10、nclude #pragma comment(lib,cv210d.lib)#pragma comment(lib,cxcore210d.lib)#pragma comment(lib,highgui210d.lib)#include using namespace std;int main(int argc, char* argv)#ifdef VIDEO /video process CvCapture* capture=cvCreateCameraCapture(-1); if (!capture) coutfailed to open cameraendl; exit(0); int

11、threshold=-1; IplImage* img; while (img=cvQueryFrame(capture) cvShowImage(video,img); cvCvtColor(img,img,CV_RGB2YCrCb); IplImage* imgCb=cvCreateImage(cvGetSize(img),8,1); cvSplit(img,NULL,NULL,imgCb,NULL); if (threshold=2?argv1:cr.jpg); IplImage* img=cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE); cvThresholdOtsu(img,img); cvShowImage( src, img ); char buf256; sprintf_s(buf,256,%s.otsu.jpg,filename); cvSaveI

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论