图像处理基本方法-将BMP图片灰度化-c语言实现

c语言实现图片灰度化,主要利用公式Gray = R0.299 + G0.587 + B*0.114实现。

实现函数为bmpGrayProccess,该函数主要使用上述公式实现图片的灰度化。

具体函数名称。

int bmpGrayProccess(char u8PicNameiIn[], char u8PicNameOut[],int mode)

其中

u8PicNameiIn为输入图像名称。

u8PicNameiOut为输入图像名称。

mode为算法方式。

关键代码

u8Tmp = u8R * 0.299 + u8G * 0.587 + u8B * 0.114;

程序代码

/*******************************************************
* file:testGray.c
* date:2021-06-26
* version:1.0.0.1
* author:jack8126
* description: bmp file, change to gray picture
*******************************************************/


#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>

#define PRINT_CTRL		 	1

typedef unsigned char       WORD8;       /* 8位无符号整型   */
typedef unsigned short      WORD16; 	 /* 16位无符号整型  */
typedef unsigned int        WORD32; 	 /* 32位无符号整型  */
typedef unsigned char       BYTE;        /* 8位无符号整型   */
typedef signed int          INT;         /* 32位有符号整型   */
typedef signed char         CHAR;        /* 8位有符号字符型  */		
typedef signed long         LONG;        /* 32位有符号长整型 */
typedef unsigned long       uLONG;       /* 32位无符号长整型 */
typedef signed short	    SHORT;       /* 16位有符号整型  */
typedef void                *LPVOID;     /* 空类型           */
typedef unsigned char       BOOLEAN;     /* 布尔类型        */

#pragma pack(push)
#pragma pack(1)


typedef struct INFOHEADER  
{   
    WORD32 biSize;   
    WORD32 biWidth;   
    WORD32 biHeight;   
    WORD16 biPlanes;   
    WORD16 biBitCount;   
    WORD32 biCompression;   
    WORD32 biSizeImage;   
    WORD32 biXPelsPerMeter;   
    WORD32 biYPelsPerMeter;   
    WORD32 biClrUsed;   
    WORD32 biClrImportant;   
}BITMAPINFOHEADER; 

typedef struct tagBITMAPFILEHEADER
{
    WORD16 bfType; 
    WORD32 bfSize;
    WORD16 bfReserved1;
    WORD16 bfReserved2;
    WORD32 bfOffBits;
} BITMAPFILEHEADER;
#pragma pack(pop)


int bmpGrayProccess(char u8PicNameiIn[], char u8PicNameOut[],int mode)
{
    INT ptn1Fd;
	
    INT fileLong = 0;
	
    BITMAPFILEHEADER bmp_file_header_save = {0};
    BITMAPINFOHEADER bmp_info_header_save = {0};
	
    //FILE* fdRead = NULL;
    FILE* fdWrite = NULL;
	
    int bmpSize = 0;

    INT nWriteLen = 0;
    BYTE *ptnBufRead = NULL;
    INT readLen = 0;
	
    INT i = 0;
    INT j = 0;

    INT iWidth = 0;
    INT iHeight = 0;
    INT iBmpBit = 3;
    int iBmpSize = 0;
    BYTE u8R = 0;    
    BYTE u8G = 0; 
    BYTE u8B = 0; 
	BYTE u8Tmp = 0;
	int ntmp = 0;
	
    if ((access(u8PicNameiIn, F_OK))!= 0)
    {		
        printf("%s not found\n", u8PicNameiIn);
        return -1;
    }

    // read bmp input file
    ptn1Fd = open(u8PicNameiIn, O_RDONLY);
    printf("open file name %s ptn1Fd %d \n",u8PicNameiIn,ptn1Fd);
    if (ptn1Fd < 0)
    {
        printf("open %s error\n", u8PicNameiIn);
        return 1;
    }

    fileLong = lseek(ptn1Fd, 0, SEEK_END);
    printf("the pic size fileLong is %d.\r\n",fileLong);

    bmpSize = fileLong - 54; 
    iBmpSize = bmpSize;
    
    lseek(ptn1Fd, 0, SEEK_SET);
    readLen = read(ptn1Fd, &bmp_file_header_save, 14);

    lseek(ptn1Fd, 14, SEEK_SET);
    readLen = read(ptn1Fd, &bmp_info_header_save, 40);
    lseek(ptn1Fd, 54, SEEK_SET);

    printf("read bmp_header.biHeight=%d,biWidth=%d\r\n",
        bmp_info_header_save.biHeight,bmp_info_header_save.biWidth); 

    nWriteLen = bmp_info_header_save.biHeight*bmp_info_header_save.biWidth*3;
    printf("nWriteLen = %d\n\r",nWriteLen);

    iWidth  = bmp_info_header_save.biWidth;
    iHeight = bmp_info_header_save.biHeight;
 
    printf("bmpSize=%d\r\n",bmpSize);
    ptnBufRead = (BYTE *)malloc(bmpSize);
    if (ptnBufRead == NULL)
    {
        printf("malloc ptnBuf failed.\r\n");
        close(ptn1Fd);
        return -1;
    }
    memset(ptnBufRead, 0, bmpSize);

    // read bmp file
    readLen = read(ptn1Fd, ptnBufRead, bmpSize);

	printf("readLen=%d\r\n",readLen);
    // draw a rectangle
    #if 1
    for (i = 0; i < iHeight; i++) 
    { 
        for (j = 0; j< iWidth; j++) 
        {
			ntmp = (iHeight -1 -  i)*3*iWidth + (j )*3;
			u8B = ptnBufRead[ntmp + 0];
			u8G = ptnBufRead[ntmp + 1];
			u8R = ptnBufRead[ntmp + 2];
			
			if(mode == 0)
			{
				u8Tmp = u8R * 0.299 + u8G * 0.587 + u8B * 0.114;
			}
			else if(mode == 1)
			{
				u8Tmp = (u8R * 299 + u8G * 587 + u8B * 114) / 1000;
			}
			else
			{
				u8Tmp = (u8R + u8G  + u8B) / 3;
			}
			
			ptnBufRead[ntmp + 0] = u8Tmp;
			ptnBufRead[ntmp + 1] = u8Tmp;
			ptnBufRead[ntmp + 2] = u8Tmp;
        }
    }
    #endif
	printf(" for out ntmp=%d\r\n",ntmp);
	
    // save output file
    fdWrite = fopen(u8PicNameOut, "w+");
	
    // write bmp file header
    fwrite(&bmp_file_header_save, 1, 14, fdWrite);
    fseek(fdWrite,0,SEEK_END);

    // write bmp info header
    fwrite(&bmp_info_header_save, 1, 40, fdWrite);
    //lseek(ptn1Fd, 54, SEEK_SET);
    fseek(fdWrite,0,SEEK_END);
    
    // write bmp data
    fwrite(ptnBufRead, 1, nWriteLen, fdWrite);

    // release
    if(NULL != ptnBufRead)
    {
        free(ptnBufRead);
        ptnBufRead = NULL;
    }

    if(ptn1Fd)
    {
        close(ptn1Fd);
    }

    //fclose(fdRead);

    if(fdWrite)
    {
        fclose(fdWrite);
        fdWrite = NULL;
    }

}

//主函数
int main(int argc, char * * argv)
{
    BYTE u8PicNameRead[64] = {0}; 
    BYTE u8PicNameOut[64] = {0}; 
    int iBmpBit = 3;
    int iBmpBitTmp = 0;
    struct timeval tStartTime;
    struct timeval tEndTime;
    int isecond = 0;
    int iusecond = 0;
	int mode = 0;
    
    if(argc < 4)
    {
        printf("please input like this:\r\n");
        printf("./testGray.bin test.bmp test-out.bmp 0 \r\n");
        printf("test.bmp       ---------- input file \r\n");
        printf("test-out.bmp   ---------- output file \r\n");
		printf("0   --------------------- mode 0, 1, 2 \r\n");
        return -1;
    }
    printf("argv[1]=%s\r\n",argv[1]);
    printf("argv[2]=%s,\r\n",argv[2]);

    sprintf(u8PicNameRead,"%s",argv[1]);
    sprintf(u8PicNameOut,"%s",argv[2]);
	mode			= atoi(argv[3]);

    printf("u8PicNameRead=%s\r\n",u8PicNameRead);
	printf("mode			=%d\r\n",mode);

    
    gettimeofday(&tStartTime, NULL);
    //printf("tStartTime time.tv_sec = %d, time.tv_usec = %d.\r\n", tStartTime.tv_sec, tStartTime.tv_usec);

    //generateBmpFunc(u8PicNameOut,iWidth,iHeigth,NULL,iBmpBit,u8R,u8G,u8B);
    bmpGrayProccess(u8PicNameRead,u8PicNameOut,mode);
	
    gettimeofday(&tEndTime, NULL);
    if(tEndTime.tv_sec >= tStartTime.tv_sec)
    {
        isecond = tEndTime.tv_sec-tStartTime.tv_sec;
    }
    if(tEndTime.tv_usec >= tStartTime.tv_usec)
    {
        iusecond = tEndTime.tv_usec-tStartTime.tv_usec;
    }
    else
    {
        isecond--;
        iusecond = tEndTime.tv_usec-tStartTime.tv_usec + 1000000;
    }
    //printf("tEndTime time.tv_sec = %d, time.tv_usec = %d.\r\n", tEndTime.tv_sec, tEndTime.tv_usec);
    printf("make gray use time tv_sec=%ds, ms=%d, tv_usec=%dus\r\n",isecond,iusecond/1000,iusecond);

    printf("u8PicNameOut=%s\r\n",u8PicNameOut);
    
    return 0; 
}


编译程序

编译命令如下

gcc testGray.c -o testGray.bin

执行完上述命令之后,会生成testGray.bin文件,后面执行程序时需要该文件。

在这里插入图片描述

执行程序

使用如下命令执行程序,可将bmp图片转换为灰度图片。

./testGray.bin image.bmp  image-out.bmp 0

执行完上述命令后,生成image-out.bmp文件。
在这里插入图片描述

原图

在这里插入图片描述

灰度图片

在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐