【圖像處理基礎】C語言對bmp圖片進行處理

1.bmp文件的定義

#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改變圖像亮度
typedef struct BMP
{
	//14字節
	unsigned short bfType; //文件標識 2字節 必須爲BM
	unsigned int bfSize; //文件大小 4字節
	unsigned short bfReserved1; //保留,每字節以"00"填寫 2字節
	unsigned short bfReserved2; //同上 2字節
	unsigned int bfOffBits; //記錄圖像數據區的起始位置(圖象數據相對於文件頭字節的偏移量)。 4字節
	//40字節
	unsigned int biSize; //表示本結構的大小 4字節
	int biWidth; //位圖的寬度 4字節
	int biHeight; //位圖的高度 4字節
	unsigned short biPlanes; //永遠爲1 , 2字節
	unsigned short biBitCount; //位圖的位數 分爲1 4 8 16 24 32 2字節
	unsigned int biCompression; //壓縮說明 4字節
	unsigned int biSizeImage; //表示位圖數據區域的大小以字節爲單位 4字節
	int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字節
	int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字節
	unsigned int biClrUsed; //位圖使用的顏色索引數 4字節
	unsigned int biClrImportant; //對圖象顯示有重要影響的顏色索引的數目 4字節
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif

2.bmp圖片的讀寫以及保存處理

#include<stdio.h>
#include<stdlib.h>
#include"image.h"
void image_info(FILE *file)
{
	int times=3; //輸入文件名次數。
	char bmp_name[10]; //文件名
	printf("\nplease enter a file name for reading:");
	do
	{
		if (times<3)
		{
			printf("\nplease enter a file name for reading again:");
		}
		fflush(stdin);
		gets(bmp_name);
		//printf("\n%s",bmp_name);
		file=fopen(bmp_name,"rb+"); //打開一個文件進行讀寫操作。
		--times;
		if (file==NULL)
		{
			printf("\nerror opening %s for reading! ",bmp_name);
		}
		else
		{
			break;
		}
	}
	while(times!=0);
	if (times==0)
	{
		printf("\nsorry, shutdown!");
		exit(1);
	}
	//讀取圖像信息
	fseek(file,0L,0); //讀取圖像文件類型
	fread(&bmp,sizeof(BMP),1,file);
	printf("\n bmp tpye: %u",bmp.bfType);
	printf("\n bmp size: %u",bmp.bfSize);
	printf("\n bmp reserved1: %u",bmp.bfReserved1);
	printf("\n bmp reserved2: %u",bmp.bfReserved2);
	printf("\n bmp offBits: %u",bmp.bfOffBits);
	printf("\n bmp bisize: %u",bmp.biSize);
	printf("\n bmp biWidth: %d",bmp.biWidth);
	printf("\n bmp biHeight: %d",bmp.biHeight);
	printf("\n bmp biplans: %u",bmp.biPlanes);
	printf("\n bmp biBitCount: %u",bmp.biBitCount);
	printf("\n bmp biCompression: %u",bmp.biCompression);
	printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
	printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
	printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
	printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
	printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);
	line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //獲得圖像數據每行的數據個數
	//printf("dfsa%u",bmp.line_byte);
	//bmp.imagedata=NULL;
	imagedata=(unsigned char*)malloc(bmp.biSizeImage);
	fseek(file,(long)bmp.bfOffBits,0);
	fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
	fclose(file);
}
//保存圖像
void image_save(FILE *file)
{
	int times=3; //輸入文件名次數。
	char bmp_name[10]; //文件名
	//int i; //記錄數據區個數
	printf("\nplease enter a file name for writeing:");
	do
	{
		if (times<3)
		{
			printf("\nplease enter a file name for writeing again:");
		}
		fflush(stdin);
		gets(bmp_name);
		printf("\n%s",bmp_name);
		file=fopen(bmp_name,"wb+"); //打開一個文件進行讀寫操作。
		--times;
		if (file==NULL)
		{
			printf("\nerror opening %s for writing",bmp_name);
		}
		else
		{
			break;
		}
	}
	while(times!=0);
	if (times==0)
	{
		printf("\nsorry, shutdown!");
		exit(1);
	}
	//寫文件頭
	printf("\n%s",bmp_name);
	fseek(file,0L,0); //圖像文件類型
	fwrite(&(bmp.bfType),sizeof(short),1,file);
	printf("\n bmp tpye: %d",bmp.bfType);
	fseek(file,2L,0); //圖像文件大小
	fwrite(&(bmp.bfSize),sizeof(int),1,file);
	printf("\n bmp size: %d",bmp.bfSize);
	fseek(file,6L,0); //圖像文件保留字1
	fwrite(&(bmp.bfReserved1),sizeof(short),1,file);
	printf("\n bmp reserved1: %d",bmp.bfReserved1);
	fseek(file,8L,0); //圖像文件保留字2
	fwrite(&(bmp.bfReserved2),sizeof(short),1,file);
	printf("\n bmp reserved2: %d",bmp.bfReserved2);
	fseek(file,10L,0);//數據區的偏移量
	fwrite(&(bmp.bfOffBits),sizeof(short),1,file);
	printf("\n bmp offBits: %d",bmp.bfOffBits);
	fseek(file,14L,0);//文件頭結構大小
	fwrite(&(bmp.biSize),sizeof(int),1,file);
	printf("\n bmp bisize: %d",bmp.biSize);
	fseek(file,18L,0);//圖像的寬度
	fwrite(&(bmp.biWidth),sizeof(int),1,file);
	printf("\n bmp biWidth: %d",bmp.biWidth);
	fseek(file,22L,0);//圖像的高度
	fwrite(&(bmp.biHeight),sizeof(int),1,file);
	printf("\n bmp biHeight: %d",bmp.biHeight);
	fseek(file,24L,0);//圖像的面數
	fwrite(&(bmp.biPlanes),sizeof(short),1,file);
	printf("\n bmp biplans: %d",bmp.biPlanes);
	fseek(file,28L,0);//圖像一個像素的字節數
	fwrite(&(bmp.biBitCount),sizeof(short),1,file);
	printf("\n bmp biBitCount: %d",bmp.biBitCount);
	fseek(file,30L,0);//圖像壓縮信息
	fwrite(&(bmp.biCompression),sizeof(short),1,file);
	printf("\n bmp biCompression: %d",bmp.biCompression);
	fseek(file,34L,0);//圖像數據區的大小
	fwrite(&(bmp.biSizeImage),sizeof(int),1,file);
	printf("\n bmp biSizeImage: %d",bmp.biSizeImage);
	fseek(file,38L,0);//水平分辨率
	fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);
	printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
	fseek(file,42L,0);//垂直分辨率
	fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);
	printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
	fseek(file,46L,0);//顏色索引數
	fwrite(&(bmp.biClrUsed),sizeof(int),1,file);
	printf("\n bmp biClrUsed: %d",bmp.biClrUsed);
	fseek(file,50L,0);//重要顏色索引數
	fwrite(&(bmp.biClrImportant),sizeof(int),1,file);
	printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);
	fseek(file,(long)(bmp.bfOffBits),0);
	fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
	fclose(file);
}

3.bmp圖片像素的處理(灰度化、二值化、反相、抽取RGB通道以及改變圖像亮度)

//pixProcess.c文件
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"
//灰度化
void image_gray()
{
	int i,j;
	unsigned char tmp;
	for (i=0;i<bmp.biHeight;i++)
	{
		for (j=0;j<line_byte/3;j++)
		{
			tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
			imagedata[i*line_byte+j*3+0]=tmp;
			imagedata[i*line_byte+j*3+1]=tmp;
			imagedata[i*line_byte+j*3+2]=tmp;
			//printf("\nnidsfh%d %d",i,j);
		}
	}
}
//二值化
void image_binarization()
{
	int i,j;
	for (i=0;i<bmp.biHeight;i++)
	{
		for (j=0;j<line_byte;j++)
		{
			if ((*(imagedata+i*line_byte+j))<128)
			{
				imagedata[i*line_byte+j]=0;
			}
			else
			{
				imagedata[i*line_byte+j]=255;
			}
		}
	}
}
//反相
void image_opposite() 
{
	int i,j;
	for (i=0;i<bmp.biHeight;i++)
	{
		for (j=0;j<line_byte;j++)
		{
			imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
		}
	}
}
//抽取RGB通道
void image_channel() 
{
	int i,j;
	char rgb;
	printf("\nplease enter a char(r/g/b): ");
	fflush(stdin);
	scanf("%c",&rgb);
	if (rgb=='b')
	{
		for (i=0;i<bmp.biHeight;i++)
		{
			for (j=0;j<line_byte/3;j++)
			{
				imagedata[i*line_byte+3*j+1]=0;
				imagedata[i*line_byte+3*j+2]=0;
			}
		}
	}
	else if(rgb=='g')
	{
		for (i=0;i<bmp.biHeight;i++)
		{
			for (j=0;j<line_byte/3;j++)
			{
				imagedata[i*line_byte+3*j]=0;
				imagedata[i*line_byte+3*j+2]=0;
			}
		}
	}
	else
	{
		for (i=0;i<bmp.biHeight;i++)
		{
			for (j=0;j<line_byte/3;j++)
			{
				imagedata[i*line_byte+3*j]=0;
				imagedata[i*line_byte+3*j+1]=0;
			}
		}
	}
}
//改變圖像亮度
void image_bright()
{
	int level;
	int i,j;
	printf("\n please enter the level of brightness[-255 to 255] :");
	fflush(stdin);
	scanf("%d",&level);
	for (i=0;i<bmp.biHeight;i++)
	{
		for (j=0;j<line_byte;j++)
		{
			if (level>=0)
			{
				if ((imagedata[i*line_byte+j]+level)>255)
					imagedata[i*line_byte+j]=255;
				else
					imagedata[i*line_byte+j]+=level;
			}
			else
			{
				if ((imagedata[i*line_byte+j]-abs(level))<0)
					imagedata[i*line_byte+j]=0;
				else
					imagedata[i*line_byte+j]+=level;
			}
		}
	}
}

4.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章