Tuesday, 15 January 2013

Terminology of BMP file format

Here we discuss some of terminology which are used in BMP file format.It is very important to having knowledge about them during study of BMP file format.They are following:-

Padding :-In Bmp file format each row in the pixel array must be in multiple of 4.If the image data row size is not in the multiple of 4 then it will make in multiple of 4 by adding to the each row. If each row in pixel array is in multiple of 4,then there is no need of padding.Row size is calculated with the help of Bits per pixel & image width in pixels which is following:-
                   
                 \mbox{RowSize} = \left\lfloor\frac { \mbox{BitsPerPixel} \cdot \mbox{ImageWidth}+31
 }{32}\right\rfloor \cdot 4,

Bits Per Pixel/Bit depth:- Bits per pixel or Bit depth are the same thing.It used to describe how many bits are stored in each pixel.With the help of bit depth we can know two things which are very important.First ,If image is of n bit per pixel it means it support 2^n distinct colors.For Example,If image is 8-bit per pixel then it supports 2^8=256 distinct colors.Second most important which we come to know is ,if image having n-bit per pixels it means n/8 byte occupies by 1 pixel because 8 bit=1 byte. Normally 1,2,4,8,12,16,24,30,32,36,48 bit depth images are possible.1 bpp is also called monochrome.Images of 15/16 bit color depth are called high color image.Image of 24-bit color depth is called true color image.There are 2^24 colors in the true color. 30,36 & 48 bit color depth image called deep color image .It used xvYCC,sRGB & YCbCr color spaces.Information of Bit depth stores on 28th byte of header & 14th byte of DIB header.Its information occupy 2 bytes.

Extra bit Masks:-It is found after DIB header.It is an optional structure in BMP image.Its main purpose is to define pixel formats.It has size of 3 or 4 DWORDs (12 or 16 bytes). This is only present when DIB header is BITMAPINFOHEADER.

BitMask:-In order to resolove the ambiguity of which bits define which samples,the dib header provide
certain defaults as well as specific BITFIELDS which are called bitmask that defines the particular group of bits in a pixel to a particular channel.

Alpha & Gamma :-There is many type of bitmasks available in DIB header.There is bitmask for all three channels R,G & B.There is an another bitmask which is called alpha channel bitmask.Alpha channel  is basically used as opacity channel.Alpha bitmask is avaliable after  66 bytes in header & occupy 4 bytes in DIB header.The alpha bitmask is valid whenever it is present in the DIB heder or when the compression member of DIB header is set to BI_ALPHABITFIELDS.
                                                                         Gamma is applicable for the all three channels RED,GREEN & BLUE.It is available in DIB header of a BMP image.It occupies 4-byte for each channel.If it is available in DIB header then It comes after 74 byte .

Some of more important details about BMP image are given below pointwise:-

1)Information of compression method used in image is available  after 30 byte of header .It is available in DIB header after 16 byte .Its occupies 4 bytes in header.

ValueIdentified byCompression methodComments
0BI_RGBnoneMost common
1BI_RLE8RLE 8-bit/pixelCan be used only with 8-bit/pixel bitmaps
2BI_RLE4RLE 4-bit/pixelCan be used only with 4-bit/pixel bitmaps
3BI_BITFIELDSBit field or Huffman 1D compression for BITMAPCOREHEADER2Pixel format defined by bit masks or Huffman 1D compressed bitmap for BITMAPCOREHEADER2
4BI_JPEGJPEG or RLE-24 compression for BITMAPCOREHEADER2The bitmap contains a JPEG image or RLE-24 compressed bitmap for BITMAPCOREHEADER2
5BI_PNGPNGThe bitmap contains a PNG image
6BI_ALPHABITFIELDSBit fieldThis value is valid in Windows CE .NET 4.0 and later.


2)BITMAPFILEHEADER is the first part of BMP image header.It has the information about Header field,file size,two reserved fields & an offset which is the starting address of image data.Its total size is 14 bytes .Header field is identification of BMP image .It has values like BM,BA,CM,CI etc. BITMAPFILEHEADER   also gives file size in bytes.It also have the reserved field each of size 2 bytes whose actual value depends on the  application   that creates the image.It also has information which stores information of starting address of  image data in bytes.this information occupy 4 bytes.

3)BITMAPCOREHEADER is the second part of BMP image header.It has the information about size of this structure,Width & height of image in pixels,no of color planes being used,no of bits per pixel in image.
It occupy  4 bytes for first 3 & occupy 2 bytes for last two.It means this structure total occupy 16 bytes.

4)BITMAPV5HEADER is an extended version of BITMAPINFOHEADER Structure.It has large information about image.It has extended information ARGB channel bitmask,Gamma for RGB channels
& Color space type,color space end points etc.

5)Size of color table entries is 3 bytes in BITMAPCOREHEADER & 4 bytes in BITMAPV5HEADER.

6)No of entries present in the Color table is found by 2^n where n is the bit depth or bits per pixel. Thus it is totally depend upon bit depth.Color table is mandatory for images which have bit depth of less than or equal to 8.  

7)The field colors in color table shows th no of colors in palette whose possible values 0 to default to 2^n where n is the bit depth.its information is stores on 46 th byte of header & its information size is 4 byte.

8)Number of important color count is availbale on 50th byte of header.Its information stores 4 byte of memory.It explain that how many important colors are used .0 means all colors are important.

9)The size of color space endpoints is 36 bytes .It comes after color space type in header. 74th byte is the offset for the color space points.

10)The 26th byte of header contains the information for how much color planes are used which must be set to 1.It occupy 2 bytes in header.

Color Space:-When the color model is associated with precise description of how the components are to be interpreted (viewing conditions etc.) the resulting set of colors is called Color Spaces.Possible values for color spaces are RGB,CMY/CMYK, YIQ,YPbPr,xvYCC,HSB,HSL,Munsell Color System,Natural Color System,RG chromaticity,TSL Color Space.70 byte is the offset for the color space type in header.Its inforamation size is 4 byte.
                                     RGB & CMYK color space are respectively additive & substractive color spaces they are descibed in previous blogs .Munsell & natural color systems are commercial color system.The RG chromaticity & TSL color Spaces are special purpose color spaces.The first one is used in computer vision application & the later is used for face detection.

*The two color Schemes widely used in bitmap compression are ARGB & AYCoCg.These schemes have the following color planes:-

Color scheme
Color planes
ARGB
Alpha, Red, Green, Blue
AYCoCg
Alpha, Luma, Orange Chroma, Green Chroma

Color Space Conversion:-Color channel based compression does not operate on the ARGB color space but rather a color space based on Luminosity (Y) & differential of two color components :Orange(Co)& Green(Cg).Conversion between ARGB & AYCoCg color spaces can be performed by using forward & inverse transformation.The Alpha channel is never converted in either direction.
The forward transformation to convert from ARGB to AYCoCg is as follows:-
A=A
|Y|=|1/4  1/2  1/4|       |R|
|Co|=|1    0     -1|   *  |G|
|Cg|=|-1/2  1  -1/2|     |B|
The inverse transformation to convert from AYCoCg to ARGB is as follows:-
A=A
|R|=|1 1/2 -1/2|     |Y|
|G|=|1 0 1/2 |     * |Co|
|B|=|1 -1/2 -1/2|   |Cg |
The allowed ranges of the individual color planes & their lengths in bits are as follows:-

Color plane
Range
Type
Alpha (A)
0 (transparent) – 255 (opaque)
8 bits
Red (R)
0 – 255
8 bits
Green (G)
0 – 255
8 bits
Blue (B)
0 – 255
8 bits
Luma (Y)
0 – 255
8 bits
Orange Chroma (Co)
-255 – 255
9 bits (two's complement)
Green Chroma (Cg)
-255 – 255
9 bits (two's complement)



Channel Separation

RGB Channel separation is the process in which we read the RGB channels separately.
Here is a input & 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 & B value for blue channel .For blue channel Blue put into the place of green & blue value for each pixel & then we get the blue channel separated image.This theory is applied respectively for red & green channel.

Input Image:-




Output Images:-

Red channel Image:-







Green Channel Image:-







Blue channel image:-








2)In second type of channel separation we take Blue value for blue channel,red value for red channel & green value for green channels & put 0 to the other value means we put 0 for red & green in blue channel.

here is the output images for same input image:-

 Output Images:-



Red channel Image:-       




Green Channel Image:-



Blue channel image:-


    



3)In third type of channel separation we create secondary colors from primary colors we use two of three to get a secondary colors.For Example we use blue & green color to create a cyan color image.Get a yellow                          color image with red & green .& magenta with red & blue colors.Here is the output images for  same input image.


Output Images:-

Magenta color Image:-



Cyan Color Image:-


Yellow Color Image:-





Friday, 11 January 2013

Edge Detection of a BMP Image Using Sobel Operator

Today ,I wrote a program for edge detection in Bitmap Image using Sobel Operator.Sobel operator is a descrete operator defined by Sobel.It has the horizontal & vertical derivative which has different values which are basically 3*3 matrix.These derivatives are convolved with image data  & Then produces edge detected image.First of all we should copy image data into an array.then this image data convolved with
horizontal & vertical derivatives.Here is the input & output images for the program :-

Input
Color Image
Output
Edge Detected Image
Here ,We need to discuss some more issues which occurred during programming which are following:-
1)We must take an unsigned char array for storing image data information.
2)Sobel Operator must stored in integer array because they have some negative values.
3)We should use abs function for calculating the gradient magnitude which is available in math.h header file.    

Tuesday, 8 January 2013

Histogram Equalization of a BMP image in C

Today i wrote a program for Histogram Equalization of a Bitmap Image in C.Histogram Equalization simply increases the contrast of images.Basically it balances the overall contrast of image.There is three basic steps in this program:First of all we should calculate probability distribution function of each pixel intensity,then we should calculate Cumulative Distribution function (CDF) of these intensity.Then we will calculate the equalized value on that intensity.
Here is the input & output image for the histogram equalization:-
Input Color Image 
Equalized Image

We have to be consider on some more topics on Histogram Equalization like:-
1)Disadvantages of He
2)When we should avoid He?
3)How can we overcome demerits of He?
4)Real world Application of He.

1)Disadvantage of He:-Followings are some disadvantages of He-
a)It reduces the brightness of some high contrast images.
b)If there are gray values which are physically far apart from each other in the image then this method fails.
c)This method is indiscriminate.It may increase the contrast of background noise & decrease the usable        
   signal.
d)Histogram equalization often produces unrealistic effects in photographs. 
e) It produces undesirable undesirable effects when applied low color depth image.

2)When we would emphasize on local contrast rather than overall contrast then we should avoid Histogram                    Equalization. 

3)We can overcome demerit of He using other generalized form of this like Adaptive histogram equalization (AHE) or Contrast limited adaptive histogram equalization(CLAHE).There are focus on local contrasts rather than overall contrast .they computes several histogram for this.

4)Here some real world application of He-:
a)It is very useful in scientific images like X-ray,thermal,Satellite.
b)It is also used in Biological Neural Network so as to maximize output firing rate of neuron .

Friday, 4 January 2013

Converting a Gray Scale To Black & white

Converting Gray Scale image Into Black & white is very simple .We should take the Each character & check if there is intensity greater than 128 then make it 255 while if it less than 128 then make it 0.Here is a input & output images for the Converting grayscale image into Black & white Image.



GrayScale Image

Black & white



Converting a Color Image To Gray Scale

A gray scale image is an image which has the same RGB values.It means R=G=B.We need to take a Color Image which has the different RGB values in a pixel.We take an average of RGB value & put it into RGB values of Output Image.Then we get  grayscaled image of input Image.

Color Image

Gray Scale Image
Here we discuss different methods to convert a 24-bit color image into gray scale image .In both of these methods there are some predefined constant which are multiplied to the RGB values of pixel to get a grayscale image for every pixel.These predefined values are some standard values diffrent for all three values
& multiplied into them to get a grayscale image.

1)
Y' =  0.299 R + 0.587 G + 0.114 B
2)
                                                Y' =  0.2126 R + 0.7152 G + 0.0722 B
3)                                            Y'=0.333(R+G+B)
4)                                            Y' =Max (R,G,B)

   In this type we calculate Maximum value among RGB value put it into the place of two other color's values.

5)                                             Y' =Min (R,G,B)

In this type we calculate Minimum value among RGB value put it into the place of two other color's values.

6)                                            Y'= (Max(R,G,B)+Min(R,G,B)) /2
 In this type we calculate both Max & min of RGB values .& Then average of them put into place of rgb values 

If Input image is same then output from various aalgorithm is here:-


.
The Last two images are respectively with Max & MIn algo.The Image with max algo is more brighter & The image with min algo is least brighter than other images.
Rest all four images are almost same in vision. 

Copying an Image

There is a two way to copy an Image :1. Copy an image character by character
2.The second is copy header of the Image then copy the data of the Image.We use both of the method in our programming .The functions for the these methods are given below:-