您好,登錄后才能下訂單哦!
小編給大家分享一下C語言如何實現BMP格式圖片轉化為灰度,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
代碼如下:
#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數據在文件中的偏移量 }fileHeader; #pragma pack() /* 位圖數據信息結構 */ #pragma pack(1) typedef struct tagBITMAPINFOHEADER { unsigned long biSize;//該結構的大小 long biWidth;//文件寬度 long biHeight;//文件高度 unsigned short biPlanes;//平面數 unsigned short biBitCount;//顏色位數 unsigned long biCompression;//壓縮類型 unsigned long biSizeImage;//DIB數據區大小 long biXPixPerMeter; long biYPixPerMeter; unsigned long biClrUsed;//多少顏色索引表 unsigned long biClrImporant;//多少重要顏色 }fileInfo; #pragma pack() /* 調色板結構 */ #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)); //讀取位圖頭結構和信息頭 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("結構體長度:%d \n", fi->biSize); printf("位圖寬度:%d \n", fi->biWidth); printf("位圖高度:%d \n", fi->biHeight); printf("位圖平面數:%d \n", fi->biPlanes); printf("顏色位數:%d \n", fi->biBitCount); printf("壓縮方式:%d \n", fi->biCompression); printf("實際位圖數據占用的字節數:%d \n", fi->biSizeImage); printf("X方向分辨率:%d \n", fi->biXPixPerMeter); printf("Y方向分辨率:%d \n", fi->biYPixPerMeter); printf("使用的顏色數:%d \n", fi->biClrUsed); printf("重要顏色數:%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("結構體長度:%d \n", fi->biSize); printf("位圖寬度:%d \n", fi->biWidth); printf("位圖高度:%d \n", fi->biHeight); printf("位圖平面數:%d \n", fi->biPlanes); printf("顏色位數:%d \n", fi->biBitCount); printf("壓縮方式:%d \n", fi->biCompression); printf("實際位圖數據占用的字節數:%d \n", fi->biSizeImage); printf("X方向分辨率:%d \n", fi->biXPixPerMeter); printf("Y方向分辨率:%d \n", fi->biYPixPerMeter); printf("使用的顏色數:%d \n", fi->biClrUsed); printf("重要顏色數:%d \n", fi->biClrImporant); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); //創建調色板 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; } //寫入文件頭、信息頭、調色板 fwrite(fh, sizeof(fileHeader), 1, fp2); fwrite(fi, sizeof(fileInfo), 1, fp2); fwrite(fq, sizeof(rgbq), 256, fp2); //將位圖信息轉為灰度 //存儲bmp一行的像素點 //unsigned char ImgData[900][3]; unsigned char ImgData[3000][3]; //將灰度圖像存到一維數組中 //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]; //聲明一個指針數組 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]; //每個數組元素也是一個指針數組 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);//每次只讀取一個字節,存入數組 for (i = 0; i<fi->biHeight; i++)//將24位真彩色轉換成灰度圖 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語言如何實現BMP格式圖片轉化為灰度”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。