title: Why does thresholding on image give a different result compared to when it is in an image with other things?
body: <p>I have an image that when I apply a binary threshold on it, it gives a different result compared to when it is in an image with other things. I'm not sure if I'm setting the parameters for the thresholding function incorrectly as I took the example from the OpenCV documentation. Link to documentation: <a href="https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html" rel="nofollow noreferrer">https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html</a></p>
  <p>In my test program, I read in two images. One where the object is by itself and another where it is in an image with other objects. When I applied the threshold to the image1 I got the result I wanted. However, when I applied the threshold to image2 I partially got the result I wanted, but one of the blocks did not threshold correctly. Any ideas as to what may be causing this?</p>
  <p><a href="https://i.stack.imgur.com/xXB0m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xXB0m.png" alt="Image1 and its resultant threshold" /></a></p>
  <p><a href="https://i.stack.imgur.com/N4Cfz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N4Cfz.png" alt="Image2 and its resultant threshold" /></a></p>
  <pre><code>#Reading in image1
  img1 = cv2.imread(&quot;Templates/YellowBlocks/SS.png&quot;, cv2.IMREAD_COLOR)
  #Reading in image2
  img2 = cv2.imread(&quot;Test Program 1.png&quot;, cv2.IMREAD_COLOR)
  #Converting image1 to grayscale
  grayImg1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  #Converting image2 to grayscale
  grayImg2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  #Applying a binary threshold on image1 to show text in black and white
  ret1,thresh1 = cv2.threshold(grayImg1, 0, 255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
  #Applying a binary threshold on image2 to show text in black and white
  ret2,thresh2 = cv2.threshold(grayImg2, 0, 255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
  #Displaying image1
  cv2.imshow('img1', img1)
  #Displaying thresholded image1
  cv2.imshow('thresh1', thresh1)
  #Displaying image2
  cv2.imshow('img2', img2)
  #Displaying thresholded image2
  cv2.imshow('thresh2', thresh2)
  cv2.waitKey(0)
  </code></pre>