You are on page 1of 16

/*********** funcDec.h ********************************// #include <stdio.h> #include <conio.h> #include <iostream.h> #include <afx.h> #include <PROCESS.H> #include <math.

h> #include <ipl98/ipl98 cplusplus.h> using namespace ipl; //******* Globals **********// int width,height,pixelVal,i,j,BGColor=0,retVal; char inputFile[80],outputFile[80]; CFloatImage temp; CImage binaryImg;

struct LinkedPoint{ int X,Y; LinkedPoint *nextPoint; }; //******* Functions Decleration **********// int MainMenu(void) { printf("\nchoose input Image type: \n"); printf("\t 1. Regular Grayscale Image.\n"); printf("\t 2. Edge Image(not yet threesholded).\n"); printf("\t 3. Binary Image.\n "); printf("\t 4. E x i t .\n\n\t "); cin >>retVal; cin.ignore(80,'\n'); }

int EdgeDetectorOperatorMenu(void) { printf("\nchoose the Operator: \n"); printf("\t 1. Robert Operator.\n"); printf("\t 2. Sobel Operator.\n"); printf("\t 3. Prewitt Operator.\n "); printf("\t 4. E x i t .\n\n\t "); cin >>retVal; cin.ignore(80,'\n'); return retVal; } //***************************************// char * GetName() { char* name; name=(char* )malloc(30); printf ("\n Image name : "); gets(name); return name; }

char * GetFullPath(char * imgName) { char *fullPath; fullPath=(char* )malloc(80); GetCurrentDirectory(70,fullPath); strcat(fullPath ,"\\"); strcat(fullPath ,imgName); return fullPath;

int GetWantedLevel() { char yn='n'; int level; while(yn!='y' && yn!='Y') { printf("\n At whitch level you want to threshold?"); printf("\n\t\t Level = "); cin >>level; cin.ignore(80,'\n'); yn='y'; if (level<0 || level>255) { printf("\n\t you must choose between 0-255"); yn='n'; } } return level; }

float GetDeltaRo(void) { float ret; printf ("\t DeltaRo = "); cin >>ret; cin.ignore(80,'\n'); return ret; }

float GetDeltaTheta(void) { float ret; printf ("\t DeltaTheta = "); cin >>ret; cin.ignore(80,'\n'); return ret; }

float GetTheta(void) { float ret; printf ("\t Theta = "); cin >>ret; cin.ignore(80,'\n'); ret+=90; //the user enter the angle of the line but //the prog ram use the angle of the normal while(retVal>90.0) retVal-=180; while(retVal<-90.0) retVal+=180; return -retVal; } //since the image origin is upper left and //the user think it in the lower left

int GetConnectDistance(void) { printf ("\t Max distance to connect = "); cin >>retVal; cin.ignore(80,'\n'); return retVal;

} int GetPixelsCount(void) { printf ("\n\t Threshold (minimum # of pixels in the line) = "); cin >>retVal; cin.ignore(80,'\n'); return retVal; }

int HoughType(void) { printf("\nHough transform using:\n"); printf("\t 1. Delta Theta.\n"); printf("\t 2. Specified angle.\n"); cin >>retVal; cin.ignore(80,'\n'); return retVal; }

///*******************************************************///

void ViewImg(char *fullPath) { char* a; a=(char*)malloc(100); strcpy(a,"ACDSee32.exe "); strcat (a,fullPath); system (a); } //run a command line

int GetIntValue(float x) { if (x>0) return (int)(x+0.5); else return (int)(x-0.5); }

int GetPixelValue(CByteImage *img,int i,int j) { if (i<0||i>=width||j<0||j>=height) return BGColor; else return img->GetPixelFast(i,j); } //out of range

int GetBackGroundColor(CByteImage* inputImg) { int black=0,white=0 ,gray =0; for (i=0;i<width;i++) for (j=0;j<height;j++) pixelVal=inputImg->GetPixelFast(i,j); if (pixelVal<30) black++; else if(pixelVal>220) white++; else if(pixelVal>90&&pixelVal<120) gray++; return (black>white)? ((black>gray)? 0:100):((white>gray)? 255:100); }

float GetMinValue(CFloatImage *img) { float val,min=1000; width=img->GetWidth(); height=img->GetHeight(); for (i=0;i<width;i++) for (j=0;j<height;j++) { val=img->GetPixelFast(i,j); if (val<min) min=val; } return min; }

float GetMaxValue(CFloatImage *img) { float val,max= -1000; width=img->GetWidth(); height=img->GetHeight(); for (i=0;i<width;i++) for (j=0;j<height;j++) { val=img->GetPixelFast(i,j); if (val>max) max=val; } return max; }

void Remap(CByteImage* outputImg,CFloatImage* floatImg) { float min,max; min=GetMinValue(floatImg); max=GetMaxValue(floatImg); float factor=255.0/(max-min); for(i=0; i<width; i++) for(j=0; j<height; j++) outputImg->SetPixelFast(i,j,(int) (((floatImg->GetPixelFast(i,j))- min)*factor )); }

void RobertOperator(CByteImage* edgeImg,CByteImage* grayImg) { width = grayImg->GetWidth(); height = grayImg->GetHeight(); temp.Alloc(width,height); BGColor=GetBackGroundColor(grayImg); for(i=0; i<width; i++) for(j=0; j<height; j++) // | I(r,c)-I(r-1,c-1) |+| I(r,c-1)-I(r-1,c) |

temp.SetPixelFast(i, j,abs( GetPixelValue(grayImg,i,j) -GetPixelValue(grayImg,i-1,j-1)) +abs(GetPixelValue(grayImg,I-1,j) -GetPixelValue(grayImg,i,j-1)) ); Remap(edgeImg,&temp); temp.Empty(); }

void SobelOperator(CByteImage* edgeImg,CByteImage* grayImg) { width = grayImg->GetWidth(); height = grayImg->GetHeight(); temp.Alloc(width,height); BGColor=GetBackGroundColor(grayImg); float S1,S2; for(i=0; i<width; i++) for(j=0; j<height; j++) { /* the code after discard the zeros multiplication */ S1=-( GetPixelValue(grayImg,i-1,j-1)

+2*GetPixelValue(grayImg,i-1,j ) + GetPixelValue(grayImg,i-1,j+1) )

+(

GetPixelValue(grayImg,i+1,j-1) +2*GetPixelValue(grayImg,i+1,j ) + GetPixelValue(grayImg,i+1,j+1) );

S2=-(

GetPixelValue(grayImg,i-1,j-1)

+2*GetPixelValue(grayImg,i ,j-1) + GetPixelValue(grayImg,I+1,j-1) )

+(

GetPixelValue(grayImg,i-1,j+1) +2*GetPixelValue(grayImg,i ,j+1) + GetPixelValue(grayImg,i+1,j+1) );

temp.SetPixelFast(i, j,sqrt( S1*S1 2 S2*S2 )); }

Remap(edgeImg,&temp); temp.Empty(); }

//// **** we do as in SobelOperator *** ////

void PrewittOperator(CByteImage* edgeImg,CByteImage* grayImg) { width = grayImg->GetWidth(); height = grayImg->GetHeight(); temp.Alloc(width,height); BGColor=GetBackGroundColor(grayImg); float P1,P2; for(i=0; i<width; i++) for(j=0; j<height; j++) { P1= -( GetPixelValue(grayImg,i-1,j-1)

+GetPixelValue(grayImg,i-1,j ) +GetPixelValue(grayImg,i-1,j+1) )

+( GetPixelValue(grayImg,i+1,j-1) +GetPixelValue(grayImg,i+1,j ) +GetPixelValue(grayImg,i+1,j+1) );

P2= -(

GetPixelValue(grayImg,i-1,j-1)

+GetPixelValue(grayImg,i ,j-1) +GetPixelValue(grayImg,i+1,j-1) ) +( GetPixelValue(grayImg,i-1,j+1) +GetPixelValue(grayImg,i ,j+1) +GetPixelValue(grayImg,i+1,j+1) );

temp.SetPixelFast(i, j,sqrt( P1*P1 2 P2*P2 )); } Remap(edgeImg,&temp); temp.Empty(); }

//****************************************************************//

void ConvertIdgeToBinary(CImage* binaryImg,CByteImage* edg,int level) { width = edg->GetWidth(); height= edg->GetHeight(); for(i=0; i<width; i++) for(j=0; j<height; j++) binaryImg->SetPixelFast(i,j,(edg->GetPixelFast(i,j))>level?1:0 ); }

void CopyImg(CImage *out,int in) { for(i=0; i<width; i++) for(j=0; j<height; j++) out->SetPixelFast(i,j,in); }

void DrawLine(CImage* transImg,int x1,int y1,int x2,int y2) { int dx=x2-x1; int dy=y2-y1;

float m; if (abs(dx)>abs(dy)) { int xInc=dx/abs(dx); m=fabs((float)dy/(float)dx); int X; float Y=(float)y1;

for(X=x1;X!=x2;X+=xInc) { transImg->SetPixelFast(X,GetIntValue(Y),1); Y+=m; } } else { int yInc=dy/abs(dy); m=(float)dx/fabs(dy); float X=(float)x1; int Y; for(Y=y1;Y!=y2;Y+=yInc) { transImg->SetPixelFast(GetIntValue(X),Y,1); X+=m; } transImg->SetPixelFast(GetIntValue(X),Y,1); }

//********* Hough transforms **********// void HoughTransform(CImage* transImg,CImage* binaryImg,float deltaRo

,float deltaTheta,float startTheta,int connectDistance ,int pixelsCount) { width = binaryImg->GetWidth(); height= binaryImg->GetHeight();

int roNum,thetaNum,tempRo,tempTheta,x1,y1,x2,y2,index; float ro,actualDistance,chord,theta; double angle;

chord=sqrt(width*width+height*height); roNum=GetIntValue( 2*(chord)/deltaRo )+2; thetaNum=GetIntValue(180.0/deltaTheta);

//Hough space allocation LinkedPoint* hough=(LinkedPoint*)malloc(3*4*roNum*thetaNum+5); LinkedPoint *tempLinkedPoint,*t1;

for (i=0;i<roNum*thetaNum;i++) { hough[i].X=-1; hough[i].nextPoint=NULL; }

for(i=0; i<width; i++) { for(j=0; j<height; j++) { if(binaryImg->GetPixelFast(i,j)==1) for(theta=startTheta;theta<90;theta+=deltaTheta)

{ angle=theta*3.14159265358979323846/180; ro =i*cos(angle)+j*sin(angle); tempRo=GetIntValue((ro+chord)/deltaRo); tempTheta=GetIntValue( (theta+90)/deltaTheta ); index=tempRo*thetaNum+tempTheta; if (hough[index].X==-1)//the first point (empty block) { hough[index].X =i; hough[index].Y =j; } else { tempLinkedPoint=&hough[index]; while(tempLinkedPoint->nextPoint!=NULL) tempLinkedPoint=tempLinkedPoint->nextPoint; tempLinkedPoint->nextPoint=(LinkedPoint* )malloc(12); tempLinkedPoint=tempLinkedPoint->nextPoint; tempLinkedPoint->X=i; tempLinkedPoint->Y=j; tempLinkedPoint->nextPoint=NULL; } } } printf("\r %d%c completed",i*100/width,37); LinkedPoint *startPoint; int counter; for(i=0; i<roNum; i++) { for(j=0; j<thetaNum; j++)

{ index=i*thetaNum+j; if (hough[index].X!=-1) { counter=1; tempLinkedPoint=&hough[index]; startPoint=&hough[index]; while (tempLinkedPoint->nextPoint!=NULL) { x1=tempLinkedPoint->X; y1=tempLinkedPoint->Y; x2=tempLinkedPoint->nextPoint->X; y2=tempLinkedPoint->nextPoint->Y; actualDistance=sqrt(pow((x2-x1),2)+pow((y2-y1),2)); if(actualDistance<=connectDistance) counter+=1; else { if(counter>=pixelsCount) { t1=startPoint; DrawLine(transImg,t1->X,t1->Y ,tempLinkedPoint->X,tempLinkedPoint->Y); } startPoint=tempLinkedPoint->nextPoint; counter=1; } tempLinkedPoint=tempLinkedPoint->nextPoint ; } if(counter>=pixelsCount)

{ t1=startPoint; DrawLine(transImg,t1->X,t1->Y ,tempLinkedPoint->X,tempLinkedPoint->Y); } } } printf("\r %d%c completed",i*100/roNum,37); }

You might also like