只是切换了图片,小图50*50工作正常:
大图1024*768只工作到三分之一的地方直接结束没有报错什么的,见图:
我特意在一些地方放了打印文本的指令,可以看出差别了吧,跳掉了后面的部分
如下是我的程序,省略了一部分:
#include "stdio.h"
#include "stdlib.h"
#include "machine_vision.h"
#define PI 3.14159265
void MeanFilter(void);
void GaussianFilter(void);
void MedianFilter(void);
void Contrastenhancement(void);
void maxandmin(void);
void connectedcomponent(void);
void Center(void);
FILE *FilePtr;
int main(void)
{
char num[10];
char number[128];
unsigned int i,n=0;
unsigned int choice;
LoadBMPImage("c:\\Irb1400_Image1.bmp");
mprintf("\n\t ------ Menu ------\n");
mprintf("Enter No of Elements\n");
mprintf("\n\tEnter Choice\n\t1.Mean\n\t2.Gaussian\n\t3.Median\n\t4.Contrast enhancement\n\t5.Max and min\n\t6.Connected component \n\t7.Center \n\t8.Exit");
do
{
mscanf(num);
choice=atoi(num);
scanf("%d",choice);
switch(choice)
{
case 1:
MeanFilter();
break;
case 2:
GaussianFilter();
break;
case 3:
MedianFilter();
break;
case 4:
Contrastenhancement();
break;
case 5:
maxandmin();
break;
case 6:
connectedcomponent();
break;
case 7:
Center();
break;
case 8:
break;
}
}while(choice!=8);
}
void Center(void)
{
unsigned int *ImageDestination ;
int long Height,Width,leftp,currentp,topp,temp;
unsigned int *Temp_Image,TempPixRed ,TempPixGreen,TempPixBlue, Temp_Pixel;
unsigned int TempGrey;
unsigned int i,j,k,countobj=1,image[5000],corresp[5000],c,ne,nb; // for lab 3
int ctx,cty,area1; //for lab 4
double a1,b1,c1,tempangle,tangle,cx=0,cy=0;
mprintf("\n You chose Laboration 4 !\n");
//LoadBMPImage("c:\\connect_component_bw_small.bmp");
//LoadBMPImage("c:\\Irb1400_Image1.bmp");
ImageDestination=GetImageDest();
Height=GetImageHeight();
Width=GetImageWidth();
if ((Temp_Image = (unsigned int *)malloc(Height*Width*sizeof(unsigned int))) == NULL)
{
mprintf("Not enough memory to allocate buffer\n");
return;
}
for (i=0 ; i < Height*Width ;i++)
{
TempPixRed=RGB_RED(ImageDestination[i]);
TempPixGreen=RGB_GREEN(ImageDestination[i]);
TempPixBlue=RGB_BLUE(ImageDestination[i]);
TempGrey=(TempPixRed+ TempPixGreen+ TempPixBlue)/3 ;
ImageDestination[i]=RGB_C(TempGrey,TempGrey,TempGrey);
if (TempGrey > 100) //create balck and white
ImageDestination[i] = WHITE;
else if (TempGrey < 100)
ImageDestination[i] = BLACK;
}
Update();
for (i=0 ; i < Height*Width ;i++) //first run
{
if (RGB_RED(ImageDestination[i])!=BLACK)
image[i]=0;
else
image[i]=1;
} //换小图后,后面不执行,直接结束
for (i=Height-1; i>0; i--) //CC算法
{
for(j=0; j<Width-1; j++)
{
leftp=image[Width*i+(j-1)]; //left
topp=image[Width*(i+1)+j]; //top
currentp=image[Width*i+j]; //current
if (currentp!=0)
{
if (leftp==0 && topp>0)
image[Width*i+j]=topp;
else if(topp==0 && leftp>0)
image[Width*i+j]=leftp;
else if(leftp==topp && leftp>0 && topp>0)
image[Width*i+j]=leftp;
else if (leftp!=topp && leftp>0 && topp>0)
{
if (leftp > topp)
{image[Width*i+j]=topp;
corresp[leftp]=topp;}
else if (leftp < topp)
{image[Width*i+j]=leftp;
corresp[topp]=leftp;}
}
else
{
image[Width*i+j]=countobj;
nb=countobj;
corresp[nb]=nb;
countobj=countobj+1;}
}
}
}
for (i=1; i<=nb; i++) //depends on corresp list
{
k=i;
if (corresp[k]!=k)
{
k=corresp[k];
corresp[i]=corresp[k];
}
}
for (i=1; i<Height-1; i++)
{
for(j=1; j<Width-1; j++)
{
if (image[Width*i+j]!=0)
{
c=image[Width*i+j]; // transform
image[Width*i+j]=corresp[c];
}
}
}
if ((FilePtr= fopen("c:\\image.txt","w"))==NULL) //output image
mprintf("file could not be opend");
for (i=Height-1; i>0; i--)
{
for (j=0;j<Width-1;j++)
{
fprintf(FilePtr,"%2d",image[Width*i+j]);
}
fprintf(FilePtr,"\n");
}
fclose(FilePtr);
//*************************
for(c=1;c<=nb;c++)
{
area1=0; //reset to zero
ctx=0;
cty=0;
a1=0;
b1=0;
c1=0;
for (i=1; i<Height-1; i++)
{
for(j=1; j<Width-1; j++)
{
if (image[Width*i+j]==c)
{
ctx=(float)(j*1+ctx);
cty=(float)(i*1+cty);
area1=area1+1;
}
}
}
if (area1>0)
{
cx=(float)(ctx/area1);
cy=(float)(cty/area1);
mprintf("\n Object Information %d",c);
mprintf(" X-value of center position =%f",cx);
mprintf(" Y-value of center position =%f",cy);
for (i=1; i<Height-1; i++)
{
for(j=1; j<Width-1; j++)
{
if (image[Width*i+j]==c)
{
a1=(j-cx)*(j-cx)+a1;
b1=(j-cx)*(i-cy)+b1;
c1=(i-cy)*(i-cy)+c1;
}
}
}
if (a1==c1 && b1==0)
mprintf(" The orientation of object is not unique");
else
{ tangle=0.5*atan2(2*b1,a1-c1)*180/PI;
mprintf(" The Angle=%f\n",tangle);}
}
}
free(Temp_Image);
return 0;
}