Skip to content

Instantly share code, notes, and snippets.

@hmurraydavis
Last active June 23, 2016 04:51
Show Gist options
  • Save hmurraydavis/b7d54b66471507292580cedf4d7a5325 to your computer and use it in GitHub Desktop.
Save hmurraydavis/b7d54b66471507292580cedf4d7a5325 to your computer and use it in GitHub Desktop.
Automated Error Analysis by Color
import cv2
import numpy as np
import pyscreenshot as ss
import matplotlib.pyplot as plt
im=ss.grab()
im = np.array( im.convert('RGB') )
numPixels = 1.0 * im.shape[0] * im.shape[1]
## Red pixels:
redThresh = cv2.inRange(im, np.array([0,0,200]), np.array([150,150,255]))
numRed = cv2.countNonZero(redThresh)
## White pixels:
whiteThresh = cv2.inRange(im, np.array([200,200,200]), np.array([255,255,255]))
numWhite = cv2.countNonZero(whiteThresh)
## Black pixels:
blackThresh = cv2.inRange(im, np.array([0,0,0]), np.array([50,50,50]))
numBlack = cv2.countNonZero(blackThresh)
## Print summary statistics:
print "Percent red is: ", 100*numRed/numPixels, "% of ", numPixels, " total pixels"
print "Total number of pixels: ", numPixels
print "Number red: ", numRed
print "Num White: ", numWhite
print "Num black: ", numBlack
## Make a bar chart:
fig, ax = plt.subplots()
red = ax.bar([1], [numRed/numPixels], 0.35, color='r', align='center')
white = ax.bar([2], [numWhite/numPixels], 0.35, color='0.9', align='center')
black = ax.bar([3], [numBlack/numPixels], 0.35, color='k', align='center')
ax.set_xticks([1,2,3])
ax.set_xticklabels(('Red', "White", "Black"))
plt.title("% Error By Color", fontsize=22)
plt.ylabel("% Color", fontsize=18)
plt.xlabel("Color", fontsize=15)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment