VOCIMG1.PRJ 檔之內容為:
vocimg1.c
vocimg2.obj
c:\tc\tcvic.lib
c:\tc\tcepo.lib
現將 VOCIMG1.C 原程式碼及其說明列出如下:
#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <dos.h>
#include <stdlib.h>
#include <float.h>
#include <graphics.h>
#include "EPOTUTOR.H"
/*聲音信號處理檔頭程式之設定*/
typedef struct
{
char mark[20];
unsigned datablock_offset;
unsigned version;
unsigned check_code;
} VOC_HEADER;
typedef struct
{
char blocktype;
unsigned char blklen[3];
} BLOCK_HEADER;
#define MAXBUFFERSIZE 40000
#define TEMPLATES 10
#define BUFFERSIZE (unsigned)40000
unsigned digital_a(char *buffer,unsigned buffersize);
unsigned dac(char *buffer,unsigned buffer_length);
char *voicedata;
main()
{
unsigned samplingr=8000;
FILE *fp,*fopen();
int i,j,n,jj[10],shap,old,k,gray;
unsigned length,step1,step2;
char q,over,overj;
char nosound;
char filename[80];
long mm,ii,aa;
float kew;
int drive=DETECT,mode,crtmode;
/*影像信號處理程式之相關設定*/
void gray_transformation( imgdes *srcimg, imgdes *resimg);
char *fname="TEST.TIF";
imgdes image1, image2, image3, image4, oprimg;
int rcode;
/*影像記憶區之初始化*/
initialize_image( &image1);
initialize_image( &image2);
initialize_image( &image3);
initialize_image( &image4);
initialize_image( &oprimg);
/*聲音信號處理記憶區長度之防誤設定*/
if ((voicedata=farmalloc((long)MAXBUFFERSIZE))==NULL)
{
printf("Memory error.\n"); exit(1);
}
/*影像畫面之初始化*/
initialize_display();
/*影像檔名之輸入*/
printf("please input image filename:");
scanf("%s",fname );
/*影像檔之載入*/
if((rcode=loadfile( fname, TIF, &image1)) != NO_ERROR)
{
setvideomode(3);
printf("\nError in loading %s, rcode = %d", fname, rcode);
exit(1);
}
/*影像顯示於座標 (42 ,2) 之位置*/
if((rcode=display_gray_image( 0x12, 42, 2, &image1)) != NO_ERROR)
{
setvideomode(3);
printf("\n error displaying image, rcode = %d", rcode);
}
gotoxy(15,1);
printf("ORIGINAL IMAGE ");
/*影像灰度分佈圖之顯示*/
draw_histogram( &image1, "FIRST IMAGE");
/*影像低通濾波*/
Lopass(&image1, &image3);
/*影像顯示於座標 (340 ,2) 之位置*/
display_gray_image(0x12, 340, 2, &image3);
gotoxy(53,1);
printf("AFTER LOWPASS ");
/*影像二值化*/
Binarize(100,&image3, &image1);
/*影像顯示於座標 (340 ,2) 之位置*/
display_gray_image(0x12, 340, 2, &image1);
gotoxy(53,1);
printf("AFTER BINARIZATION ");
/* 影像中的物体原已獨立,這時候我們可以對物体的幾何特性加以分析計算 */
mm=0;
/* 計算在此區域中所有有亮度值的點素數量總數。 */
for(i=1;i<255;i++)
{
for(j=1;j<234;j++)
{
gray=getpixelgray(&image1,i,j);
if(gray==255)
{
mm=1+mm;
}
}
}
/*如欲換算實際面積的計算很簡單,只要計數物體區域中的像點數目,
然後乘以每像點所代表的實際面積數,即為物體的實際面積。*/
aa=mm;
mm=mm*25;
gotoxy(3,4);
printf("area %lu",mm);
n=0;
/*載入聲音檔AREA "待測物的面積" */
if (sp_file("area",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 "待測物的面積" */
dac(voicedata,length);
over=0;
/* 載入數目字之聲音檔 ,並發出聲音 */
while (!over)
{
jj[n]=(long)mm-(long)mm/(long)10*(long)10;
mm=mm/(long)10;
if (mm<1) over=1;
n++;
}
for(k=n-1;k>-1;k--)
{
itoa((int)jj[k],&filename,10);
printf("\n %s",filename);
if (sp_file(filename,voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
if(k==5 | k==1)
{
if (sp_file("10",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
if(k==4)
{
if (sp_file("10000",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
if(k==3)
{
if (sp_file("1000",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
if(k==2)
{
if (sp_file("100",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
}
/* 載入聲音檔 unit1 */
if (sp_file("unit1",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 */
dac(voicedata,length);
/* 利用 Laplacian 或其他邊緣偵測技術來
分離邊緣和背景 */
Laplacian( &image1, &image3);
/*影像顯示於座標 (340 ,2) 之位置*/
display_gray_image(0x12, 340, 2, &image3);
gotoxy(53,1);
printf("AFTER Laplacian ");
/* 計算在此區域中所有有亮度值的點素數量總數,換算為週長。 */
mm=0;
for(i=1;i<255;i++)
{
for(j=1;j<234;j++)
{
gray=getpixelgray(&image1,i,j);
if(gray==255)
{
mm=1+mm;
}
}
}
ii=mm;
mm=mm*5;
gotoxy(3,5);
printf("length %lu",mm);
n=0;
/* 載入聲音檔 LENGTH "待測物的週長" */
if (sp_file("length",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 "待測物的週長" */
dac(voicedata,length);
over=0;
/* 載入數目字之聲音檔 ,並發出聲音 */
while (!over)
{
jj[n]=(long)mm-(long)mm/(long)10*(long)10;
mm=mm/(long)10;
if (mm<1) over=1;
n++;
}
for(k=n-1;k>-1;k--)
{
itoa((int)jj[k],&filename,10);
printf("\n %s",filename);
if (sp_file(filename,voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
if(k==5 | k==1)
{
if (sp_file("10",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
if(k==4)
{
if (sp_file("10000",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
if(k==3)
{
if (sp_file("1000",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
if(k==2)
{
if (sp_file("100",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
dac(voicedata,length);
}
}
/* 載入聲音檔 unit2 */
if (sp_file("unit2",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 */
dac(voicedata,length);
Copy_image(&image3,&image1);
/*計算物體的中心點座標,表示出此物體的落點所在位置*/
centerm( &image1,&image3);
/*計算物體的緊湊度,其定義為周長的平方與面積之比。*/
shap=(double)ii/(double)aa*(double)ii;
gotoxy(3,10);
printf("\n shape factor= %d ", shap);
/* 載入聲音檔 shape "待測物的形狀像" */
if (sp_file("shape",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 "待測物的形狀像" */
dac(voicedata,length);
/* 利用緊湊度來判斷待測物的形狀,圓之緊湊度等於4p,是理論上的最小值;
而其他的形狀,特別是細長不緊湊的形狀,其緊湊度量都大於這個值,至於在一數位
化二值影像來說,實際上的緊湊度理論上的值頗有差距,特別是就小面積之物體而言
,其差距更大。*/
if(shap <= 14)
{
/* 載入聲音檔 circle "圓形" */
if (sp_file("circle",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 "圓形" */
dac(voicedata,length);
}
if(shap > 14 && shap <= 19)
{
/* 載入聲音檔 rectan "方形" */
if (sp_file("rectan",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 "方形" */
dac(voicedata,length);
}
if(shap < 36 && shap >= 20 )
{
/* 載入聲音檔 trian "三角形" */
if (sp_file("trian",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 "三角形" */
dac(voicedata,length);
}
if(shap >= 36)
{
/* 載入聲音檔 unorm "不屬於圓形、正方形、三角形的其他形狀" */
if (sp_file("unorm",voicedata,&length))
nosound=0;
else
printf("VOC file loading error.\n");
/* 發出聲音 "不屬於圓形、正方形、三角形的其他形狀" */
dac(voicedata,length);
}
getch();
/*恢復DOS 畫面*/
setvideomode(3);
}
/*計算物體的邊界的偏心度。一個區域邊界的長軸或主軸(principle axis)是連接相互距離
最遠的兩個點的直線。短軸是垂直長軸的,而長軸與短軸之比叫做邊界的偏心度*/
int centerm(image1,image3)
imgdes *image1,*image3;
{
int i,j,gray,xc,yc,i1,i2,j1,j2;
double a1,pp,qq,mm=0,xaxis=0,yaxis=0;
for(i=1;i<255;i++)
{
for(j=1;j<234;j++)
{
gray=getpixelgray(image1,i,j);
if(gray==255)
{
mm=1+mm;
xaxis=i+xaxis;
yaxis=j+yaxis;
}
}
}
xc=xaxis/mm;
yc=yaxis/mm;
for(i=1;i<255;i++)
{
setpixelgray(image3,i,yc,255);
}
for(j=1;j<234;j++)
{
setpixelgray(image3,xc,j,255);
}
display_gray_image(0x12, 42, 262, image1);
gotoxy(15,17);
printf("profile");
display_gray_image(0x12, 342, 262,image3);
gotoxy(53,17);
printf("center of mass");
initialize_image(image3);
qq=0.;
pp=2.55*255+2.34*234;
for(i=1;i<255;i++)
{
for(j=1;j<234;j++)
{
gray=getpixelgray(image1,i,j);
if(gray==255)
{
setpixelgray(image3,i,j,50);
a1=(i-xc)*(i-xc)/100.+(j-yc)*(j-yc)/100.;
if(a1>qq)
{
qq=a1;
j1=j;
i1=i;
}
if(pp>a1)
{
pp=a1;
j2=j;
i2=i;
}
}
}
}
qq=sqrt(qq)*10;
pp=sqrt(pp)*10;
gotoxy(3,1);
printf("long axis %10e",qq);
gotoxy(3,2);
printf("short axis %10e",pp);
pp=qq/pp;
gotoxy(3,3);
printf("ratio %10e",pp);
setpixelgray(image3,xc,yc,255);
setpixelgray(image3,i1,j1,255);
setpixelgray(image3,i2,j2,255);
display_gray_image(0x12, 342, 2,image3);
}
/*Laplacian 遮罩處理 */
int Laplacian( imgdes *srcimg, imgdes *resimg)
{
static char conv[]={-1,-1,-1,-1,8,-1,-1,-1,-1,1};
int rcode;
if((rcode=Convolution(conv, srcimg, resimg)) != NO_ERROR)
{
setvideomode(3);
printf("\nError in Convolution, rcode = %d", rcode);
exit(1);
}
return(rcode);
}
/*低通遮罩處理 */
int Lopass( imgdes *srcimg, imgdes *resimg)
{
static char conv[]={1,1,1,1,1,1,1,1,1,9};
int rcode;
if((rcode=Convolution(conv, srcimg, resimg)) != NO_ERROR)
{
setvideomode(3);
printf("\nError in Convolution, rcode = %d", rcode);
exit(1);
}
return(rcode);
}
回上一畫面