?? image.cpp
字號:
#ifndef __IMAGE_H
#define __IMAGE_H
#include "stdafx.h"
//#define WIDTH 512
//#define HEIGHT 512
#include "Image.h"
#include <cmath>
#include <iostream>
//#endif
int HEIGHT,WIDTH;
int OFFSET;
int ISIZE;
unsigned char *m_cHeaderData;
using namespace std;
typedef unsigned char uchar;
///////////////////////////////structure //////////////////////////////////////
#pragma pack(2) /*2 byte packing */
typedef struct
{
unsigned short type;
unsigned long size;
unsigned short reserved1,reserved2;
unsigned long offset;
}Header;
#pragma pack() /* Default packing */
typedef struct
{
unsigned long size;
long width,height;
unsigned short planes;
unsigned short bits;
unsigned int compression;
unsigned int imagesize;
int xresolution,yresolution;
unsigned int ncolors;
unsigned int importantcolors;
}Infoheader;
Header headfirst;
Infoheader headsecond;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////
CImage::CImage(const char* filename)
{
ifstream m_pInFile (filename, ios::in|ios::binary);
m_pInFile.read ((char*)&headfirst.type, sizeof(headfirst.type));
m_pInFile.read ((char*)&headfirst.size, sizeof(headfirst.size));
m_pInFile.read ((char*)&headfirst.reserved1, sizeof(headfirst.reserved1) );
m_pInFile.read ((char*)&headfirst.reserved2, sizeof(headfirst.reserved2) );
m_pInFile.read ((char*)&headfirst.offset, sizeof(headfirst.offset));
cout<<"\n########################################################";
cout<<"\nTYPE : "<<headfirst.type;
cout<<"\nSIZE : "<<headfirst.size;
cout<<"\nOFFSET : "<<headfirst.offset;
cout<<"\n########################################################";
OFFSET = headfirst.offset;
m_pInFile.read ((char*)&headsecond.size, sizeof(headsecond.size));
m_pInFile.read ((char*)&headsecond.width, sizeof(headsecond.width));
m_pInFile.read ((char*)&headsecond.height, sizeof(headsecond.height));
m_pInFile.read ((char*)&headsecond.planes, sizeof(headsecond.planes));
m_pInFile.read ((char*)&headsecond.bits, sizeof(headsecond.bits));
m_pInFile.read ((char*)&headsecond.compression, sizeof(headsecond.compression));
m_pInFile.read ((char*)&headsecond.imagesize, sizeof(headsecond.imagesize));
m_pInFile.read ((char*)&headsecond.xresolution, sizeof(headsecond.xresolution));
m_pInFile.read ((char*)&headsecond.yresolution, sizeof(headsecond.yresolution));
m_pInFile.read ((char*)&headsecond.ncolors, sizeof(headsecond.ncolors));
m_pInFile.read ((char*)&headsecond.importantcolors, sizeof(headsecond.importantcolors));
cout<<"\n########################################################";
cout<<"\nSIZE : "<<headsecond.size;
cout<<"\nWIDTH: "<<headsecond.width;
cout<<"\nHEIGHT : "<<headsecond.height;
cout<<"\nCOLORS: "<<headsecond.ncolors;
cout<<"\nIMAGE SIZE: "<<headsecond.imagesize;
cout<<"\nbits SIZE: "<<headsecond.bits;
cout<<"\n########################################################";
HEIGHT = headsecond.height;
WIDTH = headsecond.width;
ISIZE = headsecond.imagesize;
/////////////////////////////////////////////////
//printf("\n sizeof2 %d",sizeof(headsecond));
///////////////////////////////////////////////////
m_pInFile.close();
m_cHeaderData = new uchar [OFFSET];
m_cImageData = new uchar* [HEIGHT];
m_cFilteredData = new uchar* [HEIGHT];
for( int i = 0; i < HEIGHT; i++)
{
m_cImageData[i] = new uchar [ISIZE/HEIGHT];
m_cFilteredData[i] = new uchar [ISIZE/HEIGHT];
}
m_pInFile1 = new ifstream;
m_pInFile1->open(filename, ios::in | ios::binary);
m_pInFile1->seekg(0, ios::beg);
m_pInFile1->read(reinterpret_cast<char*>(m_cHeaderData),OFFSET);
//cout<<"\nData -> "<<m_cHeaderData;
cout<<"\nvalue of HEIGHT : "<<HEIGHT;
cout<<"\nvalue of WIDTH : "<<WIDTH;
cout<<"\nSIZE OF F-HEADER : "<<sizeof(headfirst);
cout<<"\nSIZE OF I-HEADER : "<<sizeof(headsecond);
for(int i = 0; i < HEIGHT; i++)
{
m_pInFile1->read(reinterpret_cast<char *>(m_cImageData[i]), ISIZE/HEIGHT);
}
m_pInFile1->close();
// define the filter for the image
char m_cFilter[3][3] = {
{1, 1, 1},
{1, 2, 1},
{1, 1, 1}
};
}//Constructor Closing
///////////////////////////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////////////////////////
/*CImage::~CImage()
{
delete m_pInFile;
delete m_pOutFile;
for(int i = 0; i < HEIGHT; i++)
{
delete[] m_cImageData[i];
delete[] m_cFilteredData[i];
}
delete[] m_cImageData;
delete[] m_cFilteredData;
}*/
///////////////////////////////////////////////////////////////////////////////
// Output the filtered image to a new bitmap file
///////////////////////////////////////////////////////////////////////////////
void CImage::write(const char* filename)
{
filter();
m_pOutFile = new ofstream;
m_pOutFile->open(filename, ios::out | ios::trunc | ios::binary);
////////////////////////////////////////////////////////////////////
//m_pOutFile->write ((char*)&headfirst, sizeof(headfirst));
//m_pOutFile->write ((char*)&headsecond, sizeof(headsecond));
/////////////////////////////////////////////////////////////////////
m_pOutFile->write(reinterpret_cast<char*>(m_cHeaderData), OFFSET);
for(int i = 0; i < HEIGHT; i++)
{
m_pOutFile->write(reinterpret_cast<char*>(m_cFilteredData[i]), ISIZE/HEIGHT);
}
m_pOutFile->close();
cout<<"Aswathy hii";
}
///////////////////////////////////////////////////////////////////////////////
// Calculate the mean of all the pixels
///////////////////////////////////////////////////////////////////////////////
float CImage::mean()
{
float total = 0;
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
total += m_cImageData[i][j];
}
}
return (total / (HEIGHT * WIDTH));
}
///////////////////////////////////////////////////////////////////////////////
// Calculate the mode of the image
///////////////////////////////////////////////////////////////////////////////
float CImage::mode()
{
float mode = 0;
float Arr[256];
for(int l=0;l<=255;l++)
{
Arr[l]=0;
}
for (int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
Arr[m_cImageData[i][j]]+=1;
}
}
//mode = 0;
float Arr2 = Arr[0];
for(int h=1;h<=255;h++)
{
//cout <<" Arr["<<h<<"] = "<<Arr[h];
if (Arr2<=Arr[h])
mode = h;
}
return (mode);
}
///////////////////////////////////////////////////////////////////////////////
// Calculate the median of the image
///////////////////////////////////////////////////////////////////////////////
float CImage::median()
{
float mode = 0;
float Arr[256];
for(int l=0;l<=255;l++)
{
Arr[l]=0;
}
for (int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
Arr[m_cImageData[i][j]]+=1;
}
}
float temp;
for(int k = 0; k<256;k++)
{
for(int b = k+1;b<256; b++)
{
if(Arr[k]<Arr[b])
{
temp = Arr[k];
Arr[k] = Arr[b];
Arr[b] = temp;
}
}
}
if((HEIGHT*WIDTH)%2 == 0)
{
//cout<<"Even "<<Arr[255/2]+1;
return(Arr[255/2]+1);
}
else
{
//cout<<"Odd "<<Arr[255/2];
return(Arr[255/2]);
}
}
///////////////////////////////////////////////////////////////////////////////
// Calculate the standard deviation of all the pixels
///////////////////////////////////////////////////////////////////////////////
float CImage::stdv()
{
float avg = mean();
float radicand = 0;
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
radicand += (m_cImageData[i][j] - avg) * (m_cImageData[i][j] - avg);
}
}
radicand /= (HEIGHT * WIDTH);
return sqrt(radicand);
}
///////////////////////////////////////////////////////////////////////////////
// Return Height of the Image
///////////////////////////////////////////////////////////////////////////////
int CImage::Img_Height()
{
return HEIGHT;
}
///////////////////////////////////////////////////////////////////////////////
// Return Width of the Image
///////////////////////////////////////////////////////////////////////////////
int CImage::Img_Width()
{
return WIDTH;
}
///////////////////////////////////////////////////////////////////////////////
// Filter all the pixels
///////////////////////////////////////////////////////////////////////////////
void CImage::filter()
{
int sumofpixels = 0;
for(int i = 0; i < HEIGHT; i++)
{
strcpy(reinterpret_cast<char*>(m_cFilteredData[i]), reinterpret_cast<char*>(m_cImageData[i]));
}
for( int i = 1; i < HEIGHT - 1; i++)
{
for(int j = 1; j < WIDTH - 1; j++)
{
sumofpixels = m_cFilter[0][0] * m_cImageData[i+1][j-1] + // top left corner
m_cFilter[0][1] * m_cImageData[i+1][j] + // top center
m_cFilter[0][2] * m_cImageData[i+1][j+1] + // top right corner
m_cFilter[1][0] * m_cImageData[i][j-1] + // center left
m_cFilter[1][1] * m_cImageData[i][j] + // center center
m_cFilter[1][2] * m_cImageData[i][j+1] + // center right
m_cFilter[2][0] * m_cImageData[i-1][j-1] + // bottom left corner
m_cFilter[2][1] * m_cImageData[i-1][j] + // bottom center
m_cFilter[2][2] * m_cImageData[i-1][j+1]; // bottom right corner
m_cFilteredData[i][j] = (sumofpixels / (m_cFilter[0][0] + m_cFilter[0][1] +
m_cFilter[0][2] + m_cFilter[1][0] +
m_cFilter[1][1] + m_cFilter[1][2] +
m_cFilter[2][0] + m_cFilter[2][1] +
m_cFilter[2][2]) );
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -