Skip to content

Instantly share code, notes, and snippets.

@ChristianWitts
Created January 22, 2018 14:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianWitts/a2c18a658572823b35ca2f6047d40de3 to your computer and use it in GitHub Desktop.
Save ChristianWitts/a2c18a658572823b35ca2f6047d40de3 to your computer and use it in GitHub Desktop.
Count the number of pixels in a certain RGB range
#!/usr/bin/env python
# Install OpenCV and numpy
# $ pip install opencv-python numpy
import cv2
import numpy as np
img = cv2.imread("bgr.png")
# minimum value of brown pixel in BGR order -> burleywood
BROWN_MIN = np.array([135, 184, 222], np.uint8)
# maximum value of brown pixel in BGR order -> brown
BROWN_MAX = np.array([42, 42, 65], np.uint8)
dst = cv2.inRange(img, BROWN_MIN, BROWN_MAX)
no_brown = cv2.countNonZero(dst)
print('The number of brown pixels is: ' + str(no_brown))
cv2.namedWindow("opencv")
cv2.imshow("opencv",img)
cv2.waitKey(0)
# vim: ft=python ts=4 sw=4 expandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment