Ask Your Question
0

cv2 threshold flags

asked 2017-06-26 04:48:44 -0500

akash29 gravatar image

updated 2017-06-26 05:03:42 -0500

berak gravatar image

What is the difference between cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU) and cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY + cv2.THRESH_OTSU) ?

# Otsu's thresholding
gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)**[1]**

In the end, there's [1] written what does it mean? I am trying to use automatic threshold.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-06-26 05:17:41 -0500

berak gravatar image

updated 2017-06-26 05:43:21 -0500

  1. for "binary flags" (pow 2) like 0,1,2,4,8,16, etc | and + are equivalent operations
  2. cv2.threshold returns the binary image and also the threshold value (useful, if you use OTSU). have a look:


>>> help(cv2.threshold)
Help on built-in function threshold:

threshold(...)
    threshold(src, thresh, maxval, type[, dst]) -> retval, dst

in your case above it means: keep only the image, and discard the threshold value.

edit flag offensive delete link more

Comments

In case of finding contours, cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2] what does -2 implies? Thanks for help.

akash29 gravatar imageakash29 ( 2017-06-26 05:28:34 -0500 )edit
1

-2 is "the second result from the right"

(but who on earth would overcomplicate simple things unnessecary like this ?)

berak gravatar imageberak ( 2017-06-26 05:45:51 -0500 )edit

I was going through some tutorial on watershed segmentation process and finding contours.

akash29 gravatar imageakash29 ( 2017-06-26 05:53:37 -0500 )edit

yea, let's continue, with the other question regarding watershed.

berak gravatar imageberak ( 2017-06-26 05:55:59 -0500 )edit

Thanks a lot.

akash29 gravatar imageakash29 ( 2017-06-26 06:00:52 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-06-26 04:48:44 -0500

Seen: 132 times

Last updated: Jun 26 '17