【问题标题】:Image Pixel Intensity and Measure of Colourfulness in PythonPython中的图像像素强度和色彩测量
【发布时间】:2017-06-27 20:48:33
【问题描述】:

我想测量图像的平均像素强度和色彩度。为此,我正在遵循这种方法(如果有任何替代方法,请告诉我):

a) 计算平均像素强度:

im = Image.open('images-16.jpeg')
stat = ImageStat.Stat(im)
r,g,b = stat.mean
mean = sqrt(0.241* (r ** 2) + 0.691* (g ** 2) + 0.068* (b ** 2))
print(mean)

b) 测量色彩:

  • 将颜色空间划分为 64 个立方块,每个维度有四个相等的分区

    w,h=im.size    
    bw,bh = 8, 8 #block size
    img = np.array(im)
    sz = img.itemsize
    shape = (h-bh+1, w-bw+1, bh, bw)
    strides = (w*sz, sz, w*sz, sz) 
    blocks = np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)
    print (blocks[1,1])
    
  • 计算每个立方体的几何中心 Ci 之间的欧几里得距离 i Not able to compute (say d(a,b)=rgb(Ca)-rgb(Cb))

  • Distribution D1 生成为假设图像的颜色分布,使得对于 64 个样本点中的每一个,频率为 1/64 -

    pixels = im.load() all_pixels = [] for x in range(218): #put your block width size for y in range(218): #your block heigh size cpixel = pixels[x, y] all_pixels.append(cpixel)

  • 分布 D2 是通过在 64 个立方体 How can i do this?

  • 计算Earth Mover's Distance: (D1,D2,d(a,b)) - d(a,b) 在上面计算

这是正确的做法吗?任何支持文件来实现这一目标?感谢您对代码的任何帮助。谢谢。

【问题讨论】:

  • 首先,我会将 r,g,b 组合成每个像素的亮度之前计算平均值。使用**2.2**(1/2.2) 的正确伽玛值代替**2sqrt。请务必先将这些像素值除以 255。
  • @MarkRansom 好的,肯定会尝试的......并用于测量色彩?
  • 对不起,如果我有关于色彩的建议,我会留下一个正确的答案。
  • @MarkRansom np,提建议

标签: python image image-processing euclidean-distance


【解决方案1】:

您将需要 pyemd 库。

from pyemd import emd
import numpy as np
from PIL import Image
import skimage.color

im = Image.open("t4.jpg")
pix = im.load()

h1 = [1.0/64] * 64
h2 = [0.0] * 64
hist1 = np.array(h1)

w,h = im.size

for x in xrange(w):
    for y in xrange(h):
        cbin = pix[x,y][0]/64*16 + pix[x,y][1]/64*4 + pix[x,y][2]/64
        h2[cbin]+=1
hist2 = np.array(h2)/w/h

# compute center of cubes

c = np.zeros((64,3))
for i in xrange(64):
    b = (i%4) * 64 + 32
    g = (i%16/4) * 64 + 32
    r = (i/16) * 64 + 32
    c[i]=(r,g,b)

c_luv = skimage.color.rgb2luv(c.reshape(8,8,3)).reshape(64,3)

d = np.zeros((64,64))

for x in xrange(64):
    d[x,x]=0
    for y in xrange(x):
        dist = np.sqrt( np.square(c_luv[x,0]-c_luv[y,0]) + 
                   np.square(c_luv[x,1]-c_luv[y,1]) + 
                   np.square(c_luv[x,2]-c_luv[y,2]))
        d[x,y] = dist
        d[y,x] = dist


colorfullness = emd(hist1, hist2, d)

print colorfullness

【讨论】:

  • 这看起来不错,类似于我试图重现的实现,但是在尝试它时,彩色图像的色彩低于那个,所以可能有问题?
猜你喜欢
  • 2021-10-06
  • 2013-10-14
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
相关资源
最近更新 更多