当前位置:文档之家› 几种常见的阈值分割算法核心代码

几种常见的阈值分割算法核心代码

阈值分割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);12double max_value=255;13intadpative_method=CV_ADAPTIVE_THRESH_GAUSSIAN_C;//CV_ADAPTIVE_THRESH_MEAN_C14int threshold_type=CV_THRESH_BINARY;15int block_size=3;//阈值的象素邻域大小16int 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/*最大熵阀值分割法*/23IplImage* 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);32int pg[256],i,thre;33for (i=0;i<256;i++) pg[i]=0;34for (i=0;i<imgBasicGlobalThreshold->imageSize;i++) // 直方图统计35 pg[(BYTE)imgBasicGlobalThreshold->imageData[i]]++;36 thre = BasicGlobalThreshold(pg,0,256); // 确定阈值37 cout<<"The Threshold of this Image in BasicGlobalThresholdis:"<<thre<<endl;//输出显示阀值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);46int thre2;47 thre2 = otsu2(imgOtsu);48 cout<<"The Threshold of this Image in Otsu is:"<<thre2<<endl;//输出显示阀值49 cvThreshold(imgOtsu,imgOtsu,thre2,255,CV_THRESH_BINARY); // 二值化50 cvNamedWindow("imgOtsu", CV_WINDOW_AUTOSIZE );51 cvShowImage( "imgOtsu", imgOtsu);//显示图像52 cvReleaseImage(&imgOtsu);53/*---------------------------------------------------------------------------*/54/*上下阀值法:利用正态分布求可信区间*/55IplImage* imgTopDown = cvCreateImage( cvGetSize(imgGrey), IPL_DEPTH_8U, 1);56 cvCopyImage(srcImgGrey,imgTopDown);57 CvScalar mean ,std_dev;//平均值、标准差58double u_threshold,d_threshold;59 cvAvgSdv(imgTopDown,&mean,&std_dev,NULL);60 u_threshold = mean.val[0] +2.5* std_dev.val[0];//上阀值61 d_threshold = mean.val[0] -2.5* std_dev.val[0];//下阀值62//u_threshold = mean + 2.5 * std_dev; //错误63//d_threshold = mean - 2.5 * std_dev;64 cout<<"The TopThreshold of this Image in TopDown is:"<<d_threshold<<endl;//输出显示阀值65 cout<<"The DownThreshold of this Image in TopDown is:"<<u_threshold<<endl; 66cvThreshold(imgTopDown,imgTopDown,d_threshold,u_threshold,CV_THRESH_BINARY_I NV);//上下阀值67 cvNamedWindow("imgTopDown", CV_WINDOW_AUTOSIZE );68 cvShowImage( "imgTopDown", imgTopDown);//显示图像69 cvReleaseImage(&imgTopDown);70/*---------------------------------------------------------------------------*/71/*迭代法*/72IplImage* imgIteration = cvCreateImage( cvGetSize(imgGrey), IPL_DEPTH_8U, 1);73 cvCopyImage(srcImgGrey,imgIteration);74int thre3,nDiffRec;75 thre3 =DetectThreshold(imgIteration, 100, nDiffRec);76 cout<<"The Threshold of this Image in imgIteration is:"<<thre3<<endl;//输出显示阀值77 cvThreshold(imgIteration,imgIteration,thre3,255,CV_THRESH_BINARY_INV);//上下阀值78 cvNamedWindow("imgIteration", CV_WINDOW_AUTOSIZE );79 cvShowImage( "imgIteration", imgIteration);80 cvReleaseImage(&imgIteration);迭代1/*======================================================================*/2/* 迭代法*/3/*======================================================================*/4// nMaxIter:最大迭代次数;nDiffRec:使用给定阀值确定的亮区与暗区平均灰度差异值5int DetectThreshold(IplImage*img, int nMaxIter, int& iDiffRec) //阀值分割:迭代法6 {7//图像信息8int height = img->height;9int width = img->width;10int step = img->widthStep/sizeof(uchar);11 uchar *data = (uchar*)img->imageData;1213 iDiffRec =0;14int F[256]={ 0 }; //直方图数组15int iTotalGray=0;//灰度值和16int iTotalPixel =0;//像素数和17byte bt;//某点的像素值1819 uchar iThrehold,iNewThrehold;//阀值、新阀值20 uchar iMaxGrayValue=0,iMinGrayValue=255;//原图像中的最大灰度值和最小灰度值21 uchar iMeanGrayValue1,iMeanGrayValue2;2223//获取(i,j)的值,存于直方图数组F24for(int i=0;i<width;i++)25 {26for(int j=0;j<height;j++)27 {28 bt = data[i*step+j];29if(bt<iMinGrayValue)30 iMinGrayValue = bt;31if(bt>iMaxGrayValue)32 iMaxGrayValue = bt;33 F[bt]++;34 }35 }3637 iThrehold =0;//38 iNewThrehold = (iMinGrayValue+iMaxGrayValue)/2;//初始阀值39 iDiffRec = iMaxGrayValue - iMinGrayValue;4041for(int a=0;(abs(iThrehold-iNewThrehold)>0.5)&&a<nMaxIter;a++)//迭代中止条件42 {43 iThrehold = iNewThrehold;44//小于当前阀值部分的平均灰度值45for(int i=iMinGrayValue;i<iThrehold;i++)46 {47 iTotalGray += F[i]*i;//F[]存储图像信息48 iTotalPixel += F[i];49 }50 iMeanGrayValue1 = (uchar)(iTotalGray/iTotalPixel);51//大于当前阀值部分的平均灰度值52 iTotalPixel =0;53 iTotalGray =0;54for(int j=iThrehold+1;j<iMaxGrayValue;j++)55 {56 iTotalGray += F[j]*j;//F[]存储图像信息57 iTotalPixel += F[j];58 }59 iMeanGrayValue2 = (uchar)(iTotalGray/iTotalPixel);6061 iNewThrehold = (iMeanGrayValue2+iMeanGrayValue1)/2; //新阀值62 iDiffRec = abs(iMeanGrayValue2 - iMeanGrayValue1);63 }6465//cout<<"The Threshold of this Image in imgIteration is:"<<iThrehold<<endl; 66return 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 image7rows, cols --- size of image8x0, y0, dx, dy --- region of vector used for computing threshold9vvv --- debug option, is 0, no debug information outputed10*/11/*12OTSU 算法可以说是自适应计算单阈值(用来转换灰度图像为二值图像)的简单高效方法。

相关主题