image enhancement

void brightness_and_constrast_auto(const cv::Mat& src, cv::Mat& dst, float clip_hist_percent)
{
    CV_Assert(clip_hist_percent >= 0);
    CV_Assert(src.type()==CV_8UC1);

    int hist_size = 256;
    float alpha=0.0;
    float beta = 0.0;

    double min_gray = 0;
    double max_gray = 0;

    if(0 == clip_hist_percent)
    {
        cv::minMaxLoc(src, &min_gray, &max_gray);
    }
    else
    {
        cv::Mat hist; /// the grayscale histogram
        float rang[] = {0, 256};
        const float* hist_range = {rang};
        bool uniform = true;
        bool accumulate = false;
        cv::calcHist(&src, 1, 0, cv::Mat(), hist, 1, &hist_size, &hist_range, uniform, accumulate);
        std::vector<float> accumulator(hist_size);
        accumulator[0] = hist.at<float>(0);
        for(int i = 1; i < hist_size; i++)
        {
            accumulator[i] = accumulator[i-1] + hist.at<float>(i);
        }

        float max = accumulator.back();

        clip_hist_percent *= (max/100.0); /// make percent a absolute
        clip_hist_percent /= 2.0; /// left and right wings

        while(accumulator[min_gray] < clip_hist_percent)
        {
            min_gray++;
        }

        max_gray = hist_size - 1;

        while (accumulator[max_gray] >= (max - clip_hist_percent))
        {
            max_gray--;
        }
    }

    alpha = (hist_size - 1)/(float)(max_gray -min_gray);
    beta = -min_gray * alpha;

    src.convertTo(dst, -1, alpha, beta);

    return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值