我正致力于图像处理。在那里打印RGB值。我将这些图像转换为灰度图像。所以它返回黑色,而像这样的值(左= {0:58,255:182})。这里0表示黑色,255表示白色。我需要得到那些黑色和分别使用白色值并检查白色或黑色是否具有更高的值。
unique, counts = np.unique(cropped_right, return_counts=True)
mapColorCounts = dict(zip(unique, counts))
print("right=",mapColorCounts)
它返回的dict值。因为我需要单独的键和值
回报:
{0: 218, 255: 426}
{255: 196}
{0: 51, 255: 189}
{0: 406, 255: 314}
{0: 47, 255: 193}
{0: 28, 255: 278}
{0: 286, 255: 632}
{255: 306}
{0: 15, 255: 85}
{0: 91, 255: 229}
我需要找到返回多少组键值。因为有时它会返回2个值和1个值
如果您将值作为字典,则可以使用mapColorCounts.values()
返回255和0值。为了可视化,dicts存储像yourDict = {key: value, key: value}
,为了返回键或值,你只需在字典上运行相应的keys()
或values()
方法
从这里,您可以创建一个简单的方法来测试值:
def whichColorGreater(inputDict):
values = inputDict.values()
for value in values:
if len(value) > 1:
if value[0] > value[1]:
print("Black is greater")
else:
print("White is greater")
else:
print("White is greater")
假设cropped_right
是二进制的,我想你可以简化为:
has_more_white = (np.count_nonzero(cropped_right) / cropped_right.size) > 0.5
如果您需要元素数量:
nb_white_pixels = np.count_nonzero(cropped_right)
nb_black_pixels = cropped_right.size - nb_white_pixels