七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)

七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)整理了一些主要的分割方法 以后用省的再查 其中大部分为转载资料 转载链接见资料 一 工具 VC OpenCV 二 语言 C 三 原理 nbsp nbsp nbsp 1 otsu 法 最大类间方差法 有时也称之为大津算法 使用的是聚类的思想 把图像的灰度数按灰度级分成 2 个部分 使得两个部分之间的灰度值差异最大 每个部分之间的灰度差异最小 通过方差的计算来寻找一个合适的灰度级别来划分 所以可以在二值化的

整理了一些主要的分割方法,以后用省的再查,其中大部分为转载资料,转载链接见资料;

一、工具:VC+OpenCV

二、语言:C++

三、原理

   (1) otsu法(最大类间方差法,有时也称之为大津算法)使用的是聚类的思想,把图像的灰度数按灰度级分成2个部分,使得两个部分之间的灰度值差异最大,每个部分之间的灰度差异最小,通过方差的计算来寻找一个合适的灰度级别 来划分。 所以 可以在二值化的时候 采用otsu算法来自动选取阈值进行二值化。otsu算法被认为是图像分割中阈值选取的最佳算法,计算简单,不受图像亮度和对比度的影响。因此,使类间方差最大的分割意味着错分概率最小。

设t为设定的阈值。

wo: 分开后 前景像素点数占图像的比例

uo: 分开后 前景像素点的平均灰度

w1:分开后 被景像素点数占图像的比例

u1: 分开后 被景像素点的平均灰度

u=w0*u0 + w1*u1 :图像总平均灰度

从L个灰度级遍历t,使得t为某个值的时候,前景和背景的方差最大, 则 这个 t 值便是我们要求得的阈值。

其中,方差的计算公式如下:

g=wo * (uo – u) * (uo – u) + w1 * (u1 – u) * (u1 – u)

[ 此公式计算量较大,可以采用: g = wo * w1 * (uo – u1) * (uo – u1) ]

由于otsu算法是对图像的灰度级进行聚类,so 在执行otsu算法之前,需要计算该图像的灰度直方图。

(2)迭代法原理:迭代选择法是首先猜测一个初始阈值,然后再通过对图像的多趟计算对阈值进行改进的过程。重复地对图像进行阈值操作,将图像分割为对象类和背景类,然后来利用每一个类中的灰阶级别对阈值进行改进。

   1 .处理流程:

       适合图像直方图有明显波谷

 

(3)

直方图双峰法(mode 法)

  Prewitt 等人于六十年代中期提出的直方图双峰法(也称 mode 法) 是典型的全局单阈值分割方法。该方法的基本思想是:假设图像中有明显的目标和背景,则其灰度直方图呈双峰分布,当灰度级直方图具有双峰特性时,选取两峰之间的谷对应的灰度级作为阈值。如果背景的灰度值在整个图像中可以合理地看作为恒定,而且所有物体与背景都具有几乎相同的对比度,那么,选择一个正确的、固定的全局阈值会有较好的效果。例如图3.1所示:

七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)

图3.1原始灰度图像

七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)

图3.2灰度直方图

选定阈值M为100

 

算法实现:找到第一个峰值和第二个峰值, 再找到第一和第二个峰值之间的谷值,谷值就是那个阀值了。

 

 

四、程序

主程序(核心部分) 

阈值分割 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_C 14 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 pg[256],i,thre; 33 for (i=0;i<256;i++) pg[i]=0; 34 for (i=0;i 
  
    imageSize;i++) // 直方图统计 35 pg[(BYTE)imgBasicGlobalThreshold->imageData[i]]++; 36 thre = BasicGlobalThreshold(pg,0,256); // 确定阈值 37 cout<<"The Threshold of this Image in BasicGlobalThreshold is:"< 
   
     height; 9 int width = img->width; 10 int step = img->widthStep/sizeof(uchar); 11 uchar *data = (uchar*)img->imageData; 12 13 iDiffRec =0; 14 int F[256]={ 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)的值,存于直方图数组F 24 for(int i=0;i 
    
      iMaxGrayValue) 32 iMaxGrayValue = bt; 33 F[bt]++; 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)&&a 
     
       gmax) gmax=*np; 41 if(*np < gmin) gmin=*np; 42 np++; /* next pixel */ 43 } 44 } 45 46 // set up everything 47 sum = csum =0.0; 48 n =0; 49 50 for (k =0; k <=255; k++) 51 { 52 sum += (double) k * (double) ihist[k]; /* x*f(x) 质量矩*/ 53 n += ihist[k]; /* f(x) 质量 */ 54 } 55 56 if (!n) 57 { 58 // if n has no value, there is problems... 59 fprintf (stderr, "NOT NORMAL thresholdValue = 160\n"); 60 return (160); 61 } 62 63 // do the otsu global thresholding method 64 fmax =-1.0; 65 n1 =0; 66 for (k =0; k <255; k++) 67 { 68 n1 += ihist[k]; 69 if (!n1) 70 { 71 continue; 72 } 73 n2 = n - n1; 74 if (n2 ==0) 75 { 76 break; 77 } 78 csum += (double) k *ihist[k]; 79 m1 = csum / n1; 80 m2 = (sum - csum) / n2; 81 sb = (double) n1 *(double) n2 *(m1 - m2) * (m1 - m2); 82 /* bbg: note: can be optimized. */ 83 if (sb > fmax) 84 { 85 fmax = sb; 86 thresholdValue = k; 87 } 88 } 89 90 // at this point we have our thresholding value 91 92 // debug code to display thresholding values 93 if ( vvv &1 ) 94 fprintf(stderr,"# OTSU: thresholdValue = %d gmin=%d gmax=%d\n", 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 ihist[256]; // 图像直方图,256个点 13 14 int i, j, k; // various counters 15 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 < h; i++) 24 { 25 np = (unsigned char*)(image->imageData + image->widthStep*i); 26 for (j =0; j < w; j++) 27 { 28 pixel = np[j]; 29 ihist[ pixel]++; 30 if(pixel > gmax) gmax= pixel; 31 if(pixel < gmin) gmin= pixel; 32 } 33 } 34 35 // set up everything 36 sum = csum =0.0; 37 n =0; 38 39 for (k =0; k <=255; k++) 40 { 41 sum += k * ihist[k]; /* x*f(x) 质量矩*/ 42 n += ihist[k]; /* f(x) 质量 */ 43 } 44 45 if (!n) 46 { 47 // if n has no value, there is problems... 48 //fprintf (stderr, "NOT NORMAL thresholdValue = 160\n"); 49 thresholdValue =160; 50 goto L; 51 } 52 53 // do the otsu global thresholding method 54 fmax =-1.0; 55 n1 =0; 56 for (k =0; k <255; k++) 57 { 58 n1 += ihist[k]; 59 if (!n1) { continue; } 60 n2 = n - n1; 61 if (n2 ==0) { break; } 62 csum += k *ihist[k]; 63 m1 = csum / n1; 64 m2 = (sum - csum) / n2; 65 sb = n1 * n2 *(m1 - m2) * (m1 - m2); 66 /* bbg: note: can be optimized. */ 67 if (sb > fmax) 68 { 69 fmax = sb; 70 thresholdValue = k; 71 } 72 } 73 74 L: 75 for (i =0; i < h; i++) 76 { 77 np = (unsigned char*)(image->imageData + image->widthStep*i); 78 for (j =0; j < w; j++) 79 { 80 if(np[j] >= thresholdValue) 81 np[j] =255; 82 else np[j] =0; 83 } 84 } 85 86 //cout<<"The Threshold of this Image in Otsu is:"< 
      
        depth ==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 double maxentropy =-1.0; 52 int max_index =-1; 53 // 循环测试每个分割点,寻找到最大的阈值分割点 54 for(int i=0;i 
       
         maxentropy) 58 { 59 maxentropy = cur_entropy; 60 max_index = i; 61 } 62 } 63 cout<<"The Threshold of this Image in MaxEntropy is:"< 
         
        
       
      
     
    
  

https://blog.csdn.net/xw/article/details/ 

https://www.cnblogs.com/wangduo/p/5556903.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/178969.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午4:06
下一篇 2026年3月26日 下午4:06


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号