# python: how to compute the gray level histogram features as mentioned in the paper, and

Hi I am extracting the grey level features of image mentioned in this paper (part 4.5 low level features).

"We describe two low level features that are particu- larly important for photo quality assessment – contrast and brightness. Professional photos usually have higher contrast than snapshots. We measure the contrast of an image as fol- lows. First, we compute the gray level histogram Hr , Hg , and Hb for each of the red, green, and blue channels, re- spectively. Then, we compute the combined histogram H, where..." I can't clearly understand the above part in the paper. As my understanding about grey level features, take the above image as example. Use the opencv convert the color image to grey image. as the following code:

image = cv2.imread('lenna.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) and then using the following code to get the histogram with the following code:

  hist = cv2.calcHist([gray], , None, , [0,255])


I want to ask, and I don't know the meaning of gray level histogram for each channel.

1. How to we compute the gray level histogram Hr , Hg , and Hb for each of the red, green, and blue channels?

2. How to compute the the contrast quality, qct mentioned in the paper?

(1) how to we compute the gray level histogram Hr , Hg , and Hb for each of the red, green, and blue channels.

The gray level histogram for each of the channels is simply the gray level histogram of the Red channel, the Green channel and the Blue channel separately. Instead of converting the image from RGB to grayscale, try to extract its RGB components.

For example, in GNU Octave, a colour image is represented as a $$M \times N \times D$$ matrix, where $$M$$ is the height of the image, $$N$$ is the width of the image and $$D$$ is the "number of channels".

In a colour RGB image, the number of channels is 3.

This can be viewed as 3 images packed together in one "3D" matrix. To work on the Red channel, all that you have to do is use slicing to obtain just one of those matrices.

In Octave, this would be done via:

image = imread("someimage.png")
red_channel = image(:,:,1)


Similar thinking applies to other languages / computing platforms.

2) how to compute the the contrast quality, qct mentioned in the paper.

According to the paper, the $$q_{ct}$$ is the width of the 98% "part" of the combined histogram.

So:

2. Get the histogram of the Red Channel (call it $$H_r$$)
3. Get the histogram of the Green Channel (call it $$H_g$$)
4. Get the histogram of the Blue Channel (call it $$H_b$$)
5. Produce the combined histogram as $$H = H_r + H_b + H_g$$
6. Get the 98% of its mass:
1. Sort $$H$$ in reverse order of values (largest count comes first)
2. Get the cumulative sum of the histogram values until you have covered the 98% of the total sum.
3. Find the minimum and maximum values of the indices of the values that contribute to the sum
4. Calculate the $$q_{ct}$$ as the difference between the maximum and minimum of the indices.

Hope this helps.

• hi @A_A, I have solved based on your answers. – tktktk0711 Jan 9 '19 at 2:00
• @tktktk0711 Glad to hear you found the post helpful. If you consider your question answered by this post then you can accept it by clicking on the tick mark at the left of the answer box. This will stop the question from being circulated around the board as "unanswered". All the best with your project. – A_A Jan 9 '19 at 9:16
• thanks I will accept it. If possible, could you help me answer another question:dsp.stackexchange.com/questions/54687/…. – tktktk0711 Jan 9 '19 at 9:31