I have been trying to obtain the image brightness in Opencv, and so far I have used calcHist and considered the average of the histogram values. However, I feel this is not accurate, as it does not actually determine the brightness of an image. I performed calcHist over a gray scale version of the image, and tried to differentiate between the avergae values obtained from bright images over that of moderate ones. I have not been successful so far. Could you please help me with a method or algorithm, that can be realised through OpenCv, to estimate brightness of an image? Thanks in advance.
-
3What exactly do you mean by brightness? Can you post examples of the bright and moderate images you're working with? Ideally alongside their histograms?– Brandon JacksonJan 9, 2013 at 23:07
-
1stackoverflow.com/questions/4876315/… Possibly this could help– 2vision2Jan 10, 2013 at 12:30
-
Thanks for your help and reply. I have to try it with either HSV as suggested or the YUV given by the link. I am gonna try them now. sorry for the late response.– Lakshmi NarayananJan 10, 2013 at 16:05
-
@Brandon : By bright images, I mean the images exposed to more white light, in comparison to images that don't strike as brightly white, but with better contrast. I will shortly upload the examples as you requested.– Lakshmi NarayananJan 10, 2013 at 16:07
-
@LakshmiNarayanan If possible check and lemme know.– 2vision2Jan 11, 2013 at 12:23
4 Answers
I suppose, that HSV color model will be usefull in your problem, where channel V is Value:
"Value is the brightness of the color and varies with color saturation. It ranges from 0 to 100%. When the value is ’0′ the color space will be totally black. With the increase in the value, the color space brightness up and shows various colors."
So use OpenCV method cvCvtColor(const CvArr* src, CvArr* dst, int code), that converts an image from one color space to another. In your case code = CV_BGR2HSV.Than calculate histogram of third channel V.
-
7Note that HSV assigns the same value to e.g. white and blue pixels, although white pixels are clearly brighter than blue pixels.– NikiJan 10, 2013 at 8:17
-
1@Ann Orlova stackoverflow.com/questions/4876315/… your thoughts on this?– 2vision2Jan 10, 2013 at 12:31
-
1Thanks for your help and reply. I have to try it with either HSV as suggested or the YUV given by the link. I am gonna try them now. sorry for the late response. Jan 10, 2013 at 16:06
-
CIE-LAB color space may also work github.com/imneonizer/How-to-find-if-an-image-is-bright-or-dark Jan 11, 2021 at 5:27
I was about to ask the same, but then found out, that similar question gave no satisfactory answers. All answers I've found on SO deal with human observation of a single pixel RGB vs HSV.
From my observations, the subjective brightness of an image also depends strongly on the pattern. A star in a dark sky may look more bright than a cloudy sky by day, while the average pixel value of the first image will be much smaller.
The images I use are grey-scale cell-images produced by a microscope. The forms vary considerably. Sometimes they are small bright dots on very black background, sometimes less bright bigger areas on not so dark background.
My approach is:
- Find histogram maximum (HMax) using threshold for removing hot pixels.
- Calculate mean values of all pixel between HMax * 2/3 and HMax
The ratio 2/3 could be also increased to 3/4 (which reduces the range of pixels considered as bright).
The approach works quite well, as different cell-patterns with same titration produce similar brightness.
P.S.: What I actually wanted to ask is, whether there is a similar function for such a calculation in OpenCV or SimpleCV. Many thanks for any comments!
-
2
I prefer Valentin's answer, but for 'yet another' way of determining average-per-pixel brightness, you can use numpy
and a geometric mean instead of arithmetic. To me it has better results.
from numpy.linalg import norm
def brightness(img):
if len(img.shape) == 3:
# Colored RGB or BGR (*Do Not* use HSV images with this function)
# create brightness with euclidean norm
return np.average(norm(img, axis=2)) / np.sqrt(3)
else:
# Grayscale
return np.average(img)
A bit of OpenCV C++ source code for a trivial check to differentiate between light and dark images. This is inspired by the answer above provided years ago by @ann-orlova:
const int darkness_threshold = 128; // you need to determine what threshold to use
cv::Mat mat = get_image_from_device();
cv::Mat hsv;
cv::cvtColor(mat, hsv, CV_BGR2HSV);
const auto result = cv::mean(hsv);
// cv::mean() will return 3 numbers, one for each channel:
// 0=hue
// 1=saturation
// 2=value (brightness)
if (result[2] < darkness_threshold)
{
process_dark_image(mat);
}
else
{
process_light_image(mat);
}