Images And Conversions
There are many type of images, and we will look in detail about different types of images, and the color distribution in them.
The binary image
The binary image as it name states, contain only two pixel values.
0 and 1.
In our previous tutorial of bits per pixel, we have explained this in detail about the representation of pixel values to their respective colors.
Here 0 refers to black color and 1 refers to white color. It is also known as Monochrome.
Black and white image:
The resulting image that is formed hence consist of only black and white color and thus can also be called as Black and White image.
No gray level
One of the interesting this about this binary image that there is no gray level in it. Only two colors that are black and white are found in it.
Binary images have a format of PBM Portable bitmap
2, 3, 4,5, 6 bit color format
The images with a color format of 2, 3, 4, 5 and 6 bit are not widely used today. They were used in old times for old TV displays, or monitor displays.
But each of these colors have more then two gray levels, and hence has gray color unlike the binary image.
In a 2 bit 4, in a 3 bit 8, in a 4 bit 16, in a 5 bit 32, in a 6 bit 64 different colors are present.
8 bit color format
8 bit color format is one of the most famous image format. It has 256 different shades of colors in it. It is commonly known as Grayscale image.
The range of the colors in 8 bit vary from 0-255. Where 0 stands for black, and 255 stands for white, and 127 stands for gray color.
This format was used initially by early models of the operating systems UNIX and the early color Macintoshes.
A grayscale image of Einstein is shown below:
The format of these images are PGM Portable Gray Map
This format is not supported by default from windows. In order to see gray scale image, you need to have an image viewer or image processing toolbox such as Matlab.
Behind gray scale image:
As we have explained it several times in the previous tutorials, that an image is nothing but a two dimensional function, and can be represented by a two dimensional array or matrix. So in the case of the image of Einstein shown above, there would be two dimensional matrix in behind with values ranging between 0 and 255.
But thats not the case with the color images.
16 bit color format
It is a color image format. It has 65,536 different colors in it. It is also known as High color format.
It has been used by Microsoft in their systems that support more then 8 bit color format. Now in this 16 bit format and the next format we are going to discuss which is a 24 bit format are both color format.
The distribution of color in a color image is not as simple as it was in grayscale image.
A 16 bit format is actually divided into three further formats which are Red , Green and Blue. The famous RGB format.
It is pictorially represented in the image below.
Now the question arises, that how would you distribute 16 into three. If you do it like this,
5 bits for R, 5 bits for G, 5 bits for B
Then there is one bit remains in the end.
So the distribution of 16 bit has been done like this.
5 bits for R, 6 bits for G, 5 bits for B.
The additional bit that was left behind is added into the green bit. Because green is the color which is most soothing to eyes in all of these three colors.
Note this is distribution is not followed by all the systems. Some have introduced an alpha channel in the 16 bit.
Another distribution of 16 bit format is like this:
- bits for R, 4 bits for G, 4 bits for B, 4 bits for alpha channel.
Or some distribute it like this
- bits for R, 5 bits for G, 5 bits for B, 1 bits for alpha channel.
24 bit color format
24 bit color format also known as true color format. Like 16 bit color format, in a 24 bit color format, the 24 bits are again distributed in three different formats of Red, Green and Blue. From figure instead of 16 use 24 bit format R, G, and B in same bit.
Since 24 is equally divided on 8, so it has been distributed equally between three different color channels.
Their distribution is like this.
8 bits for R, 8 bits for G, 8 bits for B.
Behind a 24 bit image.
Unlike a 8 bit gray scale image, which has one matrix behind it, a 24 bit image has three different matrices of R, G, B.
Color Codes Conversion Different color codes
All the colors here are of the 24 bit format, that means each color has 8 bits of red, 8 bits of green, 8 bits of blue, in it. Or we can say each color has three different portions. You just have to change the quantity of these three portions to make any color.
Binary color format
Color: Black Image
As it has been explained in the previous tutorials, that in an 8-bit format, 0 refers to black. So if we have to make a pure black color, we have to make all the three portion of R, G, B to 0. Color: White Image:
Since each portion of R, G, B is an 8 bit portion. So in 8-bit, the white color is formed by 255. It is explained in the tutorial of pixel. So in order to make a white color we set each portion to 255 and thats how we got a white color. By setting each of the value to 255, we get overall value of 255, thats make the color white.
RGB color model:
Color: Red Image
Since we need only red color, so we zero out the rest of the two portions which are green and blue, and we set the red portion to its maximum which is 255.
Color: Green Image
Since we need only green color, so we zero out the rest of the two portions which are red and blue, and we set the green portion to its maximum which is 255.
Color: Blue Image
Since we need only blue color, so we zero out the rest of the two portions which are red and green, and we set the blue portion to its maximum which is 255
Color: Gray Image
As we have already defined in our tutorial of pixel, that gray color Is actually the mid point. In an 8-bit format, the mid point is 128 or 127. In this case we choose 128. So we set each of the portion to its mid point which is 128, and that results in overall mid value and we got gray color.
CMYK color model:
CMYK is another color model where c stands for cyan, m stands for magenta, y stands for yellow, and k for black. CMYK model is commonly used in color printers in which there are two carters of color is used. One consist of CMY and other consist of black color.
The colors of CMY can also made from changing the quantity or portion of red, green and blue.
Color: Cyan Image:
Cyan color is formed from the combination of two different colors which are Green and blue. So we set those two to maximum and we zero out the portion of red. And we get cyan color.
Color: Magenta Image
Magenta color is formed from the combination of two different colors which are Red and Blue. So we set those two to maximum and we zero out the portion of green. And we get magenta color.
Color: Yellow Image
Yellow color is formed from the combination of two different colors which are Red and Green.
So we set those two to maximum and we zero out the portion of blue. And we get yellow color.
Now we will see that how color are converted are from one format to another.
Conversion from RGB to Hex code:
Conversion from Hex to rgb is done through this method:
- Take a color. E.g: White = 255,255,255255,255,255.
- Take the first portion e.g 255.
- Divide it by 16. Like this:
- Take the two numbers below line, the factor, and the remainder. In this case it is 15 15 which is FF.
- Repeat the step 2 for the next two portions. Combine all the hex code into one.
Conversion from Hex to RGB:
Conversion from hex code to rgb decimal format is done in this way.
Take a hex number. E.g: #FFFFFF
Break this number into 3 parts: FF FF FF
Take the first part and separate its components: F F
Convert each of the part separately into binary: 11111111 11111111
Now combine the individual binaries into one: 11111111
Convert this binary into decimal: 255
Now repeat step 2, two more times.
The value comes in the first step is R, second one is G, and the third one belongs to B.
Common colors and their Hex code has been given in this table.
Grayscale to RGB Conversion
Now we will convert an color image into a grayscale image. There are two methods to convert it. Both has their own merits and demerits. The methods are:
- Average method
- Weighted method or luminosity method
Average method is the most simple one. You just have to take the average of three colors. Since its an RGB image, so it means that you have add r with g with b and then divide it by 3 to get your desired grayscale image.
Its done in this way.
Grayscale = R+G+B/3R+G+B/3 For example:
If you have an color image like the image shown above and you want to convert it into grayscale using average method. The following result would appear.
There is one thing to be sure, that something happens to the original works. It means that our average method works. But the results were not as expected. We wanted to convert the image into a grayscale, but this turned out to be a rather black image.
This problem arise due to the fact, that we take average of the three colors. Since the three different colors have three different wavelength and have their own contribution in the formation of image, so we have to take average according to their contribution, not done it averagely using average method. Right now what we are doing is this,
33% of Red, 33% of Green, 33% of Blue
We are taking 33% of each, that means, each of the portion has same contribution in the image. But in reality thats not the case. The solution to this has been given by luminosity method.
Weighted method or luminosity method
You have seen the problem that occur in the average method. Weighted method has a solution to that problem. Since red color has more wavelength of all the three colors, and green is the color that has not only less wavelength then red color but also green is the color that gives more soothing effect to the eyes.
It means that we have to decrease the contribution of red color, and increase the contribution of the green color, and put blue color contribution in between these two.
So the new equation that form is:
New grayscale image = (0.3R(0.3 R + 0.59 G0.59 G + 0.11 B0.11 B ).
According to this equation, Red has contribute 30%, Green has contributed 59% which is greater in all three colors and Blue has contributed 11%.
Applying this equation to the image, we get this
Original Image: Grayscale Image:
As you can see here, that the image has now been properly converted to grayscale using weighted method. As compare to the result of average method, this image is more brighter.