intTypePromotion=1
ADSENSE

Xử lý ảnh số

Chia sẻ: Hoangtuan Hoangtuan | Ngày: | Loại File: DOC | Số trang:107

185
lượt xem
47
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'xử lý ảnh số', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Xử lý ảnh số

  1. Phô lôc - Xö lý ¶nh Phô lôc gåm listing ch¬ng tr×nh tr×nh bµy mét c¸ch chi tiÕt c¸c ®Þnh d¹ng ¶nh: BMP, PCX, TIFF, GIF,... vµ c¸c m«®ul ch ¬ng tr×nh thùc hiÖn c¸c c«ng ®o¹n cña qu¸ tr×nh xö lý ¶nh. Ch ¬ng tr×nh ®îc viÕt trªn C vµ Visual C vµ cã thÓ coi nh mét c«ng cô ®¬n gi¶n minh ho¹ c¸c b íc cña xö lý ¶nh. C¸c ¶nh minh ho¹ trong cuèn s¸ch nµy lÊy tõ kÕt qu¶ thùc hiÖn ch ¬ng tr×nh trªn. 1. Nhãm ch¬ng tr×nh n¹p vµ lu ¶nh ViÖc n¹p ¶nh tõ tÖp vµo m¶ng sè vµ l u ¶nh tõ m¶ng lªn tÖp lµ cÇn thiÕt cho mäi chøc n¨ng xö lý. ChÝnh v× thÕ, phÇn ch ¬ng tr×nh cã tæ chøc 2 moduls riªng: - BMP.H: Thùc hiÖn viÖc më vµ ®äc ¶nh sè tõ “tÖp *.BMP” vµo “m¶ng” 2 chiÒu. - PCX.H: Thùc hiÖn viÖc më vµ ®äc ¶nh sè tõ “tÖp *.PCX” vµo “m¶ng” 2 chiÒu. • BMP.H // Chøa c¸c khai b¸o vÒ file ¶nh BMP & c¸c thñ tôc më file. // typedef unsigned int WORD; typedef unsigned long DWORD; typedef unsigned char BYTE; typedef struct tagBITMAPFILEHEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfoffBits; } BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER { DWORD biSize; long biWidth; long biHeight; WORD biPlanes; WORD biBitCount; NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 11
  2. Phô lôc - Xö lý ¶nh DWORD biCompression; DWORD biSizeImage; long biXPelsPerMeter; long biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER; typedef struct tagRGBQUAD { BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved; } RGBQUAD; BYTE huge *InImage; unsigned long p,chieucao,chieurong; int OpenBitmapFile(char *fname,int *fh, BITMAPFILEHEADER *bmfhd, BITMAPINFOHEADER *bmihd); int ReadBitmapPalette(int fh, int numcl, RGBQUAD *bmpals); int GetScanLine(int fh, int BytePerLine, BITMAPINFOHEADER *bih, int x, int y); void SetRGBPalette(int index,int red ,int green,int blue); void SetRGBPalettes(int index, int red, int green, int blue); int GetBitmapToArray(char *fname); extern void Error(unsigned OrderError); //***************************************** // //Më file bitmap // int OpenBitmapFile(char *fname,int *fh,BITMAPFILEHEADER *bmfhd, BITMAPINFOHEADER *bmihd) {int tmp; *fh = _open(fname,O_RDONLY); if(*fh == -1) return -1; // kh«ng më ®îc file tmp = _read(*fh,bmfhd,sizeof(BITMAPFILEHEADER)); if (tmp == -1) { close(*fh); return -1;// lçi ®äc file } NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 12
  3. Phô lôc - Xö lý ¶nh if(bmfhd->bfType != 0x4D42) { close(*fh); return -2 ;// kh«ng ph¶i d¹ng BMP } tmp = _read(*fh,bmihd,sizeof(BITMAPINFOHEADER)); if(tmp != sizeof(BITMAPINFOHEADER)) { close(*fh); return -2 ;//kh«ng ph¶i d¹ng BMP } if (bmihd->biCompression == 0) { bmihd->biSizeImage = (bmfhd->bfSize-bmfhd- >bfoffBits); } return 0; } //end of function //******************************************************** // //§äc b¶ng mµu file bitmap // int ReadBitmapPalette(int fh,int numcl,RGBQUAD *bmpals) { int numbyte,tmp; numbyte=numcl*sizeof(RGBQUAD); lseek(fh,sizeof(BITMAPFILEHEADER) +sizeof(BITMAPINFOHEADER), SEEK_SET); tmp=_read(fh,(void*)bmpals,numbyte); if (tmp == -1) { close(fh);return -1; } if(tmp!=numbyte) {close(fh);return -2; } return 0; } // end of function //******************************************// NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 13
  4. Phô lôc - Xö lý ¶nh int GetScanLine(int fh,int BytePerLine,BITMAPINFOHEADER *bih) //§äc dßng ¶nh vµo m¶ng // { BYTE *buff; int tmp,ppb,mask,i,j,cx,nb,cl; buff=(BYTE *)malloc(BytePerLine+1); if(buff==NULL) {return -3; //Kh«ng ®ñ bé nhí } tmp=_read(fh,buff,BytePerLine); if(tmp != BytePerLine) {return -1; } ppb=8/bih->biBitCount; mask=(1 biBitCount)-1; for(i=0,cx=0;i (bih->biBitCount * (ppb-1-j)); cl=cl & mask; InImage[p++]= cl; if(cx == bih->biWidth) break; } if(cx >= bih->biWidth) break; } free(buff); return 0; } //****************************************// void SetRGBPalette(int index,int red ,int green,int blue) { int r,g,b,cl; red >>= 6; r=(red & 1) = 6; g= (green & 1) >= 6; b= (blue & 1) > 1; cl=r+g+b; _AX=0x1000; NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 14
  5. Phô lôc - Xö lý ¶nh _BL=index; _BH=cl; geninterrupt(0x10); } //***************************************** // void SetRGBPalettes(RGBQUAD *pals,int numcl) { int i; for(i=0;irgbRed,(pals+i) ->rgbGreen, (pals+i) - >rgbBlue); } } //********************************************* // // §äc File BMP vµo m¶ng bé nhí // int GetBitmapToArray(char *fname) { int i,j,tmp,numcolor,BytePerLine,bmfilehand; BITMAPINFOHEADER bminfohdr; BITMAPFILEHEADER bmfilehdr; RGBQUAD *bmpalettes; tmp=OpenBitmapFile(fname,&bmfilehand,&bmfilehdr, &bminfohdr); chieucao= bminfohdr.biHeight; chieurong= bminfohdr.biWidth; switch(tmp) {case -1: return(1); //break; //kh«ng më ®îc file case -2: return(2); //break; //lçi file } numcolor=1
  6. Phô lôc - Xö lý ¶nh if (tmp != 0) return(1); SetRGBPalettes(bmpalettes,numcolor); farfree(bmpalettes); lseek(bmfilehand,bmfilehdr.bfoffBits,SEEK_SET); if((InImage =(BYTE huge *) farcalloc(bminfohdr.biHeight*bminfohdr.biWidth, sizeof(BYTE)))==NULL) return(3); p= 0; for(i=0;i
  7. Phô lôc - Xö lý ¶nh #define EBadParms -1 #define EFileNotFound -2 #define EReadFileHdr -3 #define ENotPCXFile -4 #define ECorrupt -5 #define EWrtFileHdr -6 #define EWrtOutFile -7 #define EWrtScanLine -8 #define EPCCFile -9 #define PCXHdrTag 0x0A #define MaxRepCount 0x3F #define PCX256ColorTag 0x0C typedef struct { BYTE Red; BYTE Green; BYTE Blue; }ColorRegister; struct PCXFileHeader { BYTE Header; BYTE Version; BYTE Encode; BYTE BitPerPix; unsigned X1; unsigned Y1; unsigned X2; unsigned Y2; unsigned Hres; unsigned Vres; }; struct PCXInfo { BYTE Vmode; BYTE NumOfPlanes; unsigned BytesPerLine; BYTE unused[60]; NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 17
  8. Phô lôc - Xö lý ¶nh }; struct ExtendedPalette { BYTE ExtendedPalette; ColorRegister Palette[MAX256PALETTECOLORS]; }; struct PCX_File { struct PCXFileHeader PCXHeader; ColorRegister Palette[MAXPALETTECOLORS]; struct PCXInfo Info; } PCX_FileType; int looping; struct PCX_File PCXData; unsigned ImageWidth,ImageHeight; FILE *PCXFile; BYTE ScanLine[MAXBYTESPERSCAN]; BYTE PixelColorNum[MAXSCREENWIDTH]; struct ExtendedPalette Color256Palette; unsigned ImageWidth,ImageHeight,NumOfPlanes; unsigned Is256ColorFile=FALSE; struct PCX_File PCXData; int ReadPCXFileHdr(char *Filename,int Verbose); static int ExpandScanLine(FILE *InFile); unsigned InstallPCXFilePalette(void); void DisplayPCXFile(char *FileName,int Verbose); void Set256ColorMode(void); void interrupt (*oldkb)(); void interrupt newkb(); int Gwrite(int x,int y,char *gtext,int inc) { WriteXYB(x,y,gtext,1,1,15,0,0,0); return (x+inc+textwidth(gtext)); } int GwriteDel(int x,int y,char *gtext,int inc) { WriteXYB(x,y,gtext,1,1,0,0,0,0); NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 18
  9. Phô lôc - Xö lý ¶nh return (x+inc+textwidth(gtext)); } int Gwriteln(int x,int y,char *gtext) { WriteXYB(x,y,gtext,1,1,15,0,0,0); return (y+10+textheight(gtext)); } char *Gread(int x,int y) { char *gtext,text[2]; char ch; gtext[0]=0x0; do { ch=getch(); if (ch!=Enter) { if (ch!=BackSpace) { text[0]=ch;text[1]='\0'; x=Gwrite(x,y,text,0); strcat(gtext,text); } else if (strcmp(gtext,"")) { text[0]=gtext[strlen(gtext)-1];text[1]='\0'; x=x-textwidth(text); x=GwriteDel(x,y,"Û",0); x=x-textwidth(text); gtext[strlen(gtext)-1]='\0'; } } } while (ch!=Enter); return gtext; } void interrupt newkb() NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 19
  10. Phô lôc - Xö lý ¶nh { if(inportb(0x60)==1) looping=0; oldkb(); } void Set256ColorMode(void) { union REGS regs; regs.h.ah=0; regs.h.al=0x13; int86(VIDEO,&regs,&regs); } int ReadPCXFileHdr(char *Filename,int Verbose) { unsigned Index,i; int row=100,col=100; for(i=0;i
  11. Phô lôc - Xö lý ¶nh printf("\tVersion %d\n",PCXData.PCXHeader.Version); printf("\tCompression %s\n", PCXData.PCXHeader.Encode==0?"none":"RLL"); printf("\tBit per Pixel %d\n",PCXData.PCXHeader.BitPerPix); printf("\tX1: %d\n",PCXData.PCXHeader.X1); printf("\tY1: %d\n",PCXData.PCXHeader.Y1); printf("\tX2: %d\n",PCXData.PCXHeader.X2); printf("\tY2: %d\n",PCXData.PCXHeader.Y2); printf("\tHoriz Resolution: %d\n",PCXData.PCXHeader.Hres); printf("\tVert Resolution: %d\n",PCXData.PCXHeader.Vres); printf("\tVmode: %d\n",PCXData.Info.Vmode); printf("\tNumber of Planes: %d\n",PCXData.Info.NumOfPlanes); printf("\tByte per Scan Line of Plane :%d\n" ,PCXData.Info.BytesPerLine); printf("\n\tHit any key to proceed"); getch(); clrscr(); if ((PCXData.PCXHeader.Hres==320)&&(PCXData.PCXHeader.Vr es==200)&& (PCXData.Info.NumOfPlanes==1)) Is256ColorFile=TRUE; printf("\n\tColor Register Value of PCX file: %s\n\n",Filename); if (Is256ColorFile) for(Index=0;Index
  12. Phô lôc - Xö lý ¶nh printf("\n\tColor Register Value of PCX file: %s\n\n", Filename); } } else for (Index=0;Index
  13. Phô lôc - Xö lý ¶nh if(CharRead==EOF) return(FALSE); while (RepCount--) ScanLine[InPtr++] = CharRead; } else ScanLine[InPtr++]=CharRead; } while (InPtr < BytesToRead); return(TRUE); } unsigned InstallPCXFilePalette(void) { struct palettetype palette; union REGS regs; unsigned Index; if(PCXData.PCXHeader.Version!=3) { if(Is256ColorFile) { for (Index=0;Index>=2; Color256Palette.Palette[Index].Green>>=2; Color256Palette.Palette[Index].Blue>>=2; } regs.h.ah=0x10; regs.h.al=0x12; regs.x.bx=0; regs.x.cx=MAX256PALETTECOLORS; _ES=FP_SEG(&Color256Palette.Palette); regs.x.dx=FP_OFF(&Color256Palette.Palette); int86(VIDEO,&regs,&regs); return(TRUE); } else { palette.size=MAXPALETTECOLORS; for (Index=0;Index
  14. Phô lôc - Xö lý ¶nh palette.colors[Index]=Index; PCXData.Palette[Index].Red>>=2; PCXData.Palette[Index].Green>>=2; PCXData.Palette[Index].Blue>>=2; } regs.h.ah=0x10; regs.h.al=0x12; regs.x.bx=0; regs.x.cx=MAXPALETTECOLORS; _ES=FP_SEG(&PCXData.Palette); int86(VIDEO,&regs,&regs); setallpalette(&palette); return(TRUE); } } else return(FALSE); } void DisplayPCXFile(char *FileName,int Verbose) { int i; BYTE far *PtrScreen; register unsigned ScanNum; register unsigned ColNum; unsigned OffsetDisplay,BytePerLine; int PCXError,color,Plane; long CurrentPos; if((PCXError=ReadPCXFileHdr(FileName,Verbose))! =NoError)exit(PCXError); if((PCXData.PCXHeader.X1!=0) || (PCXData.PCXHeader.Y1! =0)) { printf("\nError PCC file not PCX file\n"); exit(EPCCFile); } if(PCXData.PCXHeader.X2==319) { NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 24
  15. Phô lôc - Xö lý ¶nh Set256ColorMode(); ImageWidth = 320; ImageHeight= 200; } else { ImageWidth=640; switch(PCXData.PCXHeader.Y2) { case 479: setgraphmode(VGAHI); ImageHeight=480;break; case 349: setgraphmode(VGAMED); ImageHeight=350;break; case 199: setgraphmode(VGALO); ImageHeight=200;break; } } CurrentPos=ftell(PCXFile); fseek(PCXFile,-769,SEEK_END); if(fread(&Color256Palette.ExtendedPalette, sizeof(struct ExtendedPalette) ,1,PCXFile)==TRUE) if(Color256Palette.ExtendedPalette==PCX256ColorTag) Is256ColorFile=TRUE; InstallPCXFilePalette(); fseek(PCXFile,CurrentPos,SEEK_SET); for(ScanNum=0;ScanNum
  16. Phô lôc - Xö lý ¶nh for (ColNum=0;ColNum
  17. Phô lôc - Xö lý ¶nh { km mang;int i;int ch; button("",0,0,0,getmaxx(),30,2,15,8,12,RED); rectangle(0+2,0+2,getmaxx()-2,30-2); button("Image Processing System ",200,0+2,0+2,getmaxx()- 4,30-4, 1,8,15,0,WHITE); button("",200,0,460,getmaxx(),getmaxy(),1,0,13,0,WHITE); outtextxy(70,465, "Chon: Di Chuyen: Thoat:"); } //* ************************* // void WriteText(int *x,int *y,char *s) { settextjustify(1,1); outtextxy(*x,*y,s); settextjustify(0,2); *x+=textwidth(s); } //------------------------------------------------// void ReadText(int *x,int *y,char *s) {int i=0;char ch[2]; ch[1]=0; while(1) {ch[0]=getch(); if((ch[0]==ENTER)||(ch[0]==ESCAPE)) break; settextjustify(1,1); WriteText(x,y,ch); settextjustify(0,2); s[i]=ch[0];++i; } s[i]=0;} //------------------------------------------------// void Message(char *st,char *st1,int thongtin) { int x,y,y1= 190; void far *q; unsigned kt; int trai= (getmaxx()/2)-(textwidth(st)/2)-10; int cao= 100; int phai= (getmaxx()/2)+(textwidth(st)/2)+10; kt= imagesize(trai,y1,phai,y1+cao); if((q=(unsigned far*)farmalloc(kt))== NULL) {outtextxy(70,465,"Kh«ng cßn vïng nhí cho cÊt mµn h×nh !"); NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 27
  18. Phô lôc - Xö lý ¶nh exit(1); } getimage(trai,y1,phai,y1+cao,q); button("",0,trai,y1,phai,y1+cao,2,15,15,0,7); rectangle(trai+1,y1+1,phai-1,y1+cao-1); rectangle(trai+3,y1+3,phai-3,y1+cao-3); y= y1+18;x= getmaxx()/2; WriteText(&x,&y,st1); y= y+textheight(st1)+19;x= getmaxx()/2; WriteText(&x,&y,st);line(trai+3,y-13,phai-3,y-13); if(thongtin==1) { y= y+textheight(st)+15; x= getmaxx()/2-30; ReadText(&x,&y,fname); } getch(); putimage(trai,y1,q,COPY_PUT); farfree((unsigned far*)q); } //------------------------------------------------------------// void Warning(void) { int x,y,y1= 190; char *st,*st1; st=" *** Process Running - Please wait ! ***"; st1="WARNING !!!"; int trai= (getmaxx()/2)-(textwidth(st)/2)-10; int cao= 80; int phai= (getmaxx()/2)+(textwidth(st)/2)+10; button("",0,trai,y1,phai,y1+cao,2,15,15,0,7); rectangle(trai+1,y1+1,phai-1,y1+cao-1); rectangle(trai+3,y1+3,phai-3,y1+cao-3); y= y1+22;x= getmaxx()/2; WriteText(&x,&y,st1); y= y+textheight(st1)+25;x= getmaxx()/2; WriteText(&x,&y,st);line(trai+3,y-17,phai-3,y-17); } • PROCESS.H NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 28
  19. Phô lôc - Xö lý ¶nh // Chøa c¸c thao t¸c chÝnh : HiÖn ¶nh, Läc TB, Trung b×nh KG, Läc Trung vÞ, //Läc Sobel, Läc Homomorphie int Averaging(BYTE huge *Image,unsigned cotbd,unsigned hangbd,unsigned rong, unsigned cao,unsigned avecol, unsigned averow,double *matran); void DipslayImageInBuf(BYTE huge *Image,unsigned hang, unsigned cot); void DipslayPCXInBuf(BYTE huge *Image,unsigned hang, unsigned cot); BYTE ReadPixel(BYTE huge *Image,unsigned cot,unsigned hang); void WritePixel(BYTE huge *Image,unsigned hang,unsigned cot,unsigned color); void MedianFilter(BYTE huge *Image,unsigned hangbd,unsigned cotbd, unsigned rong,unsigned cao); int Averaging(BYTE huge *Image,unsigned cotbd,unsigned hangbd, unsigned rong, unsigned cao,unsigned avecol, unsigned averow, double *matran); int SpatialConvolution(BYTE huge *Image,unsigned cotbd, unsigned hangbd, unsigned rong,unsigned cao, unsigned KernelCol, unsigned KernelRow, short *Kernel); extern void Error(unsigned OrderError); extern void Message(char *st,char *st1,int thongtin); void Threshold(BYTE huge *Image,unsigned col,unsigned row, unsigned rong,unsigned cao); //**************************************************// //HiÓn thÞ ¶nh BMP // void DipslayImageInBuf(BYTE huge *Image,unsigned hang,unsigned cot) { int i,j; unsigned long p; p= 0; for(i= cot;i>0;i--) for(j=0;j< hang;j++) { putpixel(j,i,Image[p++]); NhËp m«n xö lý ¶nh sè - §HBK Hµ néi 29
  20. Phô lôc - Xö lý ¶nh } } //******************************************************// //§äc 1 pixel tõ buffer BYTE ReadPixel(BYTE huge *Image,unsigned cot,unsigned hang) {unsigned long p; p= hang; p*= chieurong; p+= cot; return(Image[p]);} //Ghi 1 pixel vµo buffer void WritePixel(BYTE huge *Image,unsigned hang,unsigned cot,unsigned color) { unsigned long p; if ((cot
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2