Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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
Posted
Updated 26-Sep-13 17:02pm
v2
Comments
[no name] 26-Sep-13 23:20pm    
Why don't you use opencv?
pasztorpisti 29-Sep-13 9:03am    
Are you serious??? :-) :-) :-) Take a look at the code...
[no name] 29-Sep-13 21:39pm    
Well actually I am serious. The op wants to read a colour image and save a B&W and grayscale image. Quite a trivial exercise with opencv done in half a dozen lines of code.
pasztorpisti 30-Sep-13 4:57am    
The problem with this is that a beginner may be unable to put together the build env, if something is wrong then troubleshooting is more difficult without background knowledge. Plus copy pasting the source code of the control system of a space rocket is not fun as a beginner. Of course if it comes to image processing then hw acceleration is the best. But here, taking a look for example at the file handling code I think that OpenCV can wait...
The_Inventor 28-Sep-13 0:55am    
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.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900