




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
阈值分割1 /*=图像分割=*/2 /*-*/3 /*手动设置阀值*/4 IplImage* binaryImg = cvCreateImage(cvSize(w, h),IPL_DEPTH_8U, 1);5 cvThreshold(smoothImgGauss,binaryImg,71,255,CV_THRESH_BINARY); 6 cvNamedWindow(cvThreshold, CV_WINDOW_AUTOSIZE );7 cvShowImage( cvThreshold, binaryImg );8 /cvReleaseImage(&binaryImg); 9 /*-*/10 /*自适应阀值 /计算像域邻域的平均灰度,来决定二值化的值*/11 IplImage* adThresImg = cvCreateImage(cvSize(w, h),IPL_DEPTH_8U, 1);12 double max_value=255;13 int adpative_method=CV_ADAPTIVE_THRESH_GAUSSIAN_C;/CV_ADAPTIVE_THRESH_MEAN_C14 int threshold_type=CV_THRESH_BINARY;15 int block_size=3;/阈值的象素邻域大小16 int offset=5;/窗口尺寸17 cvAdaptiveThreshold(smoothImgGauss,adThresImg,max_value,adpative_method,threshold_type,block_size,offset);18 cvNamedWindow(cvAdaptiveThreshold, CV_WINDOW_AUTOSIZE );19 cvShowImage( cvAdaptiveThreshold, adThresImg );20 cvReleaseImage(&adThresImg);21 /*-*/22 /*最大熵阀值分割法*/ 23 IplImage* imgMaxEntropy = cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);24 MaxEntropy(smoothImgGauss,imgMaxEntropy);25 cvNamedWindow(MaxEntroyThreshold, CV_WINDOW_AUTOSIZE );26 cvShowImage( MaxEntroyThreshold, imgMaxEntropy );/显示图像27 cvReleaseImage(&imgMaxEntropy ); 28 /*-*/29 /*基本全局阀值法*/30 IplImage* imgBasicGlobalThreshold = cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);31 cvCopyImage(srcImgGrey,imgBasicGlobalThreshold);32 int pg256,i,thre; 33 for (i=0;i256;i+) pgi=0;34 for (i=0;iimageSize;i+) / 直方图统计35 pg(BYTE)imgBasicGlobalThreshold-imageDatai+; 36 thre = BasicGlobalThreshold(pg,0,256); / 确定阈值37 coutThe Threshold of this Image in BasicGlobalThreshold is:threendl;/输出显示阀值38 cvThreshold(imgBasicGlobalThreshold,imgBasicGlobalThreshold,thre,255,CV_THRESH_BINARY); / 二值化 39 cvNamedWindow(BasicGlobalThreshold, CV_WINDOW_AUTOSIZE );40 cvShowImage( BasicGlobalThreshold, imgBasicGlobalThreshold);/显示图像41 cvReleaseImage(&imgBasicGlobalThreshold);42 /*-*/43 /*OTSU*/44 IplImage* imgOtsu = cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);45 cvCopyImage(srcImgGrey,imgOtsu);46 int thre2;47 thre2 = otsu2(imgOtsu);48 coutThe Threshold of this Image in Otsu is:thre2endl;/输出显示阀值49 cvThreshold(imgOtsu,imgOtsu,thre2,255,CV_THRESH_BINARY); / 二值化 50 cvNamedWindow(imgOtsu, CV_WINDOW_AUTOSIZE );51 cvShowImage( imgOtsu, imgOtsu);/显示图像 52 cvReleaseImage(&imgOtsu);53 /*-*/54 /*上下阀值法:利用正态分布求可信区间*/55 IplImage* imgTopDown = cvCreateImage( cvGetSize(imgGrey), IPL_DEPTH_8U, 1 );56 cvCopyImage(srcImgGrey,imgTopDown);57 CvScalar mean ,std_dev;/平均值、 标准差58 double u_threshold,d_threshold;59 cvAvgSdv(imgTopDown,&mean,&std_dev,NULL); 60 u_threshold = mean.val0 +2.5* std_dev.val0;/上阀值61 d_threshold = mean.val0 -2.5* std_dev.val0;/下阀值62 /u_threshold = mean + 2.5 * std_dev; /错误63 /d_threshold = mean - 2.5 * std_dev;64 coutThe TopThreshold of this Image in TopDown is:d_thresholdendl;/输出显示阀值65 coutThe DownThreshold of this Image in TopDown is:u_thresholdendl;66 cvThreshold(imgTopDown,imgTopDown,d_threshold,u_threshold,CV_THRESH_BINARY_INV);/上下阀值67 cvNamedWindow(imgTopDown, CV_WINDOW_AUTOSIZE );68 cvShowImage( imgTopDown, imgTopDown);/显示图像 69 cvReleaseImage(&imgTopDown);70 /*-*/71 /*迭代法*/72 IplImage* imgIteration = cvCreateImage( cvGetSize(imgGrey), IPL_DEPTH_8U, 1 );73 cvCopyImage(srcImgGrey,imgIteration);74 int thre3,nDiffRec;75 thre3 =DetectThreshold(imgIteration, 100, nDiffRec);76 coutThe Threshold of this Image in imgIteration is:thre3height;9 int width = img-width;10 int step = img-widthStep/sizeof(uchar);11 uchar *data = (uchar*)img-imageData;12 13 iDiffRec =0;14 int F256= 0 ; /直方图数组15 int iTotalGray=0;/灰度值和16 int iTotalPixel =0;/像素数和17 byte bt;/某点的像素值18 19 uchar iThrehold,iNewThrehold;/阀值、新阀值20 uchar iMaxGrayValue=0,iMinGrayValue=255;/原图像中的最大灰度值和最小灰度值21 uchar iMeanGrayValue1,iMeanGrayValue2;22 23 /获取(i,j)的值,存于直方图数组F24 for(int i=0;iwidth;i+)25 26 for(int j=0;jheight;j+)27 28 bt = datai*step+j;29 if(btiMaxGrayValue)32 iMaxGrayValue = bt;33 Fbt+;34 35 36 37 iThrehold =0;/38 iNewThrehold = (iMinGrayValue+iMaxGrayValue)/2;/初始阀值39 iDiffRec = iMaxGrayValue - iMinGrayValue;40 41 for(int a=0;(abs(iThrehold-iNewThrehold)0.5)&anMaxIter;a+)/迭代中止条件42 43 iThrehold = iNewThrehold;44 /小于当前阀值部分的平均灰度值45 for(int i=iMinGrayValue;iiThrehold;i+)46 47 iTotalGray += Fi*i;/F存储图像信息48 iTotalPixel += Fi;49 50 iMeanGrayValue1 = (uchar)(iTotalGray/iTotalPixel);51 /大于当前阀值部分的平均灰度值52 iTotalPixel =0;53 iTotalGray =0;54 for(int j=iThrehold+1;jiMaxGrayValue;j+)55 56 iTotalGray += Fj*j;/F存储图像信息57 iTotalPixel += Fj; 58 59 iMeanGrayValue2 = (uchar)(iTotalGray/iTotalPixel);60 61 iNewThrehold = (iMeanGrayValue2+iMeanGrayValue1)/2; /新阀值62 iDiffRec = abs(iMeanGrayValue2 - iMeanGrayValue1);63 64 65 /coutThe Threshold of this Image in imgIteration is:iThreholdendl;66 return iThrehold;67 68Otsu代码一1 /*=*/2 /* OTSU global thresholding routine */3 /* takes a 2D unsigned char array pointer, number of rows, and */4 /* number of cols in the array. returns the value of the threshold */5 /*parameter: 6 *image - buffer for image7 rows, cols - size of image8 x0, y0, dx, dy - region of vector used for computing threshold9 vvv - debug option, is 0, no debug information outputed10 */11 /*12 OTSU 算法可以说是自适应计算单阈值(用来转换灰度图像为二值图像)的简单高效方法。13 下面的代码最早由 Ryan Dibble提供,此后经过多人Joerg.Schulenburg, R.Z.Liu 等修改,补正。14 算法对输入的灰度图像的直方图进行分析,将直方图分成两个部分,使得两部分之间的距离最大。15 划分点就是求得的阈值。16 */17 /*=*/18 int otsu (unsigned char*image, int rows, int cols, int x0, int y0, int dx, int dy, int vvv)19 20 21 unsigned char*np; / 图像指针22 int thresholdValue=1; / 阈值23 int ihist256; / 图像直方图,256个点24 25 int i, j, k; / various counters26 int n, n1, n2, gmin, gmax;27 double m1, m2, sum, csum, fmax, sb;28 29 / 对直方图置零30 memset(ihist, 0, sizeof(ihist);31 32 gmin=255; gmax=0;33 / 生成直方图34 for (i = y0 +1; i y0 + dy -1; i+) 35 36 np = (unsigned char*)imagei*cols+x0+1;37 for (j = x0 +1; j gmax) gmax=*np;41 if(*np gmin) gmin=*np;42 np+; /* next pixel */43 44 45 46 / set up everything47 sum = csum =0.0;48 n =0;49 50 for (k =0; k =255; k+) 51 52 sum += (double) k * (double) ihistk; /* x*f(x) 质量矩*/53 n += ihistk; /* f(x) 质量 */54 55 56 if (!n) 57 58 / if n has no value, there is problems.59 fprintf (stderr, NOT NORMAL thresholdValue = 160n);60 return (160);61 62 63 / do the otsu global thresholding method64 fmax =-1.0;65 n1 =0;66 for (k =0; k fmax) 84 85 fmax = sb;86 thresholdValue = k;87 88 89 90 / at this point we have our thresholding value91 92 / debug code to display thresholding values93 if ( vvv &1 )94 fprintf(stderr,# OTSU: thresholdValue = %d gmin=%d gmax=%dn,95 thresholdValue, gmin, gmax);96 97 return(thresholdValue);98 Otsu代码二1 /*=*/2 /* OTSU global thresholding routine */3 /*=*/4 int otsu2 (IplImage *image)5 6 int w = image-width;7 int h = image-height;8 9 unsigned char*np; / 图像指针10 unsigned char pixel;11 int thresholdValue=1; / 阈值12 int ihist256; / 图像直方图,256个点13 14 int i, j, k; / various counters15 int n, n1, n2, gmin, gmax;16 double m1, m2, sum, csum, fmax, sb;17 18 / 对直方图置零.19 memset(ihist, 0, sizeof(ihist);20 21 gmin=255; gmax=0;22 / 生成直方图23 for (i =0; i imageData + image-widthStep*i);26 for (j =0; j gmax) gmax= pixel;31 if(pixel gmin) gmin= pixel;32 33 34 35 / set up everything36 sum = csum =0.0;37 n =0;38 39 for (k =0; k =255; k+) 40 41 sum += k * ihistk; /* x*f(x) 质量矩*/42 n += ihistk; /* f(x) 质量 */43 44 45 if (!n) 46 47 / if n has no value, there is problems.48 /fprintf (stderr, NOT NORMAL thresholdValue = 160n);49 thresholdValue =160;50 goto L;51 52 53 / do the otsu global thresholding method54 fmax =-1.0;55 n1 =0;56 for (k =0; k fmax)68 69 fmax = sb;70 thresholdValue = k;71 72 73 74 L:75 for (i =0; i imageData + image-widthStep*i);78 for (j =0; j = thresholdValue)81 npj =255;82 else npj =0;83 84 85 86 /coutThe Threshold of this Image in Otsu is:thresholdValueendl;87 return(thresholdValue);88 最大熵阀值1 /*=2 = 代码内容:最大熵阈值分割 3 = 修改日期:2009-3-3 4 = 作者:crond123 5 = 博客:/crond123/6 = E_Mail: 7 =*/8 / 计算当前位置的能量熵9 double caculateCurrentEntropy(CvHistogram * Histogram1,int cur_threshold,entropy_state state)10 11 int start,end;12 int total =0;13 double cur_entropy =0.0;14 if(state = back) 15 16 start =0;17 end = cur_threshold; 18 19 else 20 21 start = cur_threshold;22 end =256; 23 24 for(int i=start;iend;i+) 25 26 total += (int)cvQueryHistValue_1D(Histogram1,i);/查询直方块的值 P30427 28 for(int j=start;jdepth =8& dst-depth =8);47 assert(src-nChannels =1);48 CvHistogram * hist = cvCreateHist(1,&HistogramBins,CV_HIST_ARRAY,HistogramRange);/创建一个指定尺寸的直方图49 /参数含义:直方图包含的维数、直方图维数尺寸的数组、直方图的表示格式、方块范围数组、归一化标志50 cvCalcHist(&src,hist);/计算直方图51 d
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业安全理论培训课件
- 2025年高级导游综合知识考试冲刺模拟试题及答案
- 渠道管理(第二版)项目八 渠道冲突与管理制(教案)
- 出租公司安全培训材料课件
- 2025汽车交易定金合同
- 2025标准房屋租赁合同样本示例
- 村委会代办员考试试题及答案
- 2025关于合同工程师的劳动合同解除问题
- 脑科学品牌策略-洞察及研究
- 跨界协同机制创新-洞察及研究
- 铁路法律知识课件
- 2025年《审计相关基础知识(中级)》考前几页纸
- 陶板幕墙施工方案
- 2025年中国汉字听写大会汉字听写知识竞赛题库及答案(共六套)
- 《离婚经济补偿制度研究》13000字【论文】
- 《国内外绩效考核指标体系研究现状文献综述》4200字
- 天津第一中学2025-2025学年高三下学期3月月考英语试卷(含答案)
- 农场生态农业循环产业园项目方案书
- 第二章第二节女性生殖系统生理课件
- 小学生红色经典故事100个红色经典故事【6篇】
- 沪教版(五四学制)(2024)六年级下册单词表+默写单
评论
0/150
提交评论