#pragma once
#include "IPDef.h"
#include "ISP.h"
//Gray Histogram
int main(void)
{
//load color image
Mat img_color = imread("histo.jpg");
Mat img_gray = Mat(img_color.rows, img_color.cols, CV_8UC1);
ISP isp;
isp.cvtColor(img_color.data, img_gray.data,
img_color.cols, img_color.rows,
ECvtColor::eCvtBGR2GRAY);
//histogram
const int histoSize = 256;
vector<int> vHisto(histoSize, 0);
isp.getHistogram(img_gray.data,
img_gray.cols,
img_gray.rows,
vHisto.data(), vHisto.size());
Mat img_draw = Mat(img_color.rows, img_color.cols + histoSize, CV_8UC3);
img_draw = 0;
uchar* pImgColor = img_color.data;
uchar* pImgDraw = img_draw.data;
for (int row = 0; row < img_color.rows; row++)
{
for (int col = 0; col < img_color.cols; col++)
{
int color_index = row * img_color.cols + col;
int draw_index = row * img_draw.cols + col;
color_index *= 3;
draw_index *= 3;
pImgDraw[draw_index + 0] = pImgColor[color_index + 0];
pImgDraw[draw_index + 1] = pImgColor[color_index + 1];
pImgDraw[draw_index + 2] = pImgColor[color_index + 2];
}
}
int maxval = *std::max_element(vHisto.begin(), vHisto.end());
for (int i = 0; i < histoSize; i++)
{
int moveTo = img_color.cols + i;
int lineTo = static_cast<int>((1.0f * vHisto[i] * img_draw.rows / maxval));
line(img_draw, Point(moveTo, img_draw.rows),
Point(moveTo, img_draw.rows - lineTo), Scalar(255, 255, 255));
}
//histogram equlization
int sum = 0;
vector<int> vAccHisto(histoSize, 0);
for (int i = 0; i < histoSize; i++)
{
sum += vHisto[i];
vAccHisto[i] = sum;
}
for (int i = 0; i < histoSize-1; i++)
{
int X = img_color.cols + i;
int YmoveTo = static_cast<int>((1.0f * vAccHisto[i] * img_draw.rows / vAccHisto[histoSize - 1]));
int YlineTo = static_cast<int>((1.0f * vAccHisto[i+1] * img_draw.rows / vAccHisto[histoSize-1]));
line(img_draw, Point(X, img_draw.rows-YmoveTo),
Point(X, img_draw.rows-YlineTo), Scalar(0, 0, 255), 5);
}
imwrite("D:\\img_histo_gray.bmp", img_gray);
imwrite("D:\\img_histo.bmp", img_draw);
//normalized acc histogram
vector<float> norm_Acc_Histo(histoSize, 0);
int length = img_gray.rows * img_gray.cols;
for (int i = 0; i < histoSize; i++)
norm_Acc_Histo[i] = (1.0f * vAccHisto[i]) / length;
Mat img_gray_histoEqual = Mat(img_gray.rows, img_gray.cols, CV_8UC1);
uchar* pImgGrayHistoEqual = img_gray_histoEqual.data;
for (int row = 0; row < img_gray.rows; row++)
{
for (int col = 0; col < img_gray.cols; col++)
{
int index = row * img_gray.cols + col;
pImgGrayHistoEqual[index] = norm_Acc_Histo[img_gray.data[index]] * 255;
}
}
//histogram
vector<int> vHistoEqul(histoSize, 0);
isp.getHistogram(img_gray_histoEqual.data,
img_gray_histoEqual.cols,
img_gray_histoEqual.rows,
vHistoEqul.data(), vHistoEqul.size());
Mat img_draw_equal = Mat(img_color.rows, img_color.cols + histoSize, CV_8UC3);
img_draw_equal = 0;
uchar* pImgDraw_Equal = img_draw_equal.data;
for (int row = 0; row < img_color.rows; row++)
{
for (int col = 0; col < img_color.cols; col++)
{
int color_index = row * img_color.cols + col;
int draw_index = row * img_draw.cols + col;
color_index *= 3;
draw_index *= 3;
pImgDraw_Equal[draw_index + 0] = pImgColor[color_index + 0];
pImgDraw_Equal[draw_index + 1] = pImgColor[color_index + 1];
pImgDraw_Equal[draw_index + 2] = pImgColor[color_index + 2];
}
}
maxval = *std::max_element(vHistoEqul.begin(), vHistoEqul.end());
for (int i = 0; i < histoSize; i++)
{
int moveTo = img_color.cols + i;
int lineTo = static_cast<int>((1.0f * vHistoEqul[i] * img_draw.rows / maxval));
line(img_draw_equal, Point(moveTo, img_draw.rows),
Point(moveTo, img_draw.rows - lineTo), Scalar(255, 255, 255));
}
sum = 0;
vector<int> vAccHistoEqual(histoSize, 0);
for (int i = 0; i < histoSize; i++)
{
sum += vHistoEqul[i];
vAccHistoEqual[i] = sum;
}
for (int i = 0; i < histoSize - 1; i++)
{
int X = img_color.cols + i;
int YmoveTo = static_cast<int>((1.0f * vAccHistoEqual[i] * img_draw_equal.rows / vAccHistoEqual[histoSize - 1]));
int YlineTo = static_cast<int>((1.0f * vAccHistoEqual[i + 1] * img_draw_equal.rows / vAccHistoEqual[histoSize - 1]));
line(img_draw_equal, Point(X, img_draw_equal.rows-YmoveTo),
Point(X, img_draw_equal.rows-YlineTo), Scalar(0, 0, 255), 5);
}
imwrite("D:\\img_histo_equal_gray.bmp", img_gray_histoEqual);
imwrite("D:\\img_histo_equal.bmp", img_draw_equal);
cin.get();
}