0

I saw this declaration in Python, but I don't understand what it means and can't find an explanation:

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

The question is: why is there there a comma between ret and thresh? What type of assignment is that?

3 Answers 3

2

That's a "tuple" or "destructuring" assignment - see e.g. Multiple assignment semantics. cv2.threshold returns a tuple containing two values, so it's equivalent to:

temp = cv2.threshold(...)
ret = temp[0]
thresh = temp[1]

See Assignment Statements in the language reference:

If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

Sign up to request clarification or add additional context in comments.

Comments

0

This is a value unpacking syntax.
cv2.threshold(imgray,127,255,0) returns a two element tuple.
With this syntax you assign elements of this tuple to separate variables ret and thresh.

Comments

0

You can use this syntax to unpack tuples to single variables, e. g.:

a, b = (0, 1)
# a == 0
# b == 1

Your code is the same as:

result = cv2.threshold(...)
ret = result[0]
thresh = result[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.