我想知道如何计算图像中颜色的百分比

时间:2018-12-08 06:32:54

标签: python python-3.x matplotlib

我想知道如何计算图像中颜色的百分比,下图代表100%: 100% 当水平降低时已经: decreases 我想正确地学习如何获取条形图当前所占的百分比,我尝试使用Matplotlib库,但无法获得预期的结果,有人可以帮我吗?我不需要准备好东西,有人来教我...

3 个答案:

答案 0 :(得分:1)

我认为您想通过查看图像来计算进度 我不确定是否有针对此特定内容的库,但这是我的简单方法,

您可以比较图像直到它们相似的列,然后可以计算完成的百分比任务,让我演示一下。

!wget https://i.stack.imgur.com/jnxX3.png
a = plt.imread( './jnxX3.png')
plt.imshow( a )

image with 100% progress

这将在变量a中加载具有100%完成度的图像

c =a 
c = c[: , 0:c.shape[1] - 50]
aa = np.zeros( dtype= float , shape=( 11,50,  3 ))
c = np.append( c, aa , axis= 1 )
plt.imshow( c)
plt.imshow( c )

制作了样本不完整的图像,您应该提供该图像 with 47% progress

def status( complete_img , part_image): 
    """inputs must be numpy arrays """ 

    complete_img = complete_img[:, 1: ] # as the first pixel column doesn't belong to % completion 
    part_image = part_image[:, 1:]

    counter = 0
    while(counter < part_image.shape[1] and counter < complete_img.shape[1]):         
        if (complete_img[:, counter ] == part_image[:,counter]).all():
            counter += 1 
        else :
            break
    perc = 100*( float(counter) / complete_img.shape[1])
    return 
status( a ,c ) # this will return % columns similar in the two images

答案 1 :(得分:0)

一个命题:

import numpy as np
from PIL import Image
from urllib.request import urlopen

full =  np.asarray(Image.open(urlopen("https://i.stack.imgur.com/jnxX3.png")))
probe =  np.asarray(Image.open(urlopen("https://i.stack.imgur.com/vx5zt.png")))

# crop the images to the same shape 
# (this step should be avoided, best compare equal shaped arrays)
full = full[:,1:probe.shape[1]+1,:]

def get_percentage(full, probe, threshold):
    def profile_red(im):
        pr = im[:,:,0] - im[:,:,1]
        return pr[pr.shape[0]//2]

    def zero(arr):
        z = np.argwhere(np.abs(np.diff(np.sign(arr))).astype(bool))
        if len(z):
            return z[0,0]
        else:
            return len(arr)

    full_red = profile_red(full)
    probe_red = profile_red(probe)
    mask = full_red > threshold
    diff = full_red[mask] - probe_red[mask]

    x0 = zero(diff - threshold) 
    percentage = x0 / diff.size * 100
    err = 2./diff.size * 100
    return percentage, err


print("{:.1f} p\m {:.1f} %".format(*get_percentage(full, probe, 75.0)))

结果:

94.6 p\m 2.2 %

答案 2 :(得分:-1)

您正在寻找Pillow库。有两种测量颜色的方法,色相,饱和度,亮度(HSL)和红色,蓝色,绿色(RGB)。库中有两种功能可以实现。