Estimate Brightness of an image Opencv

前端 未结 4 640
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 03:08

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

4条回答
  •  温柔的废话
    2020-12-14 03:17

    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);
    }
    

提交回复
热议问题