Introduction to Image Processing
This is a very popular and interesting topic now a days, everyday we use social media where we share images. We perform some actions on image like cropping image, drawing shapes, writing text etc. These all acts are part of image processing.
An Image is nothing but collection of combinations of pixel. In technical term we can say that it is a matrix of combination of pixels where every cell /pixel contains ratio of combination of three color i.e. Red Blue Green (popularly known as RGB combination).
Image processing is a process of manipulating image pixels to result in a new image with different characteristics such as different shape, different color transitions, drawing shapes on image or writing text on image etc.
There are 3 basic color RGB due to which several colors are made. Range of the content of color varies from 0–255 that means R can vary from 0–255 and same for green and blue.
There are various libraries exist for image processing. Most popular library is OpenCV. Still we should not use this first, we should learn the algorithms, I will use numpy for manipulating the image data.
Note-: I will use OpenCV just to load image data (matrix form)
#Example 1 (How to load image data) [example1.py]
import cv2
imageData=cv2.imread("1.jpg")
print(imageData.shape)
#imread() is function which will read image data and will return a numpy multidimensional array. We need to pass path of the image to the function is string form.
In output we can see that there is a matrix which has some numbers of range 0–255 which is value of content of R/G/B. At last line there is a tuple which is shape of the matrix that is 337x600 and 3 is every cell contains 3 elements.
NOTE-: OpenCV has been made such that pixel are not in form of RBG, actually they are in BGR form that means content of Red color is at 3rd element and Blue is at 1st element and Green is at 2nd element.
#Example 2 (Applying gray scale algorithm)- Average Method
import cv2
imageData=cv2.imread("1.jpg") #Reading Image Data
for r in range(imageData.shape[0]):
for c in range(imageData.shape[1]):
pixel=imageData[r][c] #tuple containing RGB value
red=int(pixel[2])
green=int(pixel[1])
blue=int(pixel[0])
avg=(red+blue+green)//3
imageData[r][c]=(avg,avg,avg)
cv2.imwrite("2.jpg",imageData)
#Saving image, imwrite() will write the imageData into a file and #will save with the name passed as 1st argument.
Now, rotation of image is a very common act that we perform. For example, an image has dimensions 500x720 after a rotation of 90 degrees, its dimensions will be 720x500 that means the matrix of 500x720 got transformed into 720x500. That’s it you are done with algorithm of rotation. Lets implement it straight away.
import cv2
import numpy
imageData=cv2.imread("1.jpg")
oldR=imageData.shape[0] #storing number of rows in matrix
oldC=imageData.shape[1] #storing number of columns in matrix
newImage=numpy.zeros((oldC,oldR,3))
#creating a matrix of rows=old columns and columns=old rows
#This will result in the rotation which we are required to do.
#print(imageData.shape) you can verify it by this line
#print(newImage.shape) and by this line
print("Processing")
r=0
while r<oldR:
c=0
while c<oldC:
newImage[c][oldR-r-1]=imageData[r][c]
c+=1
r+=1
cv2.imwrite("10.jpg",newImage)
print("done")