Ask Your Question

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

asked 2019-01-11 02:32:23 -0500

bubu_ka gravatar image

I am computing the color distribution of image as mentioned in the paper:

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 flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2019-01-11 04:13:38 -0500

kbarni gravatar image

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.


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


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])
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-01-11 02:32:23 -0500

Seen: 1,570 times

Last updated: Jan 11 '19