13

I have this gray video stream: enter image description here

The histogram of this image:

enter image description here

The thresholded image by :

  threshold( image, image, 150, 255, CV_THRESH_BINARY );

i get :

enter image description here

Which i expect.

When i do adaptive thresholding with :

adaptiveThreshold(image, image,255,ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,15,-5);

i get :

enter image description here

Which looks like edge detection and not thresholding. What i expected was black and white areas . So my question is, why does this look like edge detection and not thresholding.

thx in advance

Olivier_s_j
  • 5,490
  • 24
  • 80
  • 126
  • Adaptive Threshold works very well. Even much better than a static threshold especially for images from a camera. But it depends on what you want to do. Your image is a VERY bad example. Your last image only shows that your parameters are completely wrong. Your first parameter mut have a significantly higher value to avoid that "edge detection" effect. There are two parameters to be passed to adaptiveThreshold() : Both affect the result. Experiment with them until you understand what they do. And use a better image for testing! – Elmue Jan 10 '18 at 14:45

2 Answers2

10

Adaptive Threshold works like this:

The function transforms a grayscale image to a binary image according to the formulas:

    THRESH_BINARY

THRESH_BINARY

    THRESH_BINARY_INV

THRESH_BINARY_INV

where T(x,y) is a threshold calculated individually for each pixel.

Threshold works differently:

The function applies fixed-level thresholding to a single-channel array.

So it sounds like adaptiveThreshold calculates a threshold pixel-by-pixel, whereas threshold calculates it for the whole image -- it measures the whole image by one ruler, whereas the other makes a new "ruler" for each pixel.

djhaskin987
  • 9,741
  • 4
  • 50
  • 86
  • If i would do a blob analysis on the image which has been thresholded adaptively. Would the middle square be 1 blob ? (I'm doing surface detection, still have to see how the blob detection works in opencv, that's why i ask it first) – Olivier_s_j Nov 29 '11 at 18:22
  • I don't know that it would, probably the white specs would be. Look at this book, page 231, section 6.2 for a comparison of blob analysis with different thresholding: http://books.google.com/books?id=dbPaBUaLzcIC&pg=PA228&lpg=PA228&dq=image+blob+analysis+adaptive+threshold&source=bl&ots=_yVSlzAvOP&sig=vbbz2jac82rMGn1CpCXx1JJnTdI&hl=en&ei=gSTVTsGaN-OuiQK0vNWyDg&sa=X&oi=book_result&ct=result&resnum=3&ved=0CDYQ6AEwAg#v=onepage&q=image%20blob%20analysis%20adaptive%20threshold&f=false – djhaskin987 Nov 29 '11 at 18:34
  • 1
    I still do not get the behavior. What makes adaptive threshold different from lets say, applying a Gaussian blur then subtracting it and do a normal threshold. According to what the document describes they should be the same, but they are not, maybe sigma is a very big number. – dashesy Aug 17 '15 at 16:45
10

I had the same issue doing adaptive thresholding for OCR purposes. (sorry this is Python not C++)

img = cv.LoadImage(sys.argv[1])
bwsrc = cv.CreateImage( cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
bwdst = cv.CreateImage( cv.GetSize(img), cv.IPL_DEPTH_8U, 1)

cv.CvtColor(img, bwsrc, cv.CV_BGR2GRAY)
cv.AdaptiveThreshold(bwsrc, bwdst, 255.0, cv.CV_THRESH_BINARY, cv.CV_ADAPTIVE_THRESH_MEAN_C,11)
cv.ShowImage("threshhold", bwdst)
cv.WaitKey()

The last paramter is the size of the neighborhood used to calculate the threshold for each pixel. If your neighborhood is too small (mine was 3), it works like edge detection. Once I made it bigger, it worked as expected. Of course, the "correct" size will depend on the resolution of your image, and size of the features you're looking at.

nont
  • 9,322
  • 7
  • 62
  • 82
  • On a hunt I found this answer, can you please tell us what kind of datatypes are used for img, bwscr, and bwdst? Is it Mat ? – Kev1n91 Dec 09 '16 at 13:28
  • 1
    I don't recall exactly, but here's a script I was working on around the time I answered this question: https://github.com/ludflu/ocrprep/blob/master/ocrprep.py – nont Dec 09 '16 at 14:11
  • I only see now that it is in python, I thought it is C++ – Kev1n91 Dec 09 '16 at 14:19