I wrote a short script to count the pixel values in an image:
import os
import sys
import cv2
import numpy as np
imn = (sys.argv[1])
a = cv2.imread(imn, 0)
b = cv2.imread(imn, 1)
c = cv2.GaussianBlur(cv2.imread(imn, 0), (7,7), 2)
def NC(img):
y = img.reshape(1, -1)
numA = (y < 127.5).sum()
numB = (y > 127.5).sum()
return ({'less': numA, 'greater': numB})
aa = NC(a)
bb = NC(b)
cc = NC(c)
print "File: {}".format(imn.split('/')[-1])
print "Image: {} - Set: {}".format('A', aa)
print "Image: {} - Set: {}".format('B', bb)
print "Image: {} - Set: {}".format('C', cc)
And it works perfectly:
File: ObamaBidenSituationRoom.jpg
Image: A - Set: {'greater': 480558, 'less': 611282}
Image: B - Set: {'greater': 1441948, 'less': 1833572}
Image: C - Set: {'greater': 471559, 'less': 620281}
But when I tried to expand it:
def NC(img):
y = img.reshape(1, -1)
numA = (00.99 < y < 85.00).sum()
numB = (85.00 < y < 170.0).sum()
numC = (170.0 < y < 256.0).sum()
return ({'low': numA, 'middle': numB, 'high': numC})
It gave me an error:
Traceback (most recent call last):
File "Bins--02.py", line 25, in <module>
aa = NC(a)
File "Bins--02.py", line 17, in NC
numA = (00.99 < y < 85.00).sum()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I got this image a while ago, but that was with a matplotlib library that I ended up not using. Why is it coming up here? Am I bounding that greater-than/less-than signs wrong? I tried to fix it
numA = (00.99 < y).sum() and (y < 85.00).sum()
But that just gave me random super high values.
UPDATE - Oct20
So, I changed it:
def NC(img):
x = img.reshape(1, -1)
numA = x[np.where((00.99 < x) & (x < 85.00))].sum()
numB = x[np.where((85.00 < x) & (x < 170.0))].sum()
numC = x[np.where((170.0 < x) & (x < 256.0))].sum()
numD = x.shape
return ({'total': numD, 'low': numA, 'middle': numB, 'high': numC})
And now it works, but there's a problem: the pixel counts don't match up.
Image: lenna.png
Image: A Set:{'high': 8367459, 'middle': 20278460, 'total': (1, 262144), 'low': 3455619}
Image: B Set:{'high': 45250935, 'middle': 43098232, 'total': (1, 786432), 'low': 11609051}
Image: C Set:{'high': 8216989, 'middle': 20633144, 'total': (1, 262144), 'low': 3531090}
The measurements are pixels, there can't be more than the total. Where am I getting 2 million from?
For example, I ran it on a 100x100 image of a blue circle:
Image: lightblue.png
Image: A Set:{'high': 0, 'middle': 1035999, 'total': (1, 10000), 'low': 0}
Image: B Set:{'high': 1758789, 'middle': 1212681, 'total': (1, 30000), 'low': 417612}
Image: C Set:{'high': 0, 'middle': 1014135, 'total': (1, 10000), 'low': 31801}
and it's completely wrong.
Edit Two
I just ran it on a test array:
i = np.array([[1, 1, 1, 1, 1, 1, 1, 1], [3, 3, 3, 3, 3, 3, 3, 3], [200, 200, 200, 200, 200, 200, 200, 200]])
def NC(img):
x = img.reshape(1, -1)
numA = x[np.where((00.99 < x) & (x < 85.00))].sum()
numB = x[np.where((85.00 < x) & (x < 170.0))].sum()
numC = x[np.where((170.0 < x) & (x < 256.0))].sum()
numD = (img.shape[0] * img.shape[1])
return ({'total': numD, 'low': numA, 'middle': numB, 'high': numC})
aa = NC(i)
bb = NC(i)
cc = NC(i)
print "Image: {} Set:{}".format('A', aa)
print "Image: {} Set:{}".format('B', bb)
print "Image: {} Set:{}".format('C', cc)
And it's entirely broken:
Image: A Set:{'high': 1600, 'middle': 0, 'total': 24, 'low': 32}
Image: B Set:{'high': 1600, 'middle': 0, 'total': 24, 'low': 32}
Image: C Set:{'high': 1600, 'middle': 0, 'total': 24, 'low': 32}
Why is it doing this?