title: Python: Contour around rectangle based on specific color on a dark image (OpenCV)
body: <p>since I'm trying to enhance my skills with OpenCV in Python, I would like to know what's the best way of extracting a specific gray tone out of a image with mostly dark colors.</p>
  
  <p>To start of, I created a test image in order to test different methods with OpenCV:</p>
  
  <p>Lets say I want to extract a specific color in this image and add a border to it. For now I chose the gray rectangle in the middle with the color (33, 33, 34 RGB), see following: </p>
  
  <p>(Here's the image without the red border in order you want to test your ideas: <a href="https://i.stack.imgur.com/Zf8Vb.png" rel="nofollow noreferrer">https://i.stack.imgur.com/Zf8Vb.png</a>)</p>
  
  <p><a href="https://i.stack.imgur.com/76JQv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/76JQv.png" alt="example image with border"></a></p>
  
  <p>This is what I've tried so far, but it's not quite working: </p>
  
  <pre><code>img = cv2.imread(path) #Read input image
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Convert from BGR to HSV color space
  saturation_plane = hsv[:, :, 1] # all black/white/gray pixels are zero, and colored pixels are above zero
  _, thresh = cv2.threshold(saturation_plane, 8, 255, cv2.THRESH_BINARY) # Apply threshold on s
  contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # draw all contours 
  contours = contours[0] if len(contours) == 2 else contours[1]
  result = img.copy()
  
  for contour in contours:
     (x, y, w, h) = cv2.boundingRect(contour) # compute the bounding box for the contour
     if width is equal to the width of the rectangle i want to extract:
         draw contour
  </code></pre>
  
  <p>What if the size of the rectangle is not fixed, so that I won't be able to detect it through its width/height? Moreover, is it better to convert the image into a gray scale instead of HSV? I'm just new to it and I would like to hear your way of achieving this.</p>
  
  <p>Thanks in advance.</p>