You are on page 1of 11

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING


COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing

Lab Manual No 03

Image Acquisition, Manipulation and Basic Operation on Images

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Learning Outcomes:-

• Introduction to Basic Image Processing Concepts


• Introduction to OpenCV and its installation
• Reading, Writing and Displaying Images
• Getting the dimensions of the image
• Resizing the Image (Down sampling and Up sampling)

Read and Display Image:

To read and display image using OpenCV Python, you could use cv2.imread() for reading image
to a variable and cv2.imshow() to display the image in a separate window.

Syntax – cv2.imread():

cv2.imread(/complete/path/to/image,flag)

• First argument is complete path to the image along with the extension.
• Second argument is an optional flag which could be any one of the following:
cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will
be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode

Returns numpy array, containing the pixel values. For colored images, each pixel is represented as
an array containing Red, Green and Blue channels. Note that the default flag is
cv2.IMREAD_COLOR.

Syntax – cv2.imshow():

cv2.imshow(window_name, image)

• First argument is name of the window that is displayed when image is shown in.
• Second argument is the image to be shown in the window.

When working with colab, there is a slight change in syntax. Add these two lines of codes

from google.colab.patches import cv2_imshow


cv2_imshow(img)
Example:

import cv2

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

from google.colab.patches import cv2_imshow

img = cv2.imread('opencv_screenshot.jpg')

cv2_imshow(img)

cv2.waitKey(0) # waits until a key is pressed


cv2.destroyAllWindows() # destroys the window showing image

OpenCV Python Save Image – cv2.imwrite():

When working with OpenCV Python, images are stored in numpy ndarray. To save an image to
the local file system, use cv2.imwrite() function of opencv python library.

Syntax of cv2.imwrite():

Following is the syntax of cv2.imwrite() function

cv2.imwrite('/path/to/destination/image.png',image)

where,

• First Argument is Path to the destination on file system, where image is ought to be
saved.
• Second Argument is ndarray containing image

Example:

import cv2

from google.colab.patches import cv2_imshow

img = cv2.imread('opencv_screenshot.jpg')

cv2.imwrite('image.png',img)

OpenCV Python – Get Image Size:

When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape
or size, use ndarray.shape to get the dimensions of the image. Then, you can use index on the
dimensions variable to get width, height and number of channels for each pixel.

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Example:

img = cv2.imread('/path/to/image.png')

dimensions = img.shape

print(dimensions)

img.shape returns (Height, Width, Number of Channels)

where

• Height represents the number of pixel rows in the image or the number of pixels in each
column of the image array.
• Width represents the number of pixel columns in the image or the number of pixels in
each row of the image array.
• Number of Channels represents the number of components used to represent each pixel.

OpenCV Resize image using cv2.resize():

Resizing an image means changing the dimensions of it, be it width alone, height alone or both.
Also, the aspect ratio of the original image could be preserved in the resized image. To resize an
image, OpenCV provides cv2.resize() function.

Syntax of cv2.resize():

Following is the syntax of resize function in OpenCV:

cv2.resize(src, dsize, dst, fx, fy, interpolation)

The description about the parameters of resize function.

• src [required] source/input image


• dsize [required] desired size for the output image
• fx [optional] scale factor along the horizontal axis
• fy [optional] scale factor along the vertical axis
• interpolation [optional] flag that takes one of the following methods. INTER_NEAREST
– a nearest-neighbor interpolation INTER_LINEAR – a bilinear interpolation (used by
default) INTER_AREA – resampling using pixel area relation. It

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

may be a preferred method for image decimation, as it gives moire’-free results. But when
the image is zoomed, it is similar to the INTER_NEAREST method. INTER_CUBIC – a
bicubic interpolation over 4×4 pixel neighborhood INTER_LANCZOS4 – a Lanczos
interpolation over 8×8 pixel neighborhood

A complete Example Code:

import cv2

from google.colab.patches import cv2_imshow

img = cv2.imread('opencv_screenshot.jpg', 0)

print(img)

cv2_imshow(img)

k = cv2.waitKey(5000)

if k == 27:

cv2.destroyAllWindows()

elif k == ord('s'):

cv2.imwrite('my_image.jpg', img)

cv2.destroyAllWindows()

# get dimensions of image

dimensions = img.shape

# height, width, number of channels in image

height = img.shape[0]

width = img.shape[1]

#channels = img.shape[2]

print('Image Dimension : ',dimensions)

print('Image Height : ',height)

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

print('Image Width : ',width)

#print('Number of Channels : ',channels)

#Resizing the Image (Downsizing the Image)

scale_percent = 60 # percent of original size

width = int(img.shape[1] * scale_percent / 100)

height = int(img.shape[0] * scale_percent / 100)

dim = (width, height)

# resize image

resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

print('Resized Dimensions : ',resized.shape)

cv2_imshow(resized)

cv2.waitKey(0)

cv2.destroyAllWindows()

#Upscale with resize()

scale_percent = 220 # percent of original size

width = int(img.shape[1] * scale_percent / 100)

height = int(img.shape[0] * scale_percent / 100)

dim = (width, height)

# resize image

resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

print('Resized Dimensions : ',resized.shape)

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

cv2_imshow(resized)

cv2.waitKey(0)

cv2.destroyAllWindows()

#Do not preserve the aspect ratio

# Resize only width

width = 440

height = img.shape[0] # keep original height

dim = (width, height)

# resize image

resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)


print('Resized Dimensions : ',resized.shape)

cv2_imshow(resized)

cv2.waitKey(0)

cv2.destroyAllWindows()

#Resize only height

width = img.shape[1] # keep original width

height = 440

dim = (width, height)

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

# resize image

resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

print('Resized Dimensions : ',resized.shape)

cv2_imshow(resized)

cv2.waitKey(0)

cv2.destroyAllWindows()

#Resize to specific width and height

width = 350

height = 450

dim = (width, height)

# resize image

resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)


print('Resized Dimensions : ',resized.shape)

cv2_imshow(resized)

cv2.waitKey(0)

cv2.destroyAllWindows()

Lab Tasks:

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

1. Pick a greyscale image, say your own image. Develop a python


function to write it to files of type JPEG, PNG and BMP. What
are the sizes of those files?
2. Read an image and resize it to dimension of 256 x 256. Next
split the image into four equal parts and swap the diagonal
parts of the image. Display both the input and output image.
3. Write a python code that will do the following
a. Read any gray scale image.
b. Display that image.
c. Again, display the image such that the pixels having
intensity values below than 50 will display as black and
pixels having intensity values above than 150 will display
as white. And the pixels between these will display as it
is.
4. Write a Python code that reads a gray scale image and
generates the flipped image of original image. Your output
should be like the one given below. Save the resultant image.

5. Write a python code in which the output should be as follows.

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

Also save the resultant image.

6. Write 3 different Python functions that can create the images


given below. Code them in such so that the size of the image
itself, size of boxes, size of lines and number of horizontal
and vertical lines are entered by the user.

Digital Image Processing Lab Instructor: - Sheharyar Khan


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

7. Write a function to create a white image size entered by the


user and then create 4 boxes of Black, Blue, Green and Red
respectively on each corner of the image as shown below. The
size of the colored boxes should be 1/10th the size of the
image. (HINT: the arrays of ones and zeros can be in more than 2
dimensions)

THINK!!
1. What will be the number of dimensions of a grayscale image if
opened as colored?
2. Is image a list, tuple or an array?
3. A black and white image can only have what gray levels?
4. Can we flip the image using just one line python code?
5. What is the difference between cv.write and printing an image?

8. Read any image that you want and save it in gray scale. Now mirror
the image that you have read at center i.e., the upper half of the
image should be the copy of the lower half but mirrored.
Write the image to the disk. See the image below.

Digital Image Processing Lab Instructor: - Sheharyar Khan

You might also like