# python: how to compute the color distribution of image in the paper with python and openCV

I am computing the color distribution of image as mentioned in the paper: https://arxiv.org/pdf/1202.2158.pdf.

3.1.2 Color Distribution

To avoid distraction from objects in the background, professional photographers tend to keep the background simple.
In [19], the authors use the color distribution of the background to measure this simplicity. We use a similar approach
to measure the simplicity of color distribution in the image. For a given image, we quantize each RGB channel into
8 values, creating a histogram Hrgb = {h0, h1, · · · , h511} of 512 bins, where hi indicates the number of pixels in
i-th bin. We define feature f4 to indicate the number of dominant colors as f4 =  512 1(hk ≥ c2 maxi hi) where k=0
c2 = 0.01 is the threshold parameter. We also calculate the size of the dominant bin relative to the image size as f5 = maxi hi .


I am no sure the meaning of we quantize each RGB channel into 8 values. How to quantize the RGB into 8 values. and Hrgb of 512 bins. Hrgb is the sum of R, G, B of histogram or not?

and I have read the image with opencv to get there channel data as following code.

image = cv2.imread(img)
B = image[:, :, 0]
G = image[:, :, 1]
R = image[:, :, 2]

edit retag close merge delete

Sort by » oldest newest most voted

The simplest solution is to quantize first the image into 8 levels simply by dividing it by 32 (integer division). Then, every channel will range between 0..7.

Rq=R//32


To get the Hrgb,combine the three channels into one:

Himg=Rq+Gq*8+Bq*64


Himg will contain values between 0...511, corresponding to the 512 bins of Hrgb. So you only need to build the histogram for Himg to get Hrgb (using cv2.hist() or numpy.histogram())

Hrgb = cv2.calcHist([Himg],[0],None,[512],[0,512])

more

Official site

GitHub

Wiki

Documentation