title: Python - OpenCV - Binarize To Isolate Object Which is Same Color as Background
body: <p>I need to isolate the cardboard target in the image below and binarize it, so that the target is white and the background black. Normally, this is not a problem, but the background is almost the exact same color as the target.</p>
  
  <p>Attempts:</p>
  
  <pre><code># LOAD IMAGE
  img_filepath = 'real_6.png'
  img = cv2.imread( img_filepath )
  rgb_img = img[:,:,::-1]
  plt.imshow( rgb_img )
  plt.title('ORIGINAL')
  plt.show()
  
  img_gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
  
  # SMOOTH
  blur_kernel = np.ones((5,5),np.float32)/30
  blur_img = cv2.filter2D( rgb_img, -1, blur_kernel )
  
  # THRESHOLD 
  lower_color_rng = np.array( [100,50,100] )
  upper_color_rng = np.array( [255,255,255] )
  target_keyholes_img = cv2.inRange( blur_img, lower_color_rng, upper_color_rng )
  plt.imshow( target_keyholes_img, cmap='gray' )
  plt.title( 'THRESHOLD' )
  plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/joml8.png" rel="nofollow noreferrer">Attempted Image Extraction</a></p>
  
  <p>How can I use OpenCV in Python 3 to binarize this image?</p>
  
  <p><a href="https://i.stack.imgur.com/1Mecw.png" rel="nofollow noreferrer">Original Image</a></p>