Tuesday, 16 July 2013

Basics of Image Processing

BASIC OF AN IMAGE:
An Image mainly consists of three contents namely:
  1. Header
  2. Data
  3. Footer
Footer is not generally used in an image but the main contents of an image are Header and Data. For a BMP (Bitmap) file Header is generally of 54 bytes (For more details refer to my blog for Bitmap Header file contents http://testyourskillsforcpp.blogspot.in/2013/06/bitmap-header-file-contents.html). After the header the main content of an image is data.
NOTE: An image without header is a raw data.


So, a raw data is stored digitally in your device and if header is added to it, then it is a visible image and can be viewed by image viewer software.

The data is stored in small elements or arrays called as pixels and 1 pixel can be of 1 bit, 1 byte, 2 byte, 3 byte or 4 byte.
Image of 400 X 300 means 400 pixels by 300 pixels= 1,20,000 pixels= 3,60,000 bytes if 1pixel= 3bytes.


  • For a Black and White image is represented by set of two colors in a pixel 0 (Black) and 1 (white).
  • For a Grayscale image pixels vary from 0-255. Grayscale image can be of 8 bit or 24 bit.
  • For a color image, each pixel contain RGB colors as primary colors and each color varies from 0-255 i.e. R= 0-255, G= 0-255, B= 0-255. A color image can be of 8 bits, 16 bits, 24 bits and 32 bits. NOTE: For a 8 bit colored image, we refer to the color table which consists of RGB values along with alpha and for image above this bit depth there is no need of color table.


IMAGE PROCESSING:
Image processing can be done in various ways but here we would discuss some of the basic image processing.

Grayscal to Binary:
Grayscale image can be converted to binary just by reading the image header and the data and saving it to the other formated or same formated file.


After copying the image to the destination file read the pixels byte by byte and thereby giving a condition inside a loop, that, if the given byte is greater than equal to 128 then assign the value of d= 0 (black) fputc(d,dest) where d is the character from the original file which you have read, else d=255 (white) fputc(d,dest).


Code for converting grey to b/w:

void main()
{
FILE *src; int offset;
fopen_s(&src,"jaguar_24bit_grayscale.bmp","rb");
FILE *dest;
fopen_s(&dest,"jaguar_24bit_bw.bmp","wb");
fseek(src,10,SEEK_SET);
fread(&offset,4,1,src);
copy_Header(src,dest);

fseek(src,offset,SEEK_SET);
fseek(dest,offset,SEEK_SET);
unsigned char x=(unsigned char)fgetc(src);
while(!feof(src))
{
if (x>=100)
{
x=255;
fputc(x,dest);
}
else if (x<=99)
{
x=0;
fputc(x,dest);
}
x=(unsigned char)fgetc(src);

}
fclose(src);
fclose(dest);
puts("Image Copied");
_getch();

}

Color to Grayscale:
Similar to the above processing the color to grayscale processing is done.


Copy the header of the source image to the destination file.
From offset read the values of the source file.
Use y= (b*0.114)+(0.587*g)+(0.299*r) formula inside the loop until the end of file.
Put the value of y in destination file.


Code for color to grey:

void main()
{
FILE *src; int offset;
fopen_s(&src,"jaguar_24bit_color.bmp","rb");
FILE *dest;
fopen_s(&dest,"jaguar_24bit_greyscale.bmp","wb");
fseek(src,10,SEEK_SET);
fread(&offset,4,1,src);
copy_Header(src,dest);

fseek(src,offset,SEEK_SET);
fseek(dest,offset,SEEK_SET);
unsigned char x=(unsigned char)fgetc(src);
double b,g,r,y;
while(!feof(src))
{
int i;
unsigned char ch[3];
for(i=0;i<3;i++)
{
b=ch[0]=fgetc(src);
g=ch[1]=fgetc(src);
r=ch[2]=fgetc(src);

y= (b*0.114)+(0.587*g)+(0.299*r);
fputc(y,dest);
fputc(y,dest);
fputc(y,dest);
}
}
fclose(src);
fclose(dest);
puts("Image Copied");
_getch();

}

Channel Separation:
RGB Channel separation is the process in which we read the RGB channels separately.
Here is an input and output images for channel separation of three different type:
  1. First type is a simple channel separation in which we take R value for red channel, G value for green channel and B value for blue channel. For blue channel, Blue is put into the place of green and red value for each pixel. This theory is applied respectively for red as well as for green channel.
RED CHANNEL SEPARATION:



GREEN CHANNEL SEPARATION:



BLUE CHANNEL SEPARATION:


    2.  In second type of channel separation we take is isolated color channel separation in which, Blue value is assigned for blue channel, red value assigned for red channel and green value assigned for green channels and rest are zero for each isolated color channel separation. In order to get blue image we have taken blue value of the source image and putting it to the place of blue in the destination file and rest is given 0 i.e. R=0 and G=0. Similarly to get green image the value of green in the destination file in each pixel and rest to 0, similar for red channel.

BLUE CHANNEL SEPARATION:

 G=0 and R=0


GREEN CHANNEL SEPARATION:


B=0 and R=0


RED CHANNEL SEPARATION:

B=0 and G=0

No comments:

Post a Comment