Created
January 22, 2018 14:57
-
-
Save ChristianWitts/a2c18a658572823b35ca2f6047d40de3 to your computer and use it in GitHub Desktop.
Count the number of pixels in a certain RGB range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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