0

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?

4

1 回答 1

1

你的方法有几个问题。

当你这样做

(y < 85.00).sum()

你实际上是在总结真相条件。因此,您最终会计算条件评估为 的位置True。您可以通过一个简单的示例轻松查看它:

In [6]: x = np.arange(10)

In [7]: x
Out[7]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [8]: x < 4
Out[8]: array([ True,  True,  True,  True, False, False, False, False, False, False], dtype=bool)

In [9]: (x < 4).sum()
Out[9]: 4

现在,如果您想获得满足条件的索引,您可以使用np.where

In [10]: np.where(x < 4)
Out[10]: (array([0, 1, 2, 3]),)

并将它们用于您的总和

In [11]: x[np.where(x < 4)].sum()
Out[11]: 6

另一个问题来自对范围使用紧凑表示法,该范围很容易解决,用&or将其分成两部分np.logical_and()

In [12]: x[np.where((2 < x) & (x < 6))].sum()
Out[12]: 12
于 2016-10-19T10:02:28.560 回答