![](/img/trans.png)
[英]python: getting value from a dictionary and inserting it into another dictionary as key
[英]getting key and value from a dictionary
我正在研究图像处理。 在那打印RGB值。 我将该图像转换为灰度图像。所以它返回黑色和像这样的值(左 = {0: 58, 255: 182})。这里 0 代表黑色,255 代表白色。我需要得到那些黑色和分别检查白色值并检查白色或黑色是否具有更高的值。
unique, counts = np.unique(cropped_right, return_counts=True)
mapColorCounts = dict(zip(unique, counts))
print("right=",mapColorCounts)
它的返回字典值。 因为我分别需要那个键和值
回报:
{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 值。 为了可视化, yourDict = {key: value, key: value}
像这样存储yourDict = {key: value, key: value}
并且为了返回键或值,您只需在 dict 上运行相应的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.