Guest User

Untitled

a guest
Jan 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. int determine_optimal_threshold( CvHistogram* hist )
  2. {
  3.  
  4. // TO DO: Given a 1-D CvHistogram you need to determine and return the optimal threshold value.
  5.  
  6. // NOTES: Assume there are 256 elements in the histogram.
  7. // To get the histogram value at index i
  8. // int histogram_value_at_i = ((int) *cvGetHistValue_1D(hist, i));
  9.  
  10. int threshold = 127;
  11. int thresh2 = 0;
  12.  
  13. while(threshold != thresh2){
  14. double background1= 1;
  15. double background2= 1;
  16. double front1=1;
  17. double front2=1;
  18. int i = 0;
  19. for (int i=0;i<256;i++){
  20. int histogram_value_at_i = ((int) *cvGetHistValue_1D(hist,i));
  21. //work with the foreground
  22. if(i > threshold){
  23. front2 = front2 + (i*histogram_value_at_i);
  24. front1 = front1 + histogram_value_at_i;
  25. }
  26. else
  27. {
  28. //else were working with the background
  29. background2 = background2 + (i*histogram_value_at_i);
  30. background1 = background1 + histogram_value_at_i;
  31. }
  32. }
  33.  
  34. background2 = (double)background2/ (double)background1;
  35. front2 = (double)front2/(double)front1;
  36. thresh2 = threshold;
  37. threshold = (int)(((double)background2 + (double)front2)/2);
  38. printf("\nTOTAL_THRESH: %d",threshold);
  39.  
  40. }
  41.  
  42.  
  43. printf("\nOptimal Threshold is : %d ", threshold);
  44. return threshold; // Just so that the project will compile...
  45. }
Add Comment
Please, Sign In to add comment