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

下载本文档

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

文档简介

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

2、阈值,继而二值化/ implementation of otsu algorithm/ author: onezeros()/ reference: rafael c. gonzalez. digital image processing using matlab void cvthresholdotsu(iplimage* src, iplimage* dst)int height=src->height; int width=src->width;/histogramfloat histogram256= 0; for(int i=0; i<height; i+)unsi

3、gned char* p=(unsigned char*)src->imagedata+src->widthstep*i; for(int j=0; j<width; j+)histogram*p+;/normalize histogram int size=height*width; for(int i=0; i<256; i+)histogrami=histogrami/size;/average pixel value float avgvalue=0;for(int i=0; i<256; i+)avgvalue+=i*histogrami;int thr

4、eshold;float maxvariance=0; float w=0,u=0;for(int i=0; i<256; i+)w+=histogrami; u+=i*histogrami;float t=avgvalue*w-u;float variance=t*t/(w*(1-w); if(variance>maxvariance)maxvariance=variance; threshold=i;cvthreshold(src,dst,threshold,255,cv_thresh_binary);更多状况下我们并不需要对每一帧都是用 otsu 查找阈值,于是可以先找到阈值

5、, 然后用找到的阈值处理后面的图像。下面这个函数重载了上面的,返回值就是阈值。只做了一点转变/ implementation of otsu algorithm/ author: onezeros()/ reference: rafael c. gonzalez. digital image processing using matlab int cvthresholdotsu(iplimage* src)int height=src->height; int width=src->width;/histogramfloat histogram256= 0; for(int i=0

6、; i<height; i+)unsigned char* p=(unsigned char*)src->imagedata+src->widthstep*i; for(int j=0; j<width; j+)histogram*p+;/normalize histogram int size=height*width; for(int i=0; i<256; i+)histogrami=histogrami/size;/average pixel value float avgvalue=0; for(int i=0; i<256; i+)avgvalu

7、e+=i*histogrami;int threshold;float maxvariance=0; float w=0,u=0;for(int i=0; i<256; i+)w+=histogrami; u+=i*histogrami;float t=avgvalue*w-u;float variance=t*t/(w*(1-w); if(variance>maxvariance)maxvariance=variance;threshold=i;return threshold;我在手的自动检测中使用这个方法,效果很好。下面是使用上述两个函数的简洁的主程序,可以试运行一下,假如处

8、理视频,要保证第一帧时,手要在图像中。#include <cv.h> #include <cxcore.h> #include <highgui.h>#pragma comment(lib,“cv210d.lib“) #pragma comment(lib,“cxcore210d.lib“) #pragma comment(lib,“highgui210d.lib“)#include <iostream> using namespace std;int main(int argc, char* argv)#ifdef video /video p

9、rocesscvcapture* capture=cvcreatecameracapture(-1); if (!capture)cout<<“failed to open camera“<<endl; exit(0);int threshold=-1;iplimage* img;while (img=cvqueryframe(capture)cvshowimage(“video“,img);cvcvtcolor(img,img,cv_rgb2ycrcb);iplimage* imgcb=cvcreateimage(cvgetsize(img),8,1); cvspli

10、t(img,null,null,imgcb,null);if (threshold<0)threshold=cvthresholdotsu(imgcb);/cvthresholdotsu(imgcb,imgcb); cvthreshold(imgcb,imgcb,threshold,255,cv_thresh_binary); cverode(imgcb,imgcb);cvdilate(imgcb,imgcb);cvshowimage(“object“,imgcb); cvreleaseimage(&imgcb);if (cvwaitkey(3)=27) /escbreak;cv

11、releasecapture(&capture);#else /single image processconst char* filename=(argc>=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); cvsaveimage(buf,img);cverod

12、e(img,img); cvdilate(img,img); cvshowimage( “dst“, img );sprintf_s(buf,256,“%cessed.jpg“,filename); cvsaveimage(buf,img);cvwaitkey(0); #endifreturn 0;#include <cv.h> #include <cxcore.h> #include <highgui.h>#pragma comment(lib,“cv210d.lib“) #pragma comment(lib,“cxcore210d.

13、lib“) #pragma comment(lib,“highgui210d.lib“) #include <iostream>using namespace std;int main(int argc, char* argv)#ifdef video /video processcvcapture* capture=cvcreatecameracapture(-1); if (!capture)cout<<“failed to open camera“<<endl; exit(0);int threshold=-1;iplimage* img;while

14、(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<0)threshold=cvthresholdotsu(imgcb);/cvthresholdotsu(imgcb,imgcb); cvthreshold(imgcb,imgcb,threshold,255,cv_thresh

15、_binary); cverode(imgcb,imgcb);cvdilate(imgcb,imgcb);cvshowimage(“object“,imgcb); cvreleaseimage(&imgcb);if (cvwaitkey(3)=27) /escbreak;cvreleasecapture(&capture);#else /single image processconst char* filename=(argc>=2?argv1:“cr.jpg“);iplimage* img=cvloadimage(filename,cv_load_image_grayscale); cvthresholdotsu(img,img);cvshowimage( “src“, img ); ch

温馨提示

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

最新文档

评论

0/150

提交评论