In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Xiaobian to share with you how C language BMP format image conversion to grayscale, I hope you have something to gain after reading this article, let's discuss it together!
The code is as follows:
#include#include#include #pragma pack(1)typedef struct tagBITMAPFILEHEADER{ unsigned char bfType[2];//file format unsigned long bfSize;//file size unsigned short bfReserved1;//Reserved unsigned short bfReserved2; unsigned long bfOffBits; //DIB Offset of data in file}fileHeader;#pragma pack()/* Bitmap data information structure */#pragma pack(1)typedef struct tagBITMAPINFOHEADER{ unsigned long biSize;//the size of the structure long biWidth;//file width long biHeight;//file height unsigned short biPlanes;//Number of planes unsigned short biBitCount;//color digits unsigned long biCompression;//Compression type unsigned long biSizeImage;//DIB data area size long biXPixPerMeter; long biYPixPerMeter; unsigned long biClrUsed;//How many color index tables unsigned long biClrImporant;//how many important colors}fileInfo;#pragma pack()/* palette structure */#pragma pack(1)typedef struct tagRGBQUAD{ unsigned char rgbBlue; //Blue component brightness unsigned char rgbGreen;//Green component brightness unsigned char rgbRed;//Red component brightness unsigned char rgbReserved;}rgbq;#pragma pack() int main(){ FILE *fp1 = fopen("C:\\Users\\Administrator\\Desktop\\data\\bmp\\image.bmp", "rb+"); if (fp1 == NULL) { printf("Failed to open file fp1"); exit(0); } FILE *fp2 = fopen("C:\\Users\\Administrator\\Desktop\\data\\bmp\\imageGray.bmp", "wb"); if (fp1 == NULL) { printf("Failed to open file fp2"); exit(0); } fileHeader * fh; fileInfo * fi; fh = (fileHeader *)malloc(sizeof(fileHeader)); fi = (fileInfo *)malloc(sizeof(fileInfo)); //Read bitmap header structure and information header fread(fh, sizeof(fileHeader), 1, fp1); fread(fi, sizeof(fileInfo), 1, fp1); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\ printf("bmp file header: \n"); printf("bfSize:%d\n", fh->bfSize); printf("bfOffBits:%d\n", fh->bfOffBits); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp header\n"); printf("struct length: %d \n", fi->biSize); printf("Bitmap Width: %d \n", fi->biWidth); printf("bitmap height: %d \n", fi->biHeight); printf("bitmap planes: %d \n", fi->biPlanes); printf("Color digits: %d \n", fi->biBitCount); printf("Compression method: %d \n", fi->biCompression); printf("actual bitmap data bytes: %d \n", fi->biSizeImage); printf("X direction resolution: %d \n", fi->biXPixPerMeter); printf("Y Resolution: %d \n", fi->biYPixPerMeter); printf("Number of colors used: %d \n", fi->biClrUsed); printf("Number of important colors: %d \n", fi->biClrImporant); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); //modify header fi->biBitCount = 8; //fi->biSizeImage = ((fi->biWidth * 3 + 3) / 4) * 4 * fi->biHeight; fi->biSizeImage = fi->biHeight*fi->biWidth; //modify file header fh->bfOffBits = sizeof(fileHeader) + sizeof(fileInfo) + 256 * sizeof(rgbq); fh->bfSize = fh->bfOffBits + fi->biSizeImage; printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\ printf("bmp file header: \n"); printf("bfSize:%d\n", fh->bfSize); printf("bfOffBits:%d\n", fh->bfOffBits); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp header\n"); printf("struct length: %d \n", fi->biSize); printf("Bitmap Width: %d \n", fi->biWidth); printf("bitmap height: %d \n", fi->biHeight); printf("bitmap planes: %d \n", fi->biPlanes); printf("Color digits: %d \n", fi->biBitCount); printf("Compression method: %d \n", fi->biCompression); printf("actual bitmap data bytes: %d \n", fi->biSizeImage); printf("X direction resolution: %d \n", fi->biXPixPerMeter); printf("Y Resolution: %d \n", fi->biYPixPerMeter); printf("Number of colors used: %d \n", fi->biClrUsed); printf("Number of important colors: %d \n", fi->biClrImporant); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); //Create palette int i,j,k=0; rgbq *fq = (rgbq *)malloc(256 * sizeof(rgbq)); for (i = 0; ibiHeight; 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); } //write gray map information fwrite(ImgData2, j, 1, fp2); } */ /* //correct algorithm (1) for (i = 0; ibiHeight; i++) { for (j = 0; jbiWidth + 3) / 4 * 4; j++) { for (k = 0; kbiHeight]; //Declare an array of pointers unsigned char *data288 = new unsigned char[fi->biHeight*fi->biWidth]; for (i = 0; ibiHeight; i++) bmp_data[i] = new unsigned char[(fi->biWidth * 3 + 3) / 4 * 4]; //Each array element is also a pointer array for (i = 0; ibiHeight; i++) for (j = 0; jbiWidth * 3 + 3) / 4 * 4; j++) fread(&bmp_data[i][j], 1, 1, fp1);//Read only one byte at a time and store it in the array for (i = 0; ibiHeight; i++)//Convert 24-bit true color to grayscale for (j = 0; jbiWidth; 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;}
After reading this article, I believe you have a certain understanding of "C language how to achieve BMP format image conversion to grayscale". If you want to know more about it, welcome to pay attention to the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.