The Ostu algorithm of graduation project---and its binary value

Source: Internet
Author: User


two-valued Ostu algorithm:


#include "stdafx.h"
#include <stdio.h>
#include <cv.h>
#include
#include <math.h>
int Otsu (iplimage* src);
  
int _tmain (int argc, _tchar* argv[])
{  
iplimage* img = cvloadimage ("Lena.jpg", 0);
iplimage* DST = cvcreateimage (Cvgetsize (IMG), 8, 1);
int threshold = Otsu (IMG);
  
cvthreshold (IMG, DST, threshold, 255, cv_thresh_binary);
  
  
Cvnamedwindow ("img", 1);
cvshowimage ("img", DST);
  
  
Cvwaitkey (-1);
  
cvreleaseimage (&IMG);
cvreleaseimage (&DST);
  
Cvdestroywindow ("DST");
return 0;
}  
  
int Otsu (iplimage* src)
{    
int height=src->height;
int width=src->width;
Long size = height * width;
  
//histogram
float histogram[256] = {0};
for (int m=0; m < height; m++)
    {    
unsigned char* p= (unsigned char*) Src->imagedata + src->widthstep * m;
for (int n = 0; n < width; n++)
        {    
histogram[int (*p++)]++;
        }    
    }    
  
int threshold;
long SUM0 = 0, sum1 = 0;//storage foreground gray sum and background gray sum
long cnt0 = 0, cnt1 = 0;//total number of foreground and total number of backgrounds
double w0 = 0, w1 = 0;//ratio of the entire image to the foreground and background
Double u0 = 0, u1 = 0; Average grayscale of foreground and background
double variance = 0;//maximum Inter-class variance
int I, J;
double u = 0;
double maxvariance = 0;
For (i = 1; i < i++)//traversing each pixel at a time
    {    
SUM0 = 0;
sum1 = 0;
cnt0 = 0;
cnt1 = 0;
w0 = 0;
W1 = 0;
For (j = 0; J < i; j + +)
        {  
cnt0 + = Histogram[j];
SUM0 + = J * Histogram[j];
        }  
  
u0 = (double) sum0/cnt0;
w0 = (double) cnt0/size;
  
For (j = i; J <= 255; j + +)
        {  
cnt1 + = Histogram[j];
Sum1 + = J * Histogram[j];
        }  
  
u1 = (double) sum1/cnt1;
W1 = 1-w0;//(double) cnt1/size;
  
u = u0 * w0 + u1 * W1;//average grayscale of image
printf ("U =%f\n", u);
//variance = w0 * POW ((u0-u), 2) + W1 * POW ((u1-u), 2);
Variance = w0 * W1 * (U0-U1) * (U0-U1);
if (Variance > Maxvariance)
        {    
maxvariance = variance;
threshold = i;
        }   
    }    
  
printf ("threshold =%d\n", threshold);
return threshold;
}  


The Ostu algorithm of graduation project---and its binary value

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.