Image Processing Part I
Published Jun 12, 2018
Last updated
Published Jun 12, 2018
Last updated
Part 2 Published on Codementor ðĨģðĨģðĨģ And It's Lit! ðð
Grab the source code ðĻðūâðŧ
Follow me on Twitter and Quora for programming and more technical stuff ðð
Computer store images as a mosaic of tiny squares. This is like the ancient art form of tile mosaic, or the melting bead kits kids play with today. Now, if these square tiles are too big, itâs then hard to make smooth edges and curves. The more and smaller tiles we use, the smoother or as we say less pixelated, image will be. These sometimes gets referred to as resolution of the images.
Vector graphics are somewhat different method of storing images that aims to avoid pixel related issues. But even vector images, in the end, are displayed as a mosaic of pixels. The word pixel means a picture element. A simple way to describe each pixel is using a combination of three colors, namely Red, Green, Blue. This is what we call an RGB image.
In an RGB image, each pixel is represented by three 8 bit numbers associated to the values for Red, Green, Blue respectively. Eventually using a magnifying glass, if we zoom a picture, weâll see the picture is made up of tiny dots of little light or more specifically the pixels and what more interesting is to see that those tiny dots of little light are actually multiple tiny dots of little light of different colors which are nothing but Red, Green, Blue channels.
Pixel together from far away, create an image and upfront theyâre just little lights that are ON and OFF. The combination of those create images and basically what we see on screen every single day.
Every photograph, in digital form, is made up of pixels. They are the smallest unit of information that makes up a picture. Usually round or square, they are typically arranged in a 2-dimensional grid.
Now, if all three values are at full intensity, that means theyâre 255, it then shows as white and if all three colors are muted, or has the value of 0, the color shows as black. The combination of these three will, in turn, give us a specific shade of the pixel color. Since each number is an 8-bit number, the values range from 0-255.
Combination of these three color will posses tends to the highest value among them. Since each value can have 256 different intensity or brightness value, it makes 16.8 million total shades.
View on more convenient env : Jupyter nbviewer
Now letâs load an image and observe its various properties in general.
The shape of the ndarray show that it is a three layered matrix. The first two numbers here are length and width, and the third number (i.e. 3) is for three layers: Red, Green, Blue. So, if we calculate the size of a RGB image, the total size will be counted as height x width x 3
These values are important to verify since the eight bit color intensity is, can not be outside of the 0 to 255 range.
Now, using the picture assigned variable we can also access any particular pixel value of an image and further can access each RGB channel separately.
In these case: R = 109 ; G = 143 ; B = 46 and we can realize that this particular pixel has a lot of GREEN in it. And now we could have also selected one of this number specifically by giving the index value of these three channel. Now we know for this
0 index value for Red channel
1 index value for Green channel
2 index value for Blue channel
But good to know that in OpenCV, Images takes as not RGB but BGR. imageio.imread loads image as RGB (or RGBA), but OpenCV assumes the image to be BGR or BGRA (BGR is the default OpenCV colour format).
OK, now letâs take a quick view of each channels in the whole image.
Now, here we can also able to change the number of RGB values. As an example, letâs set the Red, Green, Blue layer for following Rows values to full intensity.
R channel: Row- 100 to 110
G channel: Row- 200 to 210
B channel: Row- 300 to 310
Weâll load the image once, so that we can visualize each change simultaneously.
To make it more clear letâs change the column section too and this time weâll change the RGB channel simultaneously.
View on more convenient env : Jupyter nbviewer
Now, we know that each pixel of the image is represented by three integers. Splitting the image into separate color components is just a matter of pulling out the correct slice of the image array.
Black and white images are stored in 2-Dimentional arrays. Thereâre two types of Black and White images:
Greyscale : Ranges of shades of grey : 0 ~ 255
Binary: Pixel are either black or white : 0 or 255
Now, Greyscaling is such process by which an image is converted from a full color to shades of grey. In image processing tools, for example: in OpenCV, many function uses greyscale images before porcessing and this is done because it simplifies the image, acting almost as a noise reduction and increasing processing time as thereâs less information in the images.
There are a couple of ways to do this in python to convert image to grayscale. But a straight forward way using matplotlib is to take the weighted mean of the RGB value of original image using this formula.
However, the GIMP converting color to grayscale image software has three algorithms to do the task.
Lightness The graylevel will be calculated as
Lightness = Â― Ã (max(R,G,B) + min(R,G,B))
Luminosity The graylevel will be calculated as
Luminosity = 0.21 Ã R + 0.72 Ã G + 0.07 Ã B
Average The graylevel will be calculated as
Average Brightness = (R + G + B) ÷ 3
Letâs give a try one of their algorithm, what about Luminosity.
We can create a bullion ndarray in the same size by using a logical operator. However, this wonât create any new array but it simply return True to its host variable. For example: letâs consider we want to filter out some low value pixel or high value or (any condition) in an RGB image and yes it would be great to convert RGB to gray scale but for now we wonât go for that rather than deal with color image.
Letâs first load an image and show it on screen.
OK, letâs consider this dump image. Now, for any case we want to filter out all the pixel value which is below than, letâs assume 20. For this weâll use logical operator to do this task which weâll return as a value of True for all the index.
Now as we said, a host variable , well this name is not traditionally used but i refer it because it behaves. It just hold the True value and nothing else. So, if we see the shape of both low_pixel and pic , weâll find that both have the same shape.
We generated that low value filter using a global comparison operator for all the values less than 200. However, we can use this low_pixel array as an index to set those low values to some specific values which may be higher than or lower than the previous pixel value.
Image masking is an image processing technique that is used to remove the background from which photographs those have fuzzy edges, transparent or hair portions.
Now, weâll create a mask that is in shape of a circular disc. First weâll measure distance from center of the image to every border pixel values. And we take a convenient radius value and then using logical operator weâll create a circular disc. Itâs quite simple, letâs see the code.
View on more convenient env : Jupyter nbviewer
One of MOOC course on edX, weâve introduced with some satellite images and its processing system. Itâs very informative of course.However, letâs do a few analysis task on it.
Letâs see some basic info of it.
Now, Thereâs something interesting about this image. Like many other visualizations, the colors in each rgb layer mean something. For example, the intensity of the red will be an indication of altitude of the geographical data point in the pixel. The intensity of blue will indicate a measure of aspect and the green will indicate slope. These colors will help to communicate this information in a quicker and more effective way rather than showing numbers.
Red pixel indicates: Altitude
Blue pixel indicates: Aspect
Green pixel indicates: Slope
There is, by just looking at this colorful image, a trained eye can tell already what the altitude, whatâs the slope, whatâs the aspect. So thatâs the idea of loading some more meaning to these colors to indicate something more scientific.
Continue...
Downlaod Find Full Source Code
Find this articles also on
References
Reference : https://www.codementor.io/@innat_2k14/image-data-analysis-using-numpy-opencv-part-1-kfadbafx6