![]() |
|
|
#1 |
|
Gà Con
![]() Tham gia: Jul 2008
Bài: 2
VZD: 185
Cảm ơn: 0
Điểm: 0/0 bài viết
|
Mình muốn mọi người coi giúp code này hoạt động như thế nào? Mình hiểu là code này viết để xử lý ảnh (cụ thể là tìm biên của ảnh). Nó sẽ cho phép mình nhập vào 1 ảnh rồi lấy thông tin của ảnh đó và sử dụng ma trận mặt nạ của thuật toán Sobel để tìm biên. Mọi hình thức hiển thị đểu dười dạng matrix. Nhưng mình ko biết làm sao để đưa ảnh vào và xuất ảnh ra như thế nào? Các bạn coi giúp và cho mình xin ý kiến nhé. Cảm ơn nhiều.
#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); } } |
|
|
|