Project 1
#Script:
import cv2
import numpy as np
def image_read(image_path):
image=cv2.imread(image_path)
return image
def image_display(window_name,image):
cv2.imshow(window_name,image)
cv2.waitKey()
cv2.destroyAllWindows()
def image_rotation(image,center,angle,scale):
M_rotation=cv2.getRotationMatrix2D(center,angle,scale)
rotated_image=cv2.warpAffine(image,M_rotation,(image.shape[0],image.shape[1]))
return rotated_image
image=cv2.imread('fu_car.jpg')
length,width,channels = image.shape
center = (length/2,width/2)
angle = 140
scale = 0.5
rotated_image= image_rotation(image,center,angle,scale)
image_display('rotated_car_screen',rotated_image)
cv2.waitKey()
cv2.destroyAllWindows()
def image_resize(image,dimensions,method_number):
methods = [cv2.INTER_AREA,cv2.INTER_LINEAR,cv2.INTER_CUBIC]
resize_method=methods[method_number]
resized_image=cv2.resize(image,dimensions,interpolation=method_number)
return resized_image
image=rotated_image
print(rotated_image.shape)
length,width, channels= rotated_image.shape
scaled_dimensions = (length//2,width//2)
method = 2
resized_image=image_resize(rotated_image,scaled_dimensions,method)
image_display('resized_car_screen',resized_image)
cv2.waitKey()
cv2.destroyAllWindows()
def crop_image(image,dimensions):
area_image=image.shape
w_s , w_e , h_s , h_e = dimensions
imag_width = [w_s,w_e]
imag_height = [h_s,h_e]
np.clip(imag_width,a_min=0,a_max=image.shape[1])
np.clip(imag_height,a_min=0,a_max=image.shape[0])
cropped_image=image[imag_width[0]:imag_width[1],imag_height[0]:imag_height[1]]
return cropped_image
image=cv2.imread('fu_car.jpg')
crop_dimensions=[200,500,0,800]
cropped_image=crop_image(image,crop_dimensions)
image_display('cropped_car_screen',cropped_image)
cv2.waitKey()
cv2.destroyAllWindows()
#Comments:
- the numpy will read the image in matrix or array format where it will have pixels ordered in rows, cloumns and channels
- def will help us define the function, which is simply a operator of doing certain function, and it can be called in mulitple times when user gives input#
- cv2.image , the inbuilt function of opencv, used to read a image in jpg format from the desired location and we are returning the image. the def fucntion here takes one argument
- the display function used to display the image we have read from the location, and it takes two arguments. one is the window name and another one source image
- the waitkey and destroyallwindows fucntion used to display the image till user action, so the user can define how long the image should be displayed and destryallwindows makes sure all the output are closed and that saves the memory usage in code execution
- The image rotation requires the cv2 function called cv2.getRoationMatrix2D and which takes the arguments such as source image, center of the image and angle and scaling. on an identity matrix , the rotation basis are represented in numerical angle values.
- the warpaffine function takes the matrix argument as input and dimension of the image to do the rotation operation
- the dimensions for the image is read from the image.shape function such as width and height along with the number of channels.
- to find out the center, the height and width is divided by 2, so that center will be calculated from the total height and width of the original image.
- the resize function takes three arguments such as source image, dimensions and the method of interpolation
- here for this resizing there are 3 methods are considred, and listed in array for indexing. the cv2.resize takes 3 arguments has image , dimensions , interpolation=method.
- the def crop function has two arguments , one is dimensions and another one is image.
- for cropping image we can have four points, starting and end of height and starting and end of width and listed in array
- The list of dimensions are clipped using np.clip function , it requires an source array list, the minimum value of array and maximum value of array. the minimum and maximum taken from dimenions array of the source image