Opencv— — Bias and Gain

// define head function
#ifndef PS_ALGORITHM_H_INCLUDED
#define PS_ALGORITHM_H_INCLUDED

#include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"
#include "cxcore.hpp"
#include "math.h"

using namespace std;
using namespace cv;

void Show_Image(Mat&, const string &);

#endif // PS_ALGORITHM_H_INCLUDED

/* Adjust bias and gain. */

#include "PS_Algorithm.h"

float Bias(float a, float b);
float Gain(float a, float b);

int main()
{
    string  Image_name("4.jpg");
    Mat Img=imread(Image_name.c_str());
    Mat Img_out(Img.size(), CV_32FC3);

    float gain_val = 0.75;   // 0-1
    float bias_val = 0.25;   // 0-1

    int width=Img.cols;
    int height=Img.rows;

    float val;

    for (int y=0; y<height; y++)
    {
        for (int x=0; x<width; x++)
        {

            for (int k=0; k<3; k++)
            {

                val=(float)Img.at<Vec3b>(y, x)[k]/255;

                val=Gain(val, gain_val);

                Img_out.at<Vec3f>(y, x)[k]=Bias(val, bias_val);

            }

        }

    }


    Show_Image(Img_out, "New_img");

    cout<<"All is well."<<endl;

    waitKey();
}


float Bias(float a, float b)
{
    float y;

    a=pow(a, log(b)/log(0.5));

    y=a/((1.0f/b-2)*(1.0f-a)+1);

    return y;

}

float Gain(float a, float b)
{
    /* float p = log(1.0 - b) / log(0.5); if (a < .001) return 0.0f; else if (a > .999) return 1.0f; if (a < 0.5) return pow(2 * a, p) / 2; else return 1.0f - pow(2 * (1. - a), p) / 2; */

        float c = (1.0f/b-2.0f) * (1.0f-2.0f*a);
        if (a < 0.5)
            return a/(c+1.0f);
        else
            return (c-a)/(c-1.0f);


}


// define the show image
#include "PS_Algorithm.h"
#include <iostream>
#include <string>

using namespace std;
using namespace cv;

void Show_Image(Mat& Image, const string& str)
{
    namedWindow(str.c_str(),CV_WINDOW_AUTOSIZE);
    imshow(str.c_str(), Image);

}

图像效果可以参考:

http://blog.csdn.net/matrix_space/article/details/46790171

你可能感兴趣的:(Opencv— — Bias and Gain)