Threshold Segmentation: Basic global threshold method, upper and lower threshold method, iterative method

Source: Internet
Author: User
Tags abs


/*---------------------------------------------------------------------------*/* Basic Global threshold method */iplimage*
    Imgbasicglobalthreshold = Cvcreateimage (Cvgetsize (Imggrey), ipl_depth_8u,1);
Cvcopyimage (Srcimggrey,imgbasicglobalthreshold);    
int Pg[256],i,thre;
for (i=0;i<256;i++) pg[i]=0; for (i=0;iimagesize;i++)//Histogram statistics pg[(BYTE) Imgbasicglobalthreshold->imag    
    edata[i]]++;    thre = Basicglobalthreshold (pg,0,256);
     Determine threshold cout<< "The Threshold of this Image in Basicglobalthreshold is:" <<thre<<endl;//output display threshold  Cvthreshold (imgbasicglobalthreshold,imgbasicglobalthreshold,thre,255,cv_thresh_binary);
    Binary Cvnamedwindow ("Basicglobalthreshold", cv_window_autosize); Cvshowimage ("Basicglobalthreshold", imgbasicglobalthreshold);//Display Image Cvreleaseimage (&imgbasicglobalthreshold)
; /*---------------------------------------------------------------------------*/* Upper and lower threshold method: Use normal distribution to find confidence interval */Iplimage* Imgtopdown = Cvcreateimage (Cvgetsize (Imggrey), ipl_depth_8u, 1);
    Cvcopyimage (Srcimggrey,imgtopdown);
    Cvscalar mean, std_dev;//average, standard deviation double u_threshold,d_threshold;    
    CVAVGSDV (Imgtopdown,&mean,&std_dev,null); U_threshold = mean.val[0] +2.5* std_dev.val[0];//upper threshold d_threshold = mean.val[0] -2.5* std_dev.val[0];//lower threshold//u_threshol D = mean + 2.5 * STD_DEV;
    Error//d_threshold = mean-2.5 * STD_DEV; cout<< "The topthreshold of this Image in Topdown is:" <<d_threshold<<endl;//output shows threshold cout<< "the D
    Ownthreshold of this Image in Topdown is: "<<u_threshold<<endl; Cvthreshold (IMGTOPDOWN,IMGTOPDOWN,D_THRESHOLD,U_THRESHOLD,CV_THRESH_BINARY_INV);//up/Down threshold CvNamedWindow ("
    Imgtopdown ", cv_window_autosize);
Cvshowimage ("Imgtopdown", imgtopdown);//Display Image cvreleaseimage (&imgtopdown); /*---------------------------------------------------------------------------*/* Iterative method */iplimage* Imgiteration = CvcreateimaGE (cvgetsize (imggrey), ipl_depth_8u, 1);
Cvcopyimage (srcimggrey,imgiteration);
    int Thre3,ndiffrec;
    Thre3 =detectthreshold (imgiteration, Ndiffrec); cout<< "The Threshold of this Image in Imgiteration is:" <<thre3<<endl;//output shows threshold Cvthreshold (Imgiterati
    ON,IMGITERATION,THRE3,255,CV_THRESH_BINARY_INV);//upper and lower threshold Cvnamedwindow ("imgiteration", cv_window_autosize);
    Cvshowimage ("Imgiteration", imgiteration); Cvreleaseimage (&imgiteration);



/*======================================================================*//* Iterative method *//*============================ ==========================================*///Nmaxiter: Maximum number of iterations; Ndiffrec: The average gray difference between the light and dark areas using the given threshold value int Detectthreshold (iplimage*img, int nmaxiter, int& idiffrec)//Threshold Segmentation: Iterative Method {//image information int height = img->height; int width = Img->widt
H
    int step = img->widthstep/sizeof (Uchar);

    Uchar *data = (uchar*) img->imagedata;
Idiffrec = 0; int f[256]={0};
    Histogram array int itotalgray=0;//gray value and int itotalpixel =0;//pixels and byte bt;//pixel value Uchar ithrehold,inewthrehold;//threshold, new threshold

The maximum gray value and the minimum gray value Uchar imeangrayvalue1,imeangrayvalue2 in the original image of Uchar imaxgrayvalue=0,imingrayvalue=255;//; Gets the value of (i,j), stored in the histogram array f for (int i=0;i<width;i++) {for (int j=0;j 

/*============================================================================ = Code content: Basic Global Threshold method ==============================================================================*/int BasicGlobalThreshold (INT*PG    
    , int start,int end) {//Basic global threshold method int i,t,t1,t2,k1,k2; double u,u1,u2;     
    t=0;
u=0;        
        for (i=start;i<end;i++) {t+=pg[i];
    U+=i*pg[i];                          } k2= (int) (U/T);
        Calculates the mean of this range grayscale do {k1=k2;    
        t1=0;
u1=0;    
            for (i=start;i<=k1;i++) {//calculates the accumulation and t1+=pg[i of low gray groups];
        U1+=i*pg[i];
        } t2=t-t1;
U2=U-U1;                     if (t1) u1=u1/t1;
Calculates the average of the low gray group else u1=0;                     if (T2) u2=u2/t2;
        Calculates the average of the high gray group else u2=0;                 k2= (int) ((U1+U2)/2); Get a new threshold estimate} while (k1!=K2);
The data is not stable, continue//cout<< "The Threshold of this Image in Basicglobalthreshold is:" <<k1<<endl;                              return (K1); Return threshold}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.