Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int determine_optimal_threshold( CvHistogram* hist )
- {
- // TO DO: Given a 1-D CvHistogram you need to determine and return the optimal threshold value.
- // NOTES: Assume there are 256 elements in the histogram.
- // To get the histogram value at index i
- // int histogram_value_at_i = ((int) *cvGetHistValue_1D(hist, i));
- int threshold = 127;
- int thresh2 = 0;
- while(threshold != thresh2){
- double background1= 1;
- double background2= 1;
- double front1=1;
- double front2=1;
- int i = 0;
- for (int i=0;i<256;i++){
- int histogram_value_at_i = ((int) *cvGetHistValue_1D(hist,i));
- //work with the foreground
- if(i > threshold){
- front2 = front2 + (i*histogram_value_at_i);
- front1 = front1 + histogram_value_at_i;
- }
- else
- {
- //else were working with the background
- background2 = background2 + (i*histogram_value_at_i);
- background1 = background1 + histogram_value_at_i;
- }
- }
- background2 = (double)background2/ (double)background1;
- front2 = (double)front2/(double)front1;
- thresh2 = threshold;
- threshold = (int)(((double)background2 + (double)front2)/2);
- printf("\nTOTAL_THRESH: %d",threshold);
- }
- printf("\nOptimal Threshold is : %d ", threshold);
- return threshold; // Just so that the project will compile...
- }
Add Comment
Please, Sign In to add comment