平台直方图均衡化

现在我也是一枚苦逼的研究生啦,然后现在的研究方向是图像增强,这篇博客算是我研究生写博客生涯的开篇吧。

网上有很多关于平台直方图均衡化的,我就不再赘述了,直接放代码:

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(){
    //视频读取
    VideoCapture cap; 
    cap.open("0.avi"); 
    while(1) 
    { 
        Mat frame; //定义Mat变量,用来存储每一帧 
        cap>>frame; //读取当前帧 
        imshow("frame",frame);
        Mat dest;

        //平台直方均衡化
        MatND  hist;
        const int histSize = 256;
        float range[] = { 0, 255 };
        const float *ranges[] = { range };
        const int channels = 0;
        cv::calcHist(&frame, 1, &channels, Mat(), hist, 1, &histSize, ranges);//计算图像直方图,hist为计算出来的直方图
        float total = frame.size().width*frame.size().height;

        float bins[histSize] = { 0 };
        float binsAcc[histSize] = { 0 };
        Mat lut(1, 256, CV_8U);
        vector<float> vectorBins;
        vector<float> maxBins;
        float sumBins = 0.0;
        int countMax = 0;
        float TValue = 0;

        // Find the mapping table
        for (int i = 0; i<histSize; i++)
        {
            float bin_val = hist.at<float>(i); // 第i灰度级上的数
            bins[i] = bin_val / total;

            if (bins[i] > 0)
            {
                vectorBins.push_back(bins[i]);
            }


        }

        // Calculate the Meadin value by 3 sapce
        for (int i = 1; i < vectorBins.size() - 1; i++)
        {
            if (vectorBins[i] < vectorBins[i - 1] && vectorBins[i - 1] < vectorBins[i + 1] || vectorBins[i] > vectorBins[i - 1] && vectorBins[i - 1]  > vectorBins[i + 1])
            {
                vectorBins[i] = vectorBins[i - 1];
            }
            else if (vectorBins[i] < vectorBins[i + 1] && vectorBins[i + 1] < vectorBins[i - 1] || vectorBins[i] > vectorBins[i + 1] && vectorBins[i + 1] > vectorBins[i - 1])
            {
                vectorBins[i] = vectorBins[i + 1];
            }
        }

        // Calculate the max peak value
        for (int i = 1; i < vectorBins.size() - 1; i++)
        {
            if (vectorBins[i] - vectorBins[i - 1] >= 0 && vectorBins[i + 1] - vectorBins[i] <= 0)
            {
                maxBins.push_back(vectorBins[i]);
                sumBins += vectorBins[i];
                countMax++;
            }
        }

    
       TValue = sumBins / countMax;
        //TValue = L.rows* L.cols*0.002;

        // Find the mapping table
        for (int i = 0; i<histSize; i++)
        {

            if (bins[i] > TValue)
            {
                bins[i] = TValue;
            }

            if (i>=1)
            {
                binsAcc[i] = binsAcc[i-1] + bins[i];//图像累积直方图
            }
            else
            {
                binsAcc[0] = bins[0];
            }
        }

        for (int i = 0; i < histSize; i++)
        {
            lut.at<uchar>(i) = static_cast<uchar>(cvFloor(binsAcc[i] * 255 / (binsAcc[255])));
        }
    
        LUT(frame, lut, dest);//查找表,根据直方图生成新的图像
        imshow("dset", dest);
        
        if(waitKey(30)>=0) break; //延时30ms 
    }
    

    //waitKey(0);
    return 0;
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值