Ask Your Question

meaning of cv2.threshold() parameters

asked 2015-02-26 10:13:47 -0500

begueradj gravatar image

updated 2015-02-26 10:44:50 -0500

I am testing the cv2.threshold() function in with different values but I get each time unexpected results. So this means simply I do not understand the meaning of 2 parameters and their effect:

  • dst
  • maxval

Could someone clear me on this ?


When I run this code:

import cv2






I do not see any result on the pictures I test (I want to draw their contour, they have white background)

Thank you in advance.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2015-02-26 10:21:56 -0500

Have you seen this:

dst ist the destination (output) of the function and maxval is a parameter for thresholding (e.g. for binary threshold, every pixel brightner than 'thres' is set to this value (for a 8UC1 image, maxval is set to 255 in most cases).

edit flag offensive delete link more


Thank you. I posted the same link as yours, but I do not understand the effect of the values of 2 parameters I posted. Especially for the destination array dimension.

begueradj gravatar imagebegueradj ( 2015-02-26 10:25:53 -0500 )edit

"Especially for the destination array dimension." The output has to have the same size as the input image (if you want to pass it as a parameter). But that's already said in the Docu: "dst – output array of the same size and type as src."

FooBar gravatar imageFooBar ( 2015-02-26 10:34:55 -0500 )edit

@FooBar thanks, I read that, but check my edit if you want.

begueradj gravatar imagebegueradj ( 2015-02-26 10:43:59 -0500 )edit

The second argument is the threshold (something like 128). im.size is something like the image size in byte (and in the order of some millions). Why do you pass that value as a threshold?

FooBar gravatar imageFooBar ( 2015-02-26 10:59:22 -0500 )edit

@FooBar no, the second argument is the destination and it must be of the same size as the input image

begueradj gravatar imagebegueradj ( 2015-02-26 11:04:13 -0500 )edit

@begueradj: I think you look at the C++ and not the python definition. For cv2.threshold() you don't need to specifiy the output (=dst) matrix. --> ret,thresh=cv2.threshold(imgray,128,255,cv2.THRESH_BINARY_INV) . Btw: if you don't want to deal with the threshold you can also use Otsu-method, which computes that for you: ret,thresh=cv2.threshold(imgray,128,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) (then the value 128 will not be used but an automatic computed one)

Guanta gravatar imageGuanta ( 2015-02-26 13:51:54 -0500 )edit

@Guanta Thank you very much for the information about Otsu stuff. Also you were right/ i confused the C++ side of OpenCV with the Python.

begueradj gravatar imagebegueradj ( 2015-02-26 16:43:00 -0500 )edit

Question Tools

1 follower


Asked: 2015-02-26 10:13:47 -0500

Seen: 788 times

Last updated: Feb 26 '15