#include <stdio.h>
#include <cstdlib>
#include <string>
#include <opencv2/highgui.hpp>
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <fstream>
#define LIGHT_MAX 256
int main(int argc, char* argv[])
{
int w = 0, h = 0;
std::string fileName = argv[1];
cv::Mat src = cv::imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat dst ;
dst.create(src.rows, src.cols, CV_8UC1);
int MN = src.rows * src.cols;
int hgmH[LIGHT_MAX];
float hgmT[LIGHT_MAX];
memset(hgmH, 0, sizeof(hgmH));
memset(hgmT, 0, sizeof(hgmT));
printf("%d,%d,%d\n", src.rows, src.cols, src.channels());
imwrite("src.bmp", src);
for(int i = 0; i < src.rows; i++)
{
unsigned char *psrc = src.ptr<unsigned char>(i);
for(int j = 0; j < src.cols; j++, psrc++)
{
++hgmH[*psrc];
}
}
int gmin = 0;
for(;gmin < LIGHT_MAX; gmin++ )
{
if(hgmH[gmin])
break;
}
for(int i = 1; i < LIGHT_MAX; i++ )
{
printf("%6u ", hgmH[i]);
}
printf("\n**************************\n");
for(int i = 1; i < LIGHT_MAX; i++ )
{
hgmH[i] += hgmH[i-1];
printf("%6u ", hgmH[i]);
}
printf("\n");
printf("gmin %d\n", gmin);
for(int i = gmin; i <= gmax; i++ )
{
hgmT[i] = static_cast<float>(roundf(1.f*(hgmH[i]-hgmH[gmin])*(LIGHT_MAX-1)/(MN-hgmH[gmin])));
printf("%f ", hgmT[i]);
}
printf("\n");
for(int i = 0; i < src.rows; i++)
{
unsigned char *psrc = src.ptr<unsigned char>(i);
unsigned char *pdst = dst.ptr<unsigned char>(i);
for(int j = 0; j < src.cols; j++, pdst++, psrc++)
{
// printf("%3u ", *psrc);
*pdst = hgmT[*psrc];
}
// printf("\n");
}
imwrite("dst.bmp", dst);
return 0;
}