资源描述:
《几种常见的阈值分割算法核心代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、阈值分割1/*===============================图像分割=====================================*/2/*---------------------------------------------------------------------------*/3/*手动设置阀值*/4IplImage*binaryImg=cvCreateImage(cvSize(w,h),IPL_DEPTH_8U,1);5cvThreshold(smoothImgGauss,binaryImg,71,255,
2、CV_THRESH_BINARY);6cvNamedWindow("cvThreshold",CV_WINDOW_AUTOSIZE);7cvShowImage("cvThreshold",binaryImg);8//cvReleaseImage(&binaryImg);9 /*---------------------------------------------------------------------------*/10/*自适应阀值//计算像域邻域的平均灰度,来决定二值化的值*/11IplImage*adThresImg=cvCreate
3、Image(cvSize(w,h),IPL_DEPTH_8U,1);12doublemax_value=255;13intadpative_method=CV_ADAPTIVE_THRESH_GAUSSIAN_C;//CV_ADAPTIVE_THRESH_MEAN_C14 intthreshold_type=CV_THRESH_BINARY;15intblock_size=3;//阈值的象素邻域大小16 intoffset=5;//窗口尺寸17 cvAdaptiveThreshold(smoothImgGauss,adThresImg,max_valu
4、e,adpative_method,threshold_type,block_size,offset);18cvNamedWindow("cvAdaptiveThreshold",CV_WINDOW_AUTOSIZE);19cvShowImage("cvAdaptiveThreshold",adThresImg);20cvReleaseImage(&adThresImg);21/*---------------------------------------------------------------------------*/22/*最大熵阀值分
5、割法*/23IplImage*imgMaxEntropy=cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);24MaxEntropy(smoothImgGauss,imgMaxEntropy);25cvNamedWindow("MaxEntroyThreshold",CV_WINDOW_AUTOSIZE);26cvShowImage("MaxEntroyThreshold",imgMaxEntropy);//显示图像27 cvReleaseImage(&imgMaxEntropy);28/*-------
6、--------------------------------------------------------------------*/29/*基本全局阀值法*/30IplImage*imgBasicGlobalThreshold=cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);31cvCopyImage(srcImgGrey,imgBasicGlobalThreshold);32intpg[256],i,thre;33for(i=0;i<256;i++)pg[i]=0;34for(i=0;i7、gBasicGlobalThreshold->imageSize;i++)//直方图统计35 pg[(BYTE)imgBasicGlobalThreshold->imageData[i]]++;36thre=BasicGlobalThreshold(pg,0,256);//确定阈值37 cout<<"TheThresholdofthisImageinBasicGlobalThresholdis:"<8、hold,thre,255,CV_THRESH_BINARY);//二值化39 cvNamed