


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年扶余市博物馆公开选调解说员(4人)考前自测高频考点模拟试题及答案详解(名师系列)
- 2025年上半年甘肃陇南文县教师资证认定模拟试卷及答案详解(必刷)
- 2025年乐山事业单位真题
- “百万英才汇南粤”2025年佛山市高明区公开招聘中小学教师(第四场)模拟试卷附答案详解(典型题)
- 2025年甘肃庆阳华池县事业单位选调工作人员考前自测高频考点模拟试题及1套完整答案详解
- 2025广东广州医科大学附属医院第一次招聘163人考前自测高频考点模拟试题含答案详解
- 2025年淡水养殖产品种苗项目合作计划书
- 2025年高邮市市级机关公开遴选考试真题
- 2025年福建省厦门市体育局所属事业单位厦门市体育运动学校公开招聘4人考前自测高频考点模拟试题附答案详解
- 2025贵州省第二人民医院第十三届贵州人才博览会引才招聘13人模拟试卷及答案详解(有一套)
- 乒乓球比赛裁判员学习用(教学)
- 非口服药物的吸收(生物药剂学与药物动力学课件)
- 《中外美术史》课件
- 七年级语文上册课外阅读之《朝花夕拾》读书分享课件
- 节目组劳务合同模板
- 锅炉培训课件
- 青岛 二年级 数学 上册 第4单元《8的乘法口诀》教学课件
- 广东省东莞市五校2024-2025学年高一上学期第一次联考数学试题(无答案)
- 中华人民共和国标准设计施工总承包招标文件(2012年版)
- PVC-地面中水泥基自流平找平层的施工作业指导书
- 道路施工分包合同范例
评论
0/150
提交评论