Statement:
Title bar, do not want to explain too much, usually learn the image processing, should all know the principle, do not know in the following references, can also find.
I'm just here to put the code.
Code:
1#include <iostream>2#include <stdio.h>3#include <math.h>4 5 using namespacestd;6 7 intHistogram (unsigned ShortAint* Hist,intHintW)8 {9 if(A = = NULL | | hist = =NULL) {Ten return-1; One } A for(inti =0; I < POW (2, A); i++){ -Hist[i] =0; - } the for(inti =0; I < H; i++){ - for(intj =0; J < W; J + +){ -Hist[a[j + i*w]]++; - } + } - return 0; + } A at intOTSU (int* Hist,int&thresh,intmax) - { - if(hist = =NULL) - return-1; - Doublesum0=0; - Doublesum1=0; in Doublen0=0; - Doublen1=0; to Doubleu0=0; + Doubleu1=0; - Doublew0=0; the Doublew1=0; * Doubleu=0; $ Doublen =0;Panax Notoginseng Doubleg =-1; - DoubleTempg =-1; the for(inti =0; i < Max; i++){ +n+=Hist[i]; A } the for(inti =0; i < Max; i++){ +SUM0 =0; -SUM1 =0; $N0 + =Hist[i]; $N1 = n-N0; - if(N1 = =0) - Break; theW0 = N0/N; -W1 =1-W0;Wuyi for(intj =0; J <= I; J + +){ theSUM0 + = j*Hist[j]; - } WuU0 = SUM0/N0; - for(intK = i +1; K < Max; k++){ AboutSum1 + = k*Hist[k]; $ } -u1 = sum1/N1; -g = w0*w1* (u0-u1) * (U0-U1); - if(Tempg <g) A { +Tempg =G; theThresh =i; - } $ } the returnThresh; the } the the intMain () - { in intheight=3072; the intWidth=3072; the intcount; AboutUnsigned Short* img = (unsigned Short*)malloc(sizeof(unsigned Short) *height*width); thefile*FP; thefp = fopen ("Test_image.raw","RB"); the if(fp = =NULL) { +cout <<"failed to open file"<<Endl; - return-1; the }Bayi Else{ theCount = Fread (IMG,sizeof(unsigned Short), height*width, FP); the } - fclose (FP); - the intmax = POW (2, A); the the int* hist = (int*)malloc(max*sizeof(int)); the - Histogram (img, hist, height, width); the the thefp = fopen ("Hist.raw","WB");94Fwrite (hist,sizeof(int), Max, FP); the fclose (FP); the the intThresh =0;98 OTSU (hist, Thresh, max); Aboutcout << Thresh <<Endl; - 101 return 0;102}View Code
Reference documents:
1. Adaptive threshold Algorithm (Dajing threshold method)
2. Otsu ' s method
Dajing Threshold Segmentation + histogram