您好,登錄后才能下訂單哦!
小編給大家分享一下C語言如何實現(xiàn)BMP格式圖片轉(zhuǎn)化為灰度,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
代碼如下:
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #pragma pack(1) typedef struct tagBITMAPFILEHEADER { unsigned char bfType[2];//文件格式 unsigned long bfSize;//文件大小 unsigned short bfReserved1;//保留 unsigned short bfReserved2; unsigned long bfOffBits; //DIB數(shù)據(jù)在文件中的偏移量 }fileHeader; #pragma pack() /* 位圖數(shù)據(jù)信息結(jié)構(gòu) */ #pragma pack(1) typedef struct tagBITMAPINFOHEADER { unsigned long biSize;//該結(jié)構(gòu)的大小 long biWidth;//文件寬度 long biHeight;//文件高度 unsigned short biPlanes;//平面數(shù) unsigned short biBitCount;//顏色位數(shù) unsigned long biCompression;//壓縮類型 unsigned long biSizeImage;//DIB數(shù)據(jù)區(qū)大小 long biXPixPerMeter; long biYPixPerMeter; unsigned long biClrUsed;//多少顏色索引表 unsigned long biClrImporant;//多少重要顏色 }fileInfo; #pragma pack() /* 調(diào)色板結(jié)構(gòu) */ #pragma pack(1) typedef struct tagRGBQUAD { unsigned char rgbBlue; //藍色分量亮度 unsigned char rgbGreen;//綠色分量亮度 unsigned char rgbRed;//紅色分量亮度 unsigned char rgbReserved; }rgbq; #pragma pack() int main() { FILE *fp1 = fopen("C:\\Users\\Administrator\\Desktop\\data\\bmp\\image.bmp", "rb+"); if (fp1 == NULL) { printf("打開文件fp1失敗"); exit(0); } FILE *fp2 = fopen("C:\\Users\\Administrator\\Desktop\\data\\bmp\\imageGray.bmp", "wb"); if (fp1 == NULL) { printf("打開文件fp2失敗"); exit(0); } fileHeader * fh; fileInfo * fi; fh = (fileHeader *)malloc(sizeof(fileHeader)); fi = (fileInfo *)malloc(sizeof(fileInfo)); //讀取位圖頭結(jié)構(gòu)和信息頭 fread(fh, sizeof(fileHeader), 1, fp1); fread(fi, sizeof(fileInfo), 1, fp1); printf("\\\\\\\\\\\\\\\\\\\\原始圖片信息\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp文件頭:\n"); printf("bfSize:%d\n", fh->bfSize); printf("bfOffBits:%d\n", fh->bfOffBits); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp信息頭\n"); printf("結(jié)構(gòu)體長度:%d \n", fi->biSize); printf("位圖寬度:%d \n", fi->biWidth); printf("位圖高度:%d \n", fi->biHeight); printf("位圖平面數(shù):%d \n", fi->biPlanes); printf("顏色位數(shù):%d \n", fi->biBitCount); printf("壓縮方式:%d \n", fi->biCompression); printf("實際位圖數(shù)據(jù)占用的字節(jié)數(shù):%d \n", fi->biSizeImage); printf("X方向分辨率:%d \n", fi->biXPixPerMeter); printf("Y方向分辨率:%d \n", fi->biYPixPerMeter); printf("使用的顏色數(shù):%d \n", fi->biClrUsed); printf("重要顏色數(shù):%d \n", fi->biClrImporant); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); //修改信息頭 fi->biBitCount = 8; //fi->biSizeImage = ((fi->biWidth * 3 + 3) / 4) * 4 * fi->biHeight; fi->biSizeImage = fi->biHeight*fi->biWidth; //修改文件頭 fh->bfOffBits = sizeof(fileHeader) + sizeof(fileInfo) + 256 * sizeof(rgbq); fh->bfSize = fh->bfOffBits + fi->biSizeImage; printf("\\\\\\\\\\\\\\\\\\\\修改后的圖片信息\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp文件頭:\n"); printf("bfSize:%d\n", fh->bfSize); printf("bfOffBits:%d\n", fh->bfOffBits); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp信息頭\n"); printf("結(jié)構(gòu)體長度:%d \n", fi->biSize); printf("位圖寬度:%d \n", fi->biWidth); printf("位圖高度:%d \n", fi->biHeight); printf("位圖平面數(shù):%d \n", fi->biPlanes); printf("顏色位數(shù):%d \n", fi->biBitCount); printf("壓縮方式:%d \n", fi->biCompression); printf("實際位圖數(shù)據(jù)占用的字節(jié)數(shù):%d \n", fi->biSizeImage); printf("X方向分辨率:%d \n", fi->biXPixPerMeter); printf("Y方向分辨率:%d \n", fi->biYPixPerMeter); printf("使用的顏色數(shù):%d \n", fi->biClrUsed); printf("重要顏色數(shù):%d \n", fi->biClrImporant); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); //創(chuàng)建調(diào)色板 int i,j,k=0; rgbq *fq = (rgbq *)malloc(256 * sizeof(rgbq)); for (i = 0; i<256; i++) { fq[i].rgbBlue = fq[i].rgbGreen = fq[i].rgbRed = i; } //寫入文件頭、信息頭、調(diào)色板 fwrite(fh, sizeof(fileHeader), 1, fp2); fwrite(fi, sizeof(fileInfo), 1, fp2); fwrite(fq, sizeof(rgbq), 256, fp2); //將位圖信息轉(zhuǎn)為灰度 //存儲bmp一行的像素點 //unsigned char ImgData[900][3]; unsigned char ImgData[3000][3]; //將灰度圖像存到一維數(shù)組中 //unsigned char grayData2[900]; unsigned char ImgData2[3000]; /* //錯誤的算法 for (i = 0; i < fi->biHeight; i++) { for (j = 0; j < (fi->biWidth * 3 + 3) / 4 * 4; j++) { for (k = 0; k < 3; k++) { fread(&ImgData[j][k], 1, 1, fp1); } } for (j = 0; j < (fi->biWidth + 3) / 4 * 4; j++) { ImgData2[j] = int((float)ImgData[j][0] * 0.114 + (float)ImgData[j][1] * 0.587 + (float)ImgData[j][2] * 0.299); } //將灰度圖信息寫入 fwrite(ImgData2, j, 1, fp2); } */ /* //正確的算法(1) for (i = 0; i<fi->biHeight; i++) { for (j = 0; j<(fi->biWidth + 3) / 4 * 4; j++) { for (k = 0; k<3; k++) fread(&ImgData[j][k], 1, 1, fp1); } for (j = 0; j<(fi->biWidth + 3) / 4 * 4; j++) { ImgData2[j] = int((float)ImgData[j][0] * 0.114 + (float)ImgData[j][1] * 0.587 + (float)ImgData[j][2] * 0.299); } //將灰度圖信息寫入 fwrite(ImgData2, j, 1, fp2); } */ //正確算法(2) unsigned char * * bmp_data; bmp_data = new unsigned char*[fi->biHeight]; //聲明一個指針數(shù)組 unsigned char *data288 = new unsigned char[fi->biHeight*fi->biWidth]; for (i = 0; i<fi->biHeight; i++) bmp_data[i] = new unsigned char[(fi->biWidth * 3 + 3) / 4 * 4]; //每個數(shù)組元素也是一個指針數(shù)組 for (i = 0; i<fi->biHeight; i++) for (j = 0; j<(fi->biWidth * 3 + 3) / 4 * 4; j++) fread(&bmp_data[i][j], 1, 1, fp1);//每次只讀取一個字節(jié),存入數(shù)組 for (i = 0; i<fi->biHeight; i++)//將24位真彩色轉(zhuǎn)換成灰度圖 for (j = 0; j<fi->biWidth; j++){ data288[fi->biWidth*i + j] = ((unsigned char)((float)bmp_data[i][3 * j] * 0.114 + (float)bmp_data[i][3 * j + 1] * 0.587 + (float)bmp_data[i][3 * j + 2] * 0.299)); } fwrite(data288, fi->biSizeImage, 1, fp2); free(fh); free(fi); free(fq); fclose(fp1); fclose(fp2); printf("success\n"); return 0; }
看完了這篇文章,相信你對“C語言如何實現(xiàn)BMP格式圖片轉(zhuǎn)化為灰度”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。