Ask Your Question
0

meaning of cv2.threshold() parameters

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

begueradj gravatar image

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

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 ?

EDIT:

When I run this code:

import cv2


im=cv2.imread('image.jpg')
imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

ret,thresh=cv2.threshold(imgray,im.size,255,cv2.THRESH_BINARY_INV)

countours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)


cv2.drawContours(im,countours,-1,(0,255,0),3)
cv2.imshow("Begueradj",im)

cv2.waitKey(0)
cv2.destroyAllWindows()

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
2

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

Have you seen this: http://docs.opencv.org/modules/imgpro...

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

Comments

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 -0600 )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 -0600 )edit

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

begueradj gravatar imagebegueradj ( 2015-02-26 10:43:59 -0600 )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 -0600 )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 -0600 )edit
1

@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 -0600 )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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 889 times

Last updated: Feb 26 '15