Home > Back-end >  C/c RGB color image histogram equilibrium -> HSI HSI after histogram equalization of luminance co
C/c RGB color image histogram equilibrium -> HSI HSI after histogram equalization of luminance co

Time:09-19

 
#include
#include
# define _USE_MATH_DEFINES
#include
#include

using namespace std;


Int main ()
{

The FILE * stream;
Fopen_s (& amp; The stream, "C: \ \ Users \ \ zero \ \ Desktop \ \ test \ \ color1_1_1 BMP", "rb");
If (stream==NULL)
{
Cout & lt; <"File does not exist" & lt; return 0;
}

Int sizeFileHeader=sizeof (BITMAPFILEHEADER);
Int sizeInfoHeader=sizeof (BITMAPINFOHEADER);

BITMAPFILEHEADER * BITMAPFILEHEADER=new BITMAPFILEHEADER [sizeFileHeader + 1];

BITMAPINFOHEADER * BITMAPINFOHEADER=new BITMAPINFOHEADER [sizeInfoHeader + 1];

Memset (bitmapFileHeader, 0, sizeFileHeader + 1);
Memset (bitmapInfoHeader, 0, sizeInfoHeader + 1);
Fread (bitmapFileHeader, sizeof (char), sizeFileHeader, stream).
Fseek (stream, sizeFileHeader, 0);
Fread (bitmapInfoHeader, sizeof (char), sizeInfoHeader, stream).
Int srcImageLineByteCount=(((bitmapInfoHeader - & gt; BiWidth * 24) + 31)/32) * 4;
Fseek (stream, sizeFileHeader + sizeInfoHeader, 0);

Int width=bitmapInfoHeader - & gt; BiWidth;
Int height=bitmapInfoHeader - & gt; BiHeight;
//read image data
Int count=srcImageLineByteCount bitmapInfoHeader - & gt; BiWidth * (bitmapInfoHeader - & gt; BiBitCount/8);
BYTE * tempData=https://bbs.csdn.net/topics/new BYTE (count);
BYTE BYTE oldImageData=https://bbs.csdn.net/topics/new * * * * * [bitmapInfoHeader -> biHeight];
for (int i=0; I & lt; BitmapInfoHeader - & gt; BiHeight; I++)
{
OldImageData [I]=new BYTE * [bitmapInfoHeader - & gt; biWidth];
For (int j=0; J & lt; BitmapInfoHeader - & gt; BiWidth; J++)
{
OldImageData [I] [j]=new BYTE [3].
For (int k=0; K & lt; 3; K++)
{
Fread (& amp; OldImageData [I] [j] [k], sizeof (BYTE), 1, stream);
}
}

For (int m=0; M & lt; The count. M++)
{
Fread (& amp; TempData [m], sizeof (BYTE), 1, stream);
}
}


The fclose (stream);

//get data


//* * * * * * * * * * * * * * * * * * * * * * * RGB - & gt; HSI * * * * * * * * * * * * * * * * * * * * * * * * *
HSIImageData_S=new double double * * * [height];
Int * * HSIImageData_I=new int * [height].
HSIImageData_H=new double double * * * [height];
for (int i=0; I & lt; height; I++)
{
HSIImageData_S [I]=new double (width);
Memset (HSIImageData_S [I], 0, width);//initialized to 0

HSIImageData_H [I]=new double (width);
Memset (HSIImageData_H [I], 0, width);//initialized to 0

HSIImageData_I [I]=new int (width).
Memset (HSIImageData_I [I], 0, width);//initialized to 0
}

for (int i=0; I & lt; height; I++) {
For (int j=0; J & lt; Width; J++) {
Double S;
Double H;
Int I;

//is
//RGB normalized
Double B=(double) oldImageData [I] [j] [0]/255.0;
Double G=(double) oldImageData [I] [j] [1]/255.0;
Double R=(double) oldImageData [I] [j] [2]/255.0;

Double numerator=0.5 * ((R - G) + (R - B));//theta molecular
Double denominator=SQRT ((R - G) * (R - G) + (R - B) * (G - B));//is the denominator

//Hue Hue
//if the denominator is zero, that is, R=G=B
If (denominator==0) {
H=0;
}
The else {
Double theta=a cosine (numerator/denominator);
//a cosine: cos of inverse function, domain is: [1, 1], the range of [0, PI]

If (B & lt; H=G)=theta;
The else H=2 * M_PI - theta;

}

//brightness Intensity
I=(int) ((R + G + B)/3 * 255);

//Saturation Saturation
If (I==0) {
S=0;
}
The else {
S=1 - (3.0 x min (min) (R, G, B))/(R + G + B);
}

//will get the HSI conversion into array
//H
HSIImageData_H [I] [j]=H;
//S
HSIImageData_S [I] [j]=S;
//I
HSIImageData_I [I] [j]=I;
}
}

//* * * * * * * * * * * * * * * * * * * * * * * * the luminance component Intensity histogram equalization * * * * * * * * * * * * * * * * * * * * * * * *

Int gray_num [256]={0};//record each number under the gray levels of pixels
Double gray_prob [256]={0};//record grayscale distribution density
Double gray_dens_accum [256]={0};//record the cumulative density
Int gray_after [256]={0};//after the equalization of grey value

Int gray_sum=height * width;//total number of pixels x
//cout & lt;
//statistics each as the number of numerical
for (int i=0; I & lt; height; I++) {
For (int j=0; J & lt; Width; J++) {
Int value=https://bbs.csdn.net/topics/HSIImageData_I [I] [j];
Gray_num [value] + +;
}
}

//statistics each gray density
for (int i=0; I & lt; 256; I++) {
Gray_prob [I]=(double) gray_num [I]/gray_sum;
}

//statistics accumulated density
Gray_dens_accum [0]=gray_prob [0];
for (int i=1; I & lt; 256; I++) {
Gray_dens_accum [I]=gray_dens_accum [I - 1) + gray_prob [I];
}

//calculation of grey value after equalization
for (int i=0; I & lt; 256; I++) {
Gray_after [I]=(UCHAR) 255 * gray_dens_accum [I];
}


//based on gray level after the equalization is worth to a new bitmap data
for (int i=0; I & lt; height; I++) {
For (int j=0; J & lt; Width; J++) {
HSIImageData_I [I] [j]=gray_after [HSIImageData_I [I] [j]].

}
}

//HSI - & gt; RGB
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related