title: Find dominant color on contour opencv
body: <p>I am trying to find the dominant color inside a contour (black or white).
  I am using OpenCV to read an image and extract white on black images. This is what I got so far:
  <a href="https://i.stack.imgur.com/HtFBh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HtFBh.jpg" alt="enter image description here"></a></p>
  
  <p>The green outline is the contour, the blue lines the bounding box. So I this instance I am trying to extract the numbers 87575220 but as you can see it also recognizes some random artifacts and for instance the letter G. I think the solution would be to find the dominant colour inside of the contours and that colour should be close to white. I don't have any idea how to do this though.</p>
  
  <p>This the code I have at the moment:</p>
  
  <pre><code>import argparse
  import cv2
  import imutils
  import numpy as np
  
  parser = argparse.ArgumentParser()
  parser.add_argument("--image", "-i", required=True, help="Image to detect blobs from")
  args = vars(parser.parse_args())
  
  image = cv2.imread(args["image"])
  image = imutils.resize(image, width=1200)
  grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(grey)
  maxval_10 = maxVal * 0.5
  ret, threshold = cv2.threshold(grey, maxval_10, 255, cv2.THRESH_BINARY)
  canny = cv2.Canny(grey, 200, 250)
  lines = cv2.HoughLines(canny, 1, np.pi / 180, 140)
  
  print(maxVal)
  
  theta_min = 60 * np.pi / 180.
  theta_max = 120 * np.pi / 180.0
  theta_avr = 0
  theta_deg = 0
  filteredLines = []
  for rho, theta in lines[0]:
      a = np.cos(theta)
      b = np.sin(theta)
      x0 = a * rho
      y0 = b * rho
      x1 = int(x0 + 1000 * (-b))
      y1 = int(y0 + 1000 * (a))
      x2 = int(x0 - 1000 * (-b))
      y2 = int(y0 - 1000 * (a))
  
      cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
  
      if theta_min &lt;= theta &lt;= theta_max:
          filteredLines.append(theta)
          theta_avr += theta
  
  if len(filteredLines) &gt; 0:
      theta_avr /= len(filteredLines)
      theta_deg = (theta_avr / np.pi * 180) - 90
  else:
      print("Failed to detect skew")
  
  image = imutils.rotate(image, theta_deg)
  canny = imutils.rotate(canny, theta_deg)
  
  im2, contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  # cv2.drawContours(image, contours, -1, (0, 255, 0), 1)
  cv2.imshow('Contours', im2)
  
  boundingBoxes = []
  filteredContours = []
  
  for cnt in contours:
      (x, y, w, h) = cv2.boundingRect(cnt)
      if (h &gt; 20 and h &lt; 90 and w &gt; 5 and w &lt; h):
          if cv2.contourArea(cnt, True) &lt;= 0:
              boundingBoxes.append((x, y, w, h))
              filteredContours.append(cnt)
  
  for x, y, w, h in boundingBoxes:
      cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
  
  cv2.drawContours(image, filteredContours, -1, (0, 255, 0), 1)
  
  cv2.imshow('Image', image)
  cv2.imshow('Edges', canny)
  cv2.imshow('Threshold', threshold)
  
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>This is the original picture:
  <a href="https://i.stack.imgur.com/fYu0I.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fYu0I.jpg" alt="enter image description here"></a></p>