views:

118

answers:

3

I'm trying to detect the first valley in a 1D histogram, to use as a threshold point. (Processing dark to light / left to right)

Unfortunately some of the histograms have mini peaks, gaps and jagged edges. The algorithm I've written is getting stuck on these. I think I need to smooth away the rough edges as the peaks valleys I need are quite prominent.

Does anyone have some pointers on to the best way to smooth a 1D histogram in OpenCV?

This shows where the algorithm got caught on a jagged edge and failed:

http://applist.s3.amazonaws.com/junk/failed.png

Blue line: peak
Red line: valley

+1  A: 

It seems to me you are performing a segmentation of some sort. You could try doing this with an adaptive algorithm, which sets the threshold itself by calculating some average values of points. This particular algorithm assumes that the boundary points represent the background, while the rest of the points represent the object. Here's the algorithm:

calculate u1 -> the grayscale average of boundary points
calculate u2 -> the grayscale average of all other points
T_old = 0
T_new = (u1 + u2) / 2
while (T_new != T_old) 
 //you might want to modify this by introducing an epsilon value, something like
 // if T_new near T_old (abs(T_new-T_old)>1)
 u1 = grayscale average of points where grayscale intensity is lower than T_new
 u2 = grayscale average of points where grayscale intensity is higher or equal to T_new
 T_old =T _new
 T_new = (u1 + u2) / 2
end
threshold = T_new

This should find the near optimum threshold for a grayscale image.

brozo
+1  A: 

Try blurring the histogram or image.

The valeys could be there, because not all image illumination levels are used on the source image. You could easily fix this by blurring image before doing the histogram. Or try to do some histogram moving average so the abrupt changes would be gone.

Rekin
Smashing, this has worked really well. I applied a Gaussian blur to the source image before calculating the histogram. Nice an simple, thanks!
developroom
A: 

Possible ways to get over this:

  • Approximate the histogram with gaussian curves
  • Try dilating and then eroding the histogram image
Utkarsh Sinha