Tags
This content has not been tagged yet
|
|
Jun 19 2008, 10:21 AM
|

Yahoo! Baby

Group: Member
Posts: 44
Cash: 44k
Age: 21
Joined: 2-December 05
From: Hà Nội
Member No.: 9,861
Yahoo! Status: 

|
Bạn nào giúp mình giải thích xem đoạn code này chạy như thế nào? Mình không biết cách nào để cho nó chạy cả vấn đề gặp phải ở chỗ bmpInput, bmpOutput. Xin giúp đỡ cảm ơn nhiều.
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <math.h> #include <alloc.h> #include <dos.h> typedef struct {int rows; int cols; unsigned char* data;} sImage;
long getImageInfo (FILE*,long,int); void copyImageInfo (FILE* inputFile,FILE* outputFile); void copyColorTable (FILE* inputFile,FILE* ouputFile,int nColors); int main(int argc,char* argv[]) { clrscr(); char far *data; FILE *bmpInput, *bmpOutput; sImage originalImage; sImage edgeImage; unsigned int X,Y,row,col; int I,J,nColors,SUM; long sumX,sumY; unsigned long vectorSize,fileSize; int GX[3][3],GY[3][3]; unsigned char *pChar,someChar; someChar='0'; pChar=&someChar; GX[0][0]=-1; GX[0][1]=0; GX[0][2]=1; GX[1][0]=-2; GX[1][1]=0; GX[1][2]=2; GX[2][0]=-1; GX[2][1]=0; GX[2][2]=1;
GY[0][0]= 1; GY[0][1]= 2; GY[0][2]= 1; GY[1][0]= 0; GY[1][1]= 0; GY[1][2]= 0; GY[2][0]=-1; GY[2][1]=-2; GY[2][2]=-1;
if (argc<2) { printf("Usage: %s bmpInput.bmp\n",argv[0]); exit(0); } printf("Reading filename %s\n",argv[1]); bmpInput=fopen(argv[1],"rb");
fseek(bmpInput,0L,SEEK_END); fileSize=getImageInfo(bmpInput,2,4); originalImage.cols=(int)getImageInfo(bmpInput,18,4); originalImage.rows=(int)getImageInfo(bmpInput,22,4); edgeImage.rows=originalImage.rows; edgeImage.cols=originalImage.cols; printf("Width: %d\n",originalImage.cols); printf("Height: %d\n",originalImage.rows); printf("File Size: %lu\n",fileSize); nColors=(int)getImageInfo(bmpInput,46,4); printf("nColors: %d\n",nColors); vectorSize=fileSize-(14+40+4*nColors); printf("vectorSize: %lu\n",vectorSize);
edgeImage.data=(unsigned char*)farmalloc(vectorSize*sizeof(unsigned char)); if (edgeImage.data==NULL) { printf("Failed to malloc edgeImage.data\n"); exit(0); } printf("%lu bytes malloc'ed for edgeImage.data\n",vectorSize);
originalImage.data=(unsigned char*)farmalloc(vectorSize*sizeof(unsigned char)); if (originalImage.data==NULL) { printf("Failed to malloc originalImage.data\n"); exit(0); } printf("%lu bytes malloc'ed for edgeImage.data\n",vectorSize);
copyImageInfo(bmpInput,bmpOutput); copyColorTable(bmpInput,bmpOutput,nColors); fseek(bmpInput,(14+40+4*nColors),SEEK_SET); fseek(bmpOutput,(14+40+4*nColors),SEEK_SET);
for (row=0;row<=(originalImage.rows-1);row++) { for (col=0;col<=(originalImage.cols-1);col++) { fread(pChar,sizeof(char),1,bmpInput); *(originalImage.data+row*originalImage.cols+col)=*pChar; } }
for (Y=0;Y<=(originalImage.rows-1);Y++) { for (X=0;X<=(originalImage.cols-1);X++) { sumX=0; sumY=0; if (Y==0 || Y==originalImage.rows-1) SUM=0; else if (X==0 || X==originalImage.cols-1) SUM=0; else { for (I=-1;I<=1;I++) { for (J=-1;J<=1;J++) { sumX=sumX+(int)((*(originalImage.data+X+I+(Y+J)*originalImage.cols))*GX[I+1][J+1 ]); } }
if (sumX>255) sumX=255; if (sumX<0) sumX=0;
for (I=-1;I<=1;I++) { for (J=-1;J<=1;J++) { sumY=sumY+(int)((*(originalImage.data+X+I+(Y+J)*originalImage.cols))*GY[I+1][J+1 ]); } }
if (sumY>255) sumY=255; if (sumY<0) sumY=0;
SUM=abs(sumX)+abs(sumY); }
*(edgeImage.data+X+Y*originalImage.cols)=255-(unsigned char)(SUM); fwrite((edgeImage.data+X+Y*originalImage.cols),sizeof(char),1,bmpOutput); } } printf("See edgeSob.bmp for results\n"); fclose(bmpInput); fclose(bmpOutput); farfree(edgeImage.data); farfree(originalImage.data); return 0; }
// ********************************************
long getImageInfo (FILE* inputFile,long offset, int numberOfChars) { unsigned char dummy,*ptrC; long value=0L; int i;
dummy='0'; ptrC=&dummy; fseek(inputFile,offset,SEEK_SET);
for (i=1;i<=numberOfChars;i++) { fread(ptrC,sizeof(char),1,inputFile); value=(long)(value+(*ptrC)*(pow(256,(i-1)))); } }
//**********************************************
void copyImageInfo (FILE* inputFile,FILE* outputFile) { unsigned char dummy,*ptrC; int i;
dummy='0'; ptrC=&dummy; fseek(inputFile,0L,SEEK_SET); fseek(outputFile,0L,SEEK_SET);
for (i=0;i<=50;i++) { fread(ptrC,sizeof(char),1,inputFile); fwrite(ptrC,sizeof(char),1,outputFile); } }
// **************************************************
void copyColorTable (FILE* inputFile,FILE* outputFile, int nColors) { unsigned char dummy,*ptrC; int i;
dummy='0'; ptrC=&dummy; fseek(inputFile,54L,SEEK_SET); fseek(outputFile,54L,SEEK_SET);
for (i=0;i<=(4*nColors);i++) { fread(ptrC,sizeof(char),1,inputFile); fwrite(ptrC,sizeof(char),1,outputFile); } }
|
|
|
|
|
|
Jun 19 2008, 01:50 PM
|

Yahoo! Baby

Group: Member
Posts: 44
Cash: 44k
Age: 21
Joined: 2-December 05
From: Hà Nội
Member No.: 9,861
Yahoo! Status: 

|
QUOTE (Thiên Hà @ Jun 19 2008, 11:33 AM)  chỗ int main(...) sửa thành void main(). Kết thúc hàm main() có chỗ return 0; sửa thành getch(); đâu có được j đâu ThiênHa bạn có thể nói rõ hơn cho mình được không vì chương trình thì chạy rồi nhưng không ra kết quả. Mình không hiểu làm sao để đưa ảnh vào và xuất ảnh ra. Đây là bài tìm biên của ảnh. Nếu mình không nhầm thì nó sẽ view ảnh ra dưới dạng ma trận pixel sau đó sử dụng mặt nạ Sobel để tìm biên của ảnh rồi in ra ma trận biên tìm được. Vấn đề ở chỗ không đưa được ảnh vào bạn à. Giúp mình với. Cảm ơn các bạn.
|
|
|
|
|
|
Jun 19 2008, 01:54 PM
|

Yahoo! Baby

Group: Member
Posts: 44
Cash: 44k
Age: 21
Joined: 2-December 05
From: Hà Nội
Member No.: 9,861
Yahoo! Status: 

|
QUOTE (tst @ Jun 19 2008, 01:50 PM)  QUOTE (Thiên Hà @ Jun 19 2008, 11:33 AM)  chỗ int main(...) sửa thành void main(). Kết thúc hàm main() có chỗ return 0; sửa thành getch(); đâu có được j đâu ThiênHa bạn có thể nói rõ hơn cho mình được không vì chương trình thì chạy rồi nhưng không ra kết quả. Mình không hiểu làm sao để đưa ảnh vào và xuất ảnh ra. Đây là bài tìm biên của ảnh. Nếu mình không nhầm thì nó sẽ view ảnh ra dưới dạng ma trận pixel sau đó sử dụng mặt nạ Sobel để tìm biên của ảnh rồi in ra ma trận biên tìm được. Vấn đề ở chỗ không đưa được ảnh vào bạn à. Giúp mình với. Cảm ơn các bạn. có phải cần tạo ra 1 file bmpInput.bmp không mình không biết phải tạo như thế nào nó có nội dung ra sao? Khó quá. Chắc do code này không phải mình code nên không hiểu hết được.
|
|
|
|
|
|
Jun 19 2008, 08:53 PM
|

Yahoo! King

Group: Admin
Posts: 1,044
Cash: 95k
Age: 24
Joined: 19-June 06
From: vũ trụ
Member No.: 14,720
Yahoo! Status: 

|
code này mình xem thì hiểu sơ sơ hình như nó có tác dụng là lấy 1 phần tấm ảnh này qua 1 tấm ảnh khác, đồng thời nó lấy luôn nội dung của tấm ảnh(chiều cao, độ rộng). Bạn đang học sử lí ảnh ah. Ngay chỗ CODE bmpInput=fopen(argv[1],"rb"); bạn hãy thay argv[1] thành đường dẫn file ảnh của bạn
|
|
|
|
|
|
Jun 19 2008, 09:25 PM
|

Yahoo! Baby

Group: Member
Posts: 44
Cash: 44k
Age: 21
Joined: 2-December 05
From: Hà Nội
Member No.: 9,861
Yahoo! Status: 

|
QUOTE (Thiên Hà @ Jun 19 2008, 08:53 PM)  code này mình xem thì hiểu sơ sơ hình như nó có tác dụng là lấy 1 phần tấm ảnh này qua 1 tấm ảnh khác, đồng thời nó lấy luôn nội dung của tấm ảnh(chiều cao, độ rộng). Bạn đang học sử lí ảnh ah. Ngay chỗ CODE bmpInput=fopen(argv[1],"rb"); bạn hãy thay argv[1] thành đường dẫn file ảnh của bạn cám ơn đã đưa ra ý kiến. Cách hiểu của bạn đúng rồi đấy đây là code sử dụng thuật toán Sobel để lấy biên của ảnh cái mà bạn nói là "1 phần tấm ảnh" là ma trận Sobel 3x3. Còn thay thế argv[1] thì không được. Vẫn bị mắc ở chỗ này đấy. Chán thật. Xử lý ảnh không hề đơn giản chút nào.
|
|
|
|
|
|
Jun 20 2008, 05:17 AM
|

Yahoo! Baby

Group: Member
Posts: 44
Cash: 44k
Age: 21
Joined: 2-December 05
From: Hà Nội
Member No.: 9,861
Yahoo! Status: 

|
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <alloc.h> #include <conio.h>
typedef struct {int rows; int cols; unsigned char* data;} sImage;
long thongtinanh(FILE*, long, int); void copythongtinanh(FILE* inputFile, FILE* outputFile); void copybangmau(FILE* inputFile, FILE* outputFile, int nColors);
void main(int argc, char* argv[]) { FILE *bmpInput, *bmpOutput; sImage anhgoc; sImage anhbien; unsigned int X, Y; int I, J; long sumX, sumY; int nColors, SUM; unsigned long vectorSize; unsigned long fileSize; int GX[3][3]; int GY[3][3]; unsigned char *pChar, someChar; unsigned int row, col;
someChar = '0'; pChar = &someChar;
/* 3x3 GX Mat na Sobel.*/
GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1; GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2; GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;
/* 3x3 GY Mat na Sobel.*/
GY[0][0] = 1; GY[0][1] = 2; GY[0][2] = 1; GY[1][0] = 0; GY[1][1] = 0; GY[1][2] = 0; GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;
if(argc < 2) { printf("Su dung : %s bmpInput.bmp\n", argv[0]); exit(0); }; printf("Dang doc file %s\n", argv[1]);
/*-------INPUT & OUTPUT FILES-------*/
bmpInput = fopen(argv[1], "rb"); bmpOutput = fopen("xla.bmp", "wb");
/*---SET POINTER TO BEGINNING OF FILE----*/
fseek(bmpInput, 0L, SEEK_END);
/*-------GET INPUT BMP DATA--------*/
fileSize = thongtinanh(bmpInput, 2, 4); anhgoc.cols = (int)thongtinanh(bmpInput, 18, 4); anhgoc.rows = (int)thongtinanh(bmpInput, 22, 4); anhbien.rows = anhgoc.rows; anhbien.cols = anhgoc.cols;
/*--------PRINT DATA TO SCREEN----------*/
printf("Chieu rong : %d\n", anhgoc.cols); printf("Chieu cao : %d\n", anhgoc.rows); printf("Co anh : %lu\n", fileSize); nColors = (int)thongtinanh(bmpInput, 46, 4); printf("nColors : %d\n", nColors);
/*------ALLOCATE MEMORY FOR FILES--------*/
vectorSize = fileSize - (14+40+4*nColors); printf("vectorSize : %lu\n", vectorSize); anhbien.data =(unsigned char*)farmalloc(vectorSize*sizeof(unsigned char)); if(anhbien.data == NULL) { printf("Failed to malloc anhbien.data\n"); exit(0); } printf("%lu bytes malloc'ed for anhbien.data\n", vectorSize);
anhgoc.data =(unsigned char*)farmalloc(vectorSize*sizeof(unsigned char)); if(anhgoc.data == NULL) { printf("Failed to malloc anhgoc.data\n"); exit(0); } printf("%lu bytes malloc'ed for anhgoc.data\n", vectorSize);
/*------COPY HEADER AND COLOR TABLE---------*/
copythongtinanh(bmpInput, bmpOutput); copybangmau(bmpInput, bmpOutput, nColors); fseek(bmpInput, (14+40+4*nColors), SEEK_SET); fseek(bmpOutput, (14+40+4*nColors), SEEK_SET);
/* Doc input.bmp luu thong tin toi anhgoc.data */
for(row=0; row<=anhgoc.rows-1; row++) { for(col=0; col<=anhgoc.cols-1; col++) { fread(pChar, sizeof(char), 1, bmpInput); *(anhgoc.data + row*anhgoc.cols + col) = *pChar; } }
/*--------------------------------------------------- CAC PHEP TOAN SOBEL ---------------------------------------------------*/ for(Y=0; Y<=(anhgoc.rows-1); Y++) { for(X=0; X<=(anhgoc.cols-1); X++) { sumX = 0; sumY = 0;
/* image boundaries */
if(Y==0 || Y==anhgoc.rows-1) SUM = 0; else if(X==0 || X==anhgoc.cols-1) SUM = 0; /* Convolution starts here */ else {
/*-------X GRADIENT APPROXIMATION------*/
for(I=-1; I<=1; I++) { for(J=-1; J<=1; J++) { sumX = sumX + (int)( (*(anhgoc.data + X + I + (Y + J)*anhgoc.cols)) * GX[I+1][J+1]); } }
/*-------Y GRADIENT APPROXIMATION-------*/
for(I=-1; I<=1; I++) { for(J=-1; J<=1; J++) { sumY = sumY + (int)( (*(anhgoc.data + X + I + (Y + J)*anhgoc.cols)) * GY[I+1][J+1]); } }
/*---GRADIENT MAGNITUDE APPROXIMATION (Myler p.218)----*/
SUM = abs(sumX) + abs(sumY); } if(SUM>255) SUM=255; if(SUM<0) SUM=0; *(anhbien.data + X + Y*anhgoc.cols) = 255 - (unsigned char)(SUM); fwrite((anhbien.data + X + Y*anhgoc.cols),sizeof(char),1,bmpOutput); } }
printf("Xet xla.bmp cho ket qua \n"); fclose(bmpInput); fclose(bmpOutput); farfree(anhbien.data); /* Ket thuc anhbien.data */ farfree(anhgoc.data); /* Ket thuc anhgoc.data */ getch(); }
/*----------GET IMAGE INFO SUBPROGRAM--------------*/
long thongtinanh(FILE* inputFile, long offset, int numberOfChars) { unsigned char *ptrC; long value = 0L; unsigned char dummy; int i;
dummy = '0'; ptrC = &dummy;
fseek(inputFile, offset, SEEK_SET);
for(i=1; i<=numberOfChars; i++) { fread(ptrC, sizeof(char), 1, inputFile); value = (long)(value + (*ptrC)*(pow(256, (i-1)))); /* Tinh gia tri co ban tren cac byte them vao */ } return(value);
} /* Ket thuc thongtinanh */
/*-------------COPIES HEADER AND INFO HEADER----------------*/
void copythongtinanh(FILE* inputFile, FILE* outputFile) { unsigned char *ptrC; unsigned char dummy; int i;
dummy = '0'; ptrC = &dummy;
fseek(inputFile, 0L, SEEK_SET); fseek(outputFile, 0L, SEEK_SET);
for(i=0; i<=50; i++) { fread(ptrC, sizeof(char), 1, inputFile); fwrite(ptrC, sizeof(char), 1, outputFile); }
}
/*----------------COPIES COLOR TABLE-----------------------------*/
void copybangmau(FILE* inputFile, FILE* outputFile, int nColors) { unsigned char *ptrC; unsigned char dummy; int i;
dummy = '0'; ptrC = &dummy;
fseek(inputFile, 54L, SEEK_SET); fseek(outputFile, 54L, SEEK_SET);
for(i=0; i<=(4*nColors); i++) { fread(ptrC, sizeof(char), 1, inputFile); fwrite(ptrC, sizeof(char), 1, outputFile); }
}
Bạn có để ý không có vẻ như cần có 1 bien.exe để lấy thông tin từ ảnh rồi mới gọi chương trình này để in ra thông tin về biên của ảnh. Vậy cái bien.exe là như thế nào đây?????????
|
|
|
|
|
|
Jun 20 2008, 08:34 AM
|

Yahoo! King

Group: Admin
Posts: 1,044
Cash: 95k
Age: 24
Joined: 19-June 06
From: vũ trụ
Member No.: 14,720
Yahoo! Status: 

|
lấy code của người khác thì bạn nên sửa lại thành của mình. Nếu bạn dùng c++ thì hàm main nên không đối vào CODE void main() { //code } thay argv[1] thành đường dẫn file ảnh của bạn. Bỏ tất cả biến argv và argc . Nếu bạn dùng biến argv thì hình như bạn phải bỏ file ảnh cùng thư mục với file chạy exe
|
|
|
|
1 User(s) are reading this topic [1 Guests and 0 Anonymous Users]
0 Members:

Similar Topics
Similar Topics

Similar Topics

Links to this thread
Links to this thread

Links to this thread

Powered By IP.Board
2.3.6 © 2008 IPS, Inc.
VNP © 2008 :: Founded by PETER :: Developed by VNP's members.
|
|