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

2024-05-13 02:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何计算图像中颜色的百分比,下面的图像代表100%: 100% 当水位下降时,已经是这样了: decreases 我想正确地学习如何获得当前条的百分比,我尝试使用Matplotlib库,但是没有得到预期的结果,有人能帮我吗?我不需要准备什么,有人教我。。。你知道吗


Tags: 图像matplotlib颜色代表百分比水位下降时
3条回答

一个命题:

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 %

我想你应该通过看图片来计算进度 我不确定是否有一个特定的库,但这是我的简单方法

你可以比较图片,直到他们是相似的列,然后可以计算出%的任务完成,让我演示。。你知道吗

!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

你在找Pillow库。有两种方法可以测量颜色、色调、饱和度、亮度(HSL)和红、蓝、绿(RGB)。图书馆里有两种功能。你知道吗

相关问题 更多 >