In this lesson, we are going to get our hands on with Opencv for python which we installed in the earlier lesson.This tutorial uses the opencv-python library from python. opencv is quite popular with various image processing technique.
Objective
1)Reading a image in Opencv
2)Getting basic stats about tmage
3)Displaying images in opencv
4)Working with resize of image.
5)Flipping images
6)Applying GRAY SCALE to image
7)Saving the images to folders.
Steps :
Before we start getting hands dirty with opencv we'll start by importing the required packages.
A)Importing the library:
import argparse
import cv2
1)Reading the image in opencv
img = cv2.imread('C:\\Users\\venka\\Downloads\\standard_test_images\\lena_color_256.tif')
2)Getting basic stats about the image
The basic stats includes the shape and dtype of image
print('shape:', img.shape)
print('dtype:', img.dtype)
shape: (256, 256, 3)
dtype: uint8
The shape tuple in this case should be interpreted as follows: image height, image width, color channels count.
The cv.imread function also supports optional flags, where users can specify whether conversion to uint8 type should be performed, and whether the image is grayscale or color.The cv.imread function also supports optional flags, where users can specify whether conversion to uint8 type should be performed, and whether the image is grayscale or color.
cv2.imread() - Loads an image from a file.
imshow() - Used to display the loaded image
waitkey(0)-Waits for a pressed key. This one is important else the notebook will get shutdown. 0 - implies the delay
3)Displaying the image
The imshow() method will diplay the image in opencv window.
img = cv2.imread('C:\\Users\\venka\\Downloads\\standard_test_images\\lena_color_256.tif')
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
dtype: uint8
4)Resizing the image
resize() - This method is used to resize the image based on height and width in pixels
w_mult, h_mult = 0.25, 0.5
resized_img = cv2.resize(img, (0, 0), resized_img, w_mult, h_mult)
print('image shape:', resized_img.shape)
width, height = 128, 256
resized_img = cv2.resize(img, (width, height))
print('resized to 128x256 image shape:', resized_img.shape)
w_mult, h_mult = 2, 4
resized_img = cv2.resize(img, (0, 0), resized_img, w_mult, h_mult, cv2.INTER_NEAREST)
print('half sized image shape:', resized_img.shape)
5)Flipping the image
The cv2.flip function is used for mirroring images. It doesn't change the size of an image, but rather swaps the pixels.
img_flip_along_x = cv2.flip(img, 0)
cv2.imshow('img',img_flip_along_x)
cv2.waitKey(0)
cv2.destroyAllWindows()
img_flip_along_y = cv2.flip(img, 1)
cv2.imshow('img',img_flip_along_y)
cv2.waitKey(0)
cv2.destroyAllWindows()
img_flipped_xy = cv2.flip(img, -1)
cv2.imshow('img',img_flip_along_y)
cv2.waitKey(0)
cv2.destroyAllWindows()
6)Applying the GRAY SCALE
IMREAD_GRAYSCALE this property of opencv gets applied on the image.
img = cv2.imread('C:\\Users\\venka\\Downloads\\standard_test_images\\lena_color_256.tif', cv2.IMREAD_GRAYSCALE)
assert img is not None
print('shape:', img.shape)
print('dtype:', img.dtype)
7)Saving the File to folders
Sometimes you want to get feedback from your computer vision algorithm. One way to do so is to store results on a disk. The feedback could be final images, pictures with additional information such as contours, metrics, values and so on, or results of individual steps from a complicated pipeline.
imwrite()-- Used to save image to the given location.
IMWRITE_JPEG_QUALITY-When saving to JPEG format, we can manage the compression process by setting the value of IMWRITE_JPEG_QUALITY. We can set this as any value from 0 to 100. But here, we have a situation where bigger is better. Larger values lead to higher result quality and a lower amount of JPEG artifacts.
cv2.imwrite('C:\\Users\\venka\\Downloads\\standard_test_images\\Lena_compressed.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 0])
EmoticonEmoticon