简体   繁体   English

Python中图像像素强度与色彩度量

[英]Image Pixel Intensity and Measure of Colourfulness in Python

I want to measure the average pixel intensity and measure of colourfulness of a image. 我想测量平均像素强度和测量图像的色彩。 For this i'm following this approach (kindly let me know if there's any alternative approach for the same): 为此我正在遵循这种方法(请告诉我是否有相同的替代方法):

a) Calculate Average Pixel Intensity: 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) To measure colourfulness: b)测量颜色:

  • Dividing color space into 64 cubic blocks with four equal partitions along each dimension 将颜色空间划分为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]) 
  • Calculate Euclidean distances between the geometric centers C i of each cube i Not able to compute (say d(a,b)=rgb(Ca)-rgb(Cb)) 计算每个立方体的几何中心C i之间的欧几里德距离i Not able to compute (say d(a,b)=rgb(Ca)-rgb(Cb))

  • Distribution D1 is generated as the color distribution of a hypothetical image such that for each of 64 sample points, the frequency is 1/64 - 分布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)

  • Distribution D2 is computed from the given image by finding the frequency of occurrence of color within each of the 64 cubes How can i do this? 通过查找64个立方体中每个立方体中颜色的出现频率,从给定图像计算分布D2 How can i do this?

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

Is this the right way to do it? 这是正确的方法吗? Any supporting documents to achieve this? 任何支持文件来实现这一目标? Any help with the code is appreciated. 任何有关代码的帮助表示赞赏。 Thanks. 谢谢。

You will need pyemd library. 你需要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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 图像上像素(X,Y)的颜色强度[OpenCV / Python] - Color Intensity of Pixel(X,Y) on Image [OpenCV / Python] 获取 python opencv 中强度为 255 的二值图像的像素位置 - Get pixel location of binary image with intensity 255 in python opencv 在python中将传感器强度值转换为像素强度 - Transform sensor intensity values to pixel intensity in python 如何计算python中同一图像内两个不同区域的平均像素强度差? - How to calculate the average pixel intensity difference for two different regions within the same image in python? matplotlib imshow()和像素强度 - matplotlib imshow() and pixel intensity 如何使用python更改图像中的强度阈值 - How to change intensity threshold in an image using python python 中的图像到像素阵列 - Image to pixel array in python 如何在python中逐像素生成图像? - How to generate an image in python pixel by pixel? 仅为图像的特定点计算相邻像素的平均像素强度并存储在 n 维数组中 - Calculating the average pixel intensity for neighboring pixels only for specific points of an image and storing inside a n-dimensional array 快速计算像素强度(颜色值)的方法,而无需遍历图像中的所有像素 - Faster way to calculate pixel intensity(color value) without iterating through all the pixels in image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM