BMP EdgeDetection (Monocrome Image)

//BMP.h
// Created by Renjith and Sajith


#include
#include

#pragma once

struct _v3f
{
_v3f( ): x(0.0f), y(0.0f), z(0.0f) //default constructor
{ };
_v3f(float xx, float yy, float zz )//3 argument constructor
{
x = xx;
y = yy;
z = zz;
}
float x;
float y;
float z;
};

class CBmp
{

//Type definitions
typedef std::vector<_v3f> vec3f;
typedef std::vector vecint;
typedef std::string _str;

//Member data
HBITMAP m_hBitmap;
BYTE* m_bmpBuffer;
int m_height;
int m_width;
int m_side;
int m_bgcolor;
bool m_eightblocks[8];
DWORD m_sizeBuffered;
vec3f m_vecCords;
vecint m_vecPixels;

public:
//constructor && destructor
CBmp (void);
virtual ~CBmp(void);

//Member functions
void Load (_str path, int nSide);
void BlacknWhite(int nBGVal);
void Process_BMPdata();
void Store_BMPdata();
int FindBackGround();
bool SetSize_BMPbuffer(long size);
long GetSize_BMPimage();
bool InttoBoolConverter(int nData);
vec3f Edges_BMPimage(){return m_vecCords;};


protected:
void Free ()
{
if (m_hBitmap)
::DeleteObject (m_hBitmap);
}
};



//BMP.cpp
// Created by Renjith and Sajith


#include "Bmp.h"

using namespace std;

CBmp::CBmp(void):m_hBitmap (0)
{
}

CBmp::~CBmp(void)
{
Free();
}

void CBmp::Load (string path, int nSide)
{
Free ();//here confirms that the handle is deleted first(freed)
m_hBitmap = (HBITMAP) ::LoadImage (0, path.c_str(),IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (m_hBitmap == 0)
{
MessageBox(NULL,"Loading Bitmap Handle Failed... Exiting","",0) ;
return;
}

//set the m_side
m_side = nSide;

//Get the size of BMP
long size = this->GetSize_BMPimage();

//Set the size of buffer for storing BMP data
if(!this->SetSize_BMPbuffer(size))
return;

this->Store_BMPdata();
}

void CBmp::Store_BMPdata()
{
m_sizeBuffered = GetBitmapBits(m_hBitmap,m_width*m_height,(BYTE*)m_bmpBuffer);

if(0 == m_sizeBuffered){
MessageBox(NULL,"Error copying BMP data to Buffer","",0);
return;
}
//find the background pixel value
m_bgcolor = this->FindBackGround();

//change the background to 0 and the Edges to 255
this->BlacknWhite(m_bgcolor);

for(int a=0; a < m_sizeBuffered; a++)
{
BYTE ch = m_bmpBuffer[a];
m_vecPixels.push_back(ch);
ch=0;
}

this->Process_BMPdata();

}

void CBmp::Process_BMPdata()
{
if(m_width%8)
m_width = m_width + (8 - m_width%8);
else if(m_width%32)
m_width = m_width + (32-m_width%32);

// Converts and appends rows in the form of 8xn ie set of 8 bits
vecint::iterator iter = m_vecPixels.begin();

int row=0;
int col=0;

FILE *fp1;
fp1 = fopen("rastordata.txt","w");

for(iter; iter != m_vecPixels.end(); ++iter)
{
if(!InttoBoolConverter(*iter))
return;//TODO Write Error msg

if(col == (m_width))
{
col=0;
row++;
}

for(int i=0; i< 8; ++i)
{
if(m_eightblocks[i] == true)
{
printf("(X,Y)=(%d,%d)\n",row,col);

fprintf(fp1,"%d,%d\n",row,col);
if(0 == m_side) //XY plane (front)
m_vecCords.push_back(_v3f(col,m_height - row,0));
else if(1 == m_side)//XZ plane (top)
m_vecCords.push_back(_v3f(col,0,m_height - row));
else if(2 == m_side)//YZ plane (Side)
m_vecCords.push_back(_v3f(0,m_height - row,col));
}
col++;
}
}
fclose(fp1);


}

bool CBmp::InttoBoolConverter(int nData)
{
unsigned short counter = 7;
unsigned short thevalue;
thevalue = nData;
// bool boolData[8];

memset(m_eightblocks,0,8);

if(thevalue < 0)
thevalue = -1*thevalue; // converts negative to positive value

if(thevalue > 255)
{
// give msg that limit exceeds
// cout<<"\n The value exceeds\n";
// Values are set to minimum
return false;
}

// Value gets converted to boolean and gets stored
while(thevalue!=0)
{
m_eightblocks[counter]= (thevalue )& 1 ;
thevalue = thevalue >> 1;
counter--;
}

return true;
}


bool CBmp::SetSize_BMPbuffer(long size)
{
m_bmpBuffer = (BYTE*)GlobalAlloc(GPTR,size);//allocate memory for image
if(NULL == m_bmpBuffer){
MessageBox(NULL,"Memory Allocation for BMP buffer failed","",0);
return false;
}
memset(m_bmpBuffer,0,size);
return true;
}

long CBmp::GetSize_BMPimage()
{
BITMAP bmp;
::GetObject (m_hBitmap, sizeof (bmp), & bmp);
m_width = bmp.bmWidth;
m_height = bmp.bmHeight;

return (long(m_width*m_height));
}

int CBmp::FindBackGround()
{
if(NULL == m_bmpBuffer);
return -1;

float PixArray[256];
int tmp= 0,bg = 0;

for(int ii=0;ii<256;ii++)
PixArray[ii]=0;

for(int i=0;i for(int j=0;j BYTE x = m_bmpBuffer[i * m_width +j];
PixArray[x]++;
}
}

/**Finding Maximum */
for(int k=0;k<256;k++){
if(tmp tmp = (int) PixArray[k];
bg = k;
}
}
return bg;
}

/*Converting the Image buffer into black and white image*/
/* Background =0
Edge = 255 */
void CBmp::BlacknWhite(int nBGVal)
{
if(NULL == m_bmpBuffer);
return ;

for(int i=0;i {
for(int j=0;j {
if(m_bmpBuffer[i * m_width+j] == nBGVal)
m_bmpBuffer[i * m_width+j] = 0;
else
m_bmpBuffer[i * m_width+j] = 255;
//printf("The buf =%d\n",m_bmpBuffer[i * m_width+j]);
}
}
}

0 comments: