将颜色转换为灰度 [英] converting a color iage to grayscale

查看:89
本文介绍了将颜色转换为灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  
#include < string.h >
#include < conio.h >
#include < stdlib.h >
#include < iostream.h >
#include < math.h >
#include < fstream.h >


#define MAXX 300
#define MAXY 300

#define biwidth 256
#define biheight 256
#define off_bits 1078


struct bithead // 定义标题结构。
{
char bm [ 2 ];
long int size;
int no1;
int no2;
long int offset;
};

struct info_header // 定义信息标题结构
{
long int size1;
long int width;
long int height;
int plane;
int bit_count;
long int comp;
long int simage;
long int xppm;
long int yppm;
long int clrused;
long int clrimp;
};
// 函数原型
void write_to_file(FILE *);
void read_image_into_array(FILE *);
void make_image_data_binary(); // 0或1

// 全局变量声明
int 巨大的图像[MAXX] [MAXY ]。
char a [ 1078 ];

// --------------- Main程序开始---------------------------
void main()
{
clrscr();
FILE * fp,* op;
struct bithead bmp; // 结构变量是已定义的文件头。
struct info_header inf; // 为图像信息标题定义结构变量。


fp = fopen( bird.bmp rb); // 源BMP文件以读取二进制模式打开

if (fp == NULL) // 文件打开失败,程序终止。
{
cout<< 无法打开文件 ;
getch();
退出( 0 );
}

fread(& bmp, sizeof (bmp), 1 < /跨度>,FP); // 阅读标题
fread(& inf, sizeof (inf), 1 ,fp); // 阅读文件信息标题

// 显示BMP文件标题。
cout<< \ n ********标题信息********* \ n \ n;
cout<< \ nBMP FILE HEADER =<< bmp。 BM;
cout<< \ n\\\
Size of file =
<< ; bmp.size;
cout<< \ nRESERVED WORD1 =<< bmp.no1 ;
cout<< \ nRESERVED WORD2 =<< bmp.no2 ;
cout<< \ nOffset of Data =<< bmp。偏移;

// 显示BMP IMG INFO标题。

cout<< \ n \\\
BITMAP INFO HEADER
;
cout<< \ n\\\
Size of Bitmap Info header =
< ;< inf.size1;
cout<< \ n位图宽度=<< inf。宽度;
cout<< \ n Bitmap =<< inf。高度;
cout<< \ nnun of planes =<< inf。平面;
cout<< \ n每像素位数=<< inf.bit_count;
cout<< \ n压缩类型=<< inf。补偿;
cout<< \ nSize of image =<< inf。 SIMAGE;
cout<< \ nHorizo​​ntal Resolution =<< inf.xppm ;
cout<< \ nVertical Resolution =<< inf.yppm ;
cout<< \ nNo。使用的颜色=<< inf.clrused;
cout<< \ nNo.of Important Colors =<< inf.clrimp;

fclose(fp);


fp = fopen( bird.bmp rb);
op = fopen( c:\\tc\\results\\ copy .bmp wb);

printf( \ n ************ ********************** \ n);
printf( \ nImage在以下位置复制:\ n);
printf( \ nc:\\tc \\ results \ n< /跨度>);


if (fp == NULL || op == NULL)
{

退出( 0 );
}

fread(& a, sizeof (a), 1 < /跨度>,FP); // bird.bmp
fwrite(& a, sizeof (a), 1 ,op); // copy.bmp

read_image_into_array(fp);
fclose(fp);
write_to_file(op);
fclose(op);
// -------------------- ----------------------------


fp = fopen( bird.bmp RB);

if (fp == NULL)
{

exit( 0 );
}

fread(& a, sizeof (a), 1 < /跨度>,FP);


read_image_into_array(fp);
fclose(fp);


op = fopen( c:\\tc \ \\\ults\\binary.bmp wb); // binary.bmp
fwrite(& a, sizeof (a), 1 ,op);

printf( \ n ************ ********************** \ n);
printf( \ nImage二进制化成功执行\ n);
printf( \ Binary图像存储在以下位置:\ n);
printf( \ nc:\\tc \\ results \ n< /跨度>);


make_image_data_binary();

write_to_file(op);
fclose(op);
printf( \ n **************** ****************** \ n);

getch();
退出( 0 );
}

void write_to_file(FILE * file)
{
int x = 0 ,y = 0 ;
fseek(file,off_bits, 0 );
for (x = 0 ; x< biheight; x ++)>
for (y = 0 ; y< biwidth; y ++)>
fputc( int (image [x] [y]),file);
}

/ * function:read_image_into_array
input:输入图像file .bmp format
output:2D数组,包含图像中每个像素的强度值
* /



void read_image_into_array(FILE * file)
{
int x = 0 ,y = 0 ;
fseek(file,off_bits, 0 );
for (x = 0 ; x< biheight; x ++)>
for (y = 0 ; y< biwidth; y ++)>
image [x] [y] = fgetc(file);

}

void make_image_data_binary()
{
int x = 0 ,y = 0 ;
for (x = 0 ; x< biheight; x ++)>
for (y = 0 ; y< biwidth; y ++)>
{
if (image [x] [y]> 128)
image [x] [y] = 255 ;
else if (image [x] [y]< = 128
image [x] [y] = 0 ;
}
}




此代码中的
将彩色图像转换为二进制。但现在我要转换颜色图像到grascale image.and我不知道如何去做.plz help

解决方案

如果你已经转换了所有的信息,你无法真正获得信息数据为'1'或'0'。你输出的函数:包含图像中每个像素的强度值的2D数组,是你获得灰度的地方,所以不要运行'make_image_data_binary()',你就会有答案。

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
#include<math.h>
#include<fstream.h>


#define MAXX 300
#define MAXY 300

#define biwidth 256
#define biheight 256
#define off_bits 1078


struct bithead    //Defining header structure.
	{
	char bm[2];
	long int size;
	int no1;
	int no2;
	long int offset;
	};

struct info_header  //Defining info header structure
	{
	long int size1;
	long int width;
	long int height;
	int plane;
	int bit_count;
	long int comp;
	long int simage;
	long int xppm;
	long int yppm;
	long int clrused;
	long int clrimp;
	};
//function prototype
void write_to_file(FILE *);
void read_image_into_array(FILE *);
void make_image_data_binary();             // 0 or 1

//Global variables declaration
int huge image[MAXX][MAXY];
char a[1078];

// ---------------Main program begins---------------------------
void main()
  {
	clrscr();
	FILE *fp,*op;
	struct bithead bmp;      //Structure Variable is defined file header.
	struct info_header inf;  //Structure Variable is defined for image info header.


	fp=fopen("bird.bmp","rb"); //source BMP File is Opened in read binary mode

	if(fp==NULL)               //On failure in file open, program terminates.
				  {
	  cout<<"Cannot open the file";
		  getch();
		  exit(0);
	  }

	fread(&bmp,sizeof(bmp),1,fp);         //Reading header
	fread(&inf,sizeof(inf),1,fp); 	       //reading file info header

	//Displaying the BMP File Header.
	cout<<"\n********Header information*********\n\n";
	cout<<"\nBMP FILE HEADER="<<bmp.bm;
	cout<<"\n\nSize of file="<<bmp.size;
	cout<<"\nRESERVED WORD1="<<bmp.no1;
	cout<<"\nRESERVED WORD2="<<bmp.no2;
	cout<<"\nOffset of Data="<<bmp.offset;

	 //Displaying the BMP IMG INFO Header.

	cout<<"\n\nBITMAP INFO HEADER";
	cout<<"\n\nSize of Bitmap Info header="<<inf.size1;
	cout<<"\nWidth of Bitmap="<<inf.width;
	cout<<"\nHeight of Bitmap="<<inf.height;
	cout<<"\nNumber of planes="<<inf.plane;
	cout<<"\nNumber of bits per pixel="<<inf.bit_count;
	cout<<"\nType of Compression="<<inf.comp;
	cout<<"\nSize of image="<<inf.simage;
	cout<<"\nHorizontal Resolution="<<inf.xppm;
	cout<<"\nVertical Resolution="<<inf.yppm;
	cout<<"\nNo. of Colors used="<<inf.clrused;
	cout<<"\nNo. of Important Colors ="<<inf.clrimp;

	fclose(fp);


	fp=fopen("bird.bmp","rb");
	op=fopen("c:\\tc\\results\\copy.bmp","wb");

	printf("\n********************************** \n");
	printf("\nImage Copied at following location: \n");
	printf("\nc:\\tc\\results \n");


  if(fp==NULL||op==NULL)
   {

   exit(0);
   }

   fread(&a,sizeof(a),1,fp);    //bird.bmp
   fwrite(&a,sizeof(a),1,op);   //copy.bmp

   read_image_into_array(fp);
   fclose(fp);
	write_to_file(op);
	fclose(op);
//------------------------------------------------


   fp=fopen("bird.bmp","rb");

   if(fp==NULL)
   {

   exit(0);
   }

   fread(&a,sizeof(a),1,fp);


   read_image_into_array(fp);
   fclose(fp);


  op=fopen("c:\\tc\\results\\binary.bmp","wb"); //binary.bmp
  fwrite(&a,sizeof(a),1,op);

printf("\n********************************** \n");
printf("\nImage Binarization successfully performed \n");
printf("\Binary Image stored at following location: \n");
printf("\nc:\\tc\\results \n");


	make_image_data_binary();

	write_to_file(op);
	fclose(op);
	printf("\n********************************** \n");

	getch();
	exit(0);
}

void write_to_file(FILE *file)
{
	int x=0,y=0;
	fseek(file,off_bits,0);
	for(x=0;x<biheight;x++)>
		for(y=0;y<biwidth;y++)>
			fputc(int(image[x][y]),file);
}

/* function : read_image_into_array
   input    : Input image file .bmp format
   output   : 2D Array containing intensity values of each pixel in the image
*/


void read_image_into_array(FILE *file)
{
	int x=0,y=0;
	fseek(file,off_bits,0);
	for(x=0;x<biheight;x++)>
		for(y=0;y<biwidth;y++)>
			image[x][y]=fgetc(file);

}

void make_image_data_binary()
{
	int x=0,y=0;
	for(x=0;x<biheight;x++)>
		for(y=0;y<biwidth;y++)>
		{
			if(image[x][y]>128)
				image[x][y]=255;
			else if(image[x][y]<=128)
				image[x][y]=0;
		}
}



in this code iv converted color image to binary .but now i want to convert color image to grascale image.and i dont know how to go about doing it.plz help

解决方案

You can't really get the information back if you have converted all the data to a '1' or a '0'. the function where you 'output : 2D Array containing intensity values of each pixel in the image', is where you obtain your gray scale, so then just don't run the 'make_image_data_binary()' and you will have your answer.


这篇关于将颜色转换为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆