Code:
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
using namespace std;
void filewritecwh(char fn[],int **aa,int row, int col, int gray);
void filereadcwh(char fn[],int **aa,int row, int col);
void fileheader(char fn[],int *a);
int **allot_int_mn(int row, int col);
int **histogram(int **image, int row, int col);
int **cumulative(int **image, int row, int col);
int **eqHistogram(int **image, int row, int col);
int **eqImage(int **image, int row, int col);
int main()
{
int **image, **newImage, temp[3], row, col ,gray;//newgray;
/* initial variables for program */
// int noPix[255];
// char file[50];
// cout << "Please enter the input Imagefile in ASCII PGM Format: ";
// cin >> file;
/* file header extraction */
//fileheader(file,temp);
fileheader("lena.pgm",temp);
col = temp[0];
row = temp[1];
gray = temp[2];
/* memory allocation */
image = allot_int_mn(row, col);
newImage = allot_int_mn(row, col);
/* file reading */
filereadcwh("lena.pgm",image,row,col);
newImage=histogram(image, row, col);
/* histogram function call */
filewritecwh("histogram.pgm",newImage,row,col,gray);
/* write new image to pgm */
newImage=cumulative(image, row, col);
/* cumulative function call */
filewritecwh("cumulative.pgm",newImage,row,col,gray);
/* write new image to pgm */
newImage=eqHistogram(image, row, col);
/* equalization hist function call */
filewritecwh("eqhistogram.pgm",newImage,row,col,gray);
/* write new image to pgm */
newImage=eqImage(image, row, col);
/* equalization image function call */
filewritecwh("eqimage.pgm",newImage,row,col,gray);
/* write new image to pgm */
return 0;
}
// This function determiones and displays the histogram of the imported picture
int **histogram(int **image, int row, int col)
{
int **newImage,i,j,pixel, noPix[255], max, rows; //black;
newImage = allot_int_mn(row, col);
for (j = 0; j < row; j++)
for (i = 0; i < col; i++)
newImage[i][j] = 255;
for (j = 0; j < 255; j++)
noPix[j]=0;
for (j = 0; j < row; j++) {
for (i = 0; i < col; i++) {
pixel = image[i][j];
noPix[pixel]+=1;
}
}
max=0;
for (j = 0; j < 255; j++)
if(noPix[j]>max) max = noPix[j];
for (j = 0; j < 255; j++)
noPix[j] = 255*noPix[j]/max;
for (j = 0; j < 255; j++) {
rows = 255 - noPix[j];
for (i = 0; i < rows; i++)
newImage[i][j] = 0;
}
return(newImage);
}
//This function calculates the cumulative distribution function (CDF) of the
//inported picture
int **cumulative(int **image, int row, int col)
{
int **newImage,i,j,pixel, noPix[255], sum, dif;
newImage = allot_int_mn(row, col);
for (j = 0; j < row; j++)
for (i = 0; i < col; i++)
newImage[i][j] = 255;
for (j = 0; j < 255; j++)
noPix[j]=0;
for (j = 0; j < row; j++) {
for (i = 0; i < col; i++) {
pixel = image[i][j];
noPix[pixel]+=1;
}
}
sum = 0;
for (j = 0; j < 255; j++) {
sum += noPix[j];
dif = 255 - sum/255;
for (i = 0; i < dif; i++)
newImage[i][j] = 0;
}
return(newImage);
}
//This function equalizes the the histogram with the use of the CDF
int **eqHistogram(int **image, int row, int col)
{
int **newImage,i,j,pixel, noPix[255], eqNoPix[255], sum, sumNew,
outGray, rows;
newImage = allot_int_mn(row, col);
for (j = 0; j < row; j++)
for (i = 0; i < col; i++)
newImage[i][j] = 255;
for (j = 0; j < 255; j++){
noPix[j]=0;
eqNoPix[j]=0;
}
for (j = 0; j < row; j++) {
for (i = 0; i < col; i++) {
pixel = image[i][j];
noPix[pixel]+=1;
}}
sum = 0;
sumNew = 0;
for (j = 0; j < 255; j++) {
sum += noPix[j];
outGray = sum/255;
eqNoPix[outGray] += noPix[j];
}
int max=0;
for (j = 0; j < 255; j++)
if(eqNoPix[j]>max) max = eqNoPix[j];
for (j = 0; j < 255; j++)
eqNoPix[j] = 255*eqNoPix[j]/max;
for (j = 0; j < 255; j++) {
rows = 255 - eqNoPix[j];
for (i = 0; i < rows; i++)
newImage[i][j] = 0;
}
return(newImage);
}
//This function uses the equalization histogram to output a new image
int **eqImage(int **image, int row, int col)
{
int **newImage,i,j,pixel, noPix[255], eqNoPix[255], outGray;// rows;
newImage = allot_int_mn(row, col);
for (j = 0; j < 255; j++){
noPix[j]=0;
eqNoPix[j]=0;
}
for (j = 0; j < row; j++) {
for (i = 0; i < col; i++) {
pixel = image[i][j];
noPix[pixel]+=1;
}}
int sum = 0;
for (int k = 0; k < 256; k++) {
sum += noPix[k];
outGray = sum/255;
if (outGray > 255)
{
outGray = 255;
}
for (j = 0; j < row; j++) {
for (i = 0; i < col; i++) {
if (image[i][j] == k) {
newImage[i][j] = outGray;
}}}}
return(newImage);
}
//Returns file header information
void fileheader(char fn[],int *a )
{
int c;
FILE *fp;
fp=fopen(fn,"r");
while((c=getc(fp)) !='\n');
while ((c=getc(fp)) == '#')
{while ((c=getc(fp)) != '\n');}
ungetc(c,fp);
fscanf(fp,"%d%d%d",&a[0],&a[1],&a[2]);
fclose(fp);
}
//Reads in image
void filereadcwh(char fn[],int **aa,int row, int col )
{
int i,j,c;
int temp[3];
int x;
FILE *fp;
fp=fopen(fn,"r");
while((c=getc(fp)) !='\n');
while ((c=getc(fp)) == '#')
{while ((c=getc(fp)) != '\n');}
ungetc(c,fp);
fscanf(fp,"%d%d%d",&temp[0],&temp[1],&temp[2]);
for(i=0;i<row;i++){
for(j=0;j<col;j++){
fscanf(fp, "%d", &x);
aa[i][j] = x;
}
}
fclose(fp);
}
//Writes out image
void filewritecwh(char fn[],int **aa,int row, int col, int gray)
{
int i,j;
ofstream outfile(fn, ios::out);
outfile << "P5\n" << col << " " << row << endl << gray << endl;
for(i=0;i<row;i++) {
for(j=0;j<col;j++) {
outfile << (char)aa[i][j];
}
}
}
//allocates memory to array pointer
int **allot_int_mn(int row, int col)
{
int** imageMatrix;
imageMatrix = new int* [row];
for (int i = 0; i < row; i++){
imageMatrix [i] = new int [col];
}
return imageMatrix;
}