Digital Image Processing: 1 Objectives
Digital Image Processing: 1 Objectives
1 Objectives
The first laboratory will introduce some conventional libraries in digital image processing and student will
learn to perform several basic operations on digital image.
Anh H. Vo - vohoanganh@tdtu.edu.vn 1
Ton Duc Thang University
Faculty of Information Technology
1 import cv2 as cv
2
3
4 img = cv. imread (’lena.jpg ’)
5 cv2. imshow (’First example ’, img)
6 cv2. waitKey (0)
Listing 1: Example shows how to load an image and show it on screen with OpenCV
Anh H. Vo - vohoanganh@tdtu.edu.vn 2
Ton Duc Thang University
Faculty of Information Technology
cv2.calcHist function in Opencv library also can be used to calculate the histogram of an image instead
of np.histogram
1 import numpy as np
2 import cv2
3 from matplotlib import pyplot as plt
Anh H. Vo - vohoanganh@tdtu.edu.vn 3
Ton Duc Thang University
Faculty of Information Technology
4
5 img = cv2. imread (’lena.jpg ’)
6 color = (’b’,’g’,’r’)
7 for i,col in enumerate (color):
8 histr = cv2. calcHist ([ img ],[i],None ,[256] ,[0 ,256])
9 plt.plot(histr ,color = col)
10 plt.xlim ([0 ,256])
11 plt.show ()
Listing 6: Example uses cv2.calcHist function to calculate the histogram of an image
Similarly, Image.histogram function of PIL library could be used to compute the histogram of an
image, and pyplot.bar is used to show the histogram result on the screen.
1 import matplotlib . pyplot as plt
2 from PIL import Image
3
4 def getRed ( redVal ):
5 return ’#%02x%02x%02x’ % (redVal , 0, 0)
6
7 image = Image.open("lena.jpg")
8 histogram = image. histogram ()
9 # Take only the Red counts
10 l1 = histogram [0:256]
11 # Take only the Blue counts
12 l2 = histogram [256:512]
13
14 # Take only the Green counts
15 l3 = histogram [512:768]
16 plt. figure (0)
17 # Red histogram
18 for i in range (0, 256):
19 plt.bar(i, l1[i], color = getRed (i),
20 edgecolor = getRed (i), alpha =0.3)
21
22 plt.show ()
Listing 7: Example uses the histogram function with Pillow library
Question:
1. Write a program to show the histogram of the blue channel.
2. Write a program to show the histogram of the green channel.
Anh H. Vo - vohoanganh@tdtu.edu.vn 4
Ton Duc Thang University
Faculty of Information Technology
1 import cv2
2 img = cv2. imread (’ the scream .jpg ’)
3 blank = np. zeros ((200 ,200 ,3) ,np.uint8)
4 cv2. rectangle (blank ,(20 ,20) ,(50 ,50) ,(255 ,0 ,0) ,3)
5 cv2. putText (blank ,’Hello world ’ ,(20 ,30) ,
6 cv2. FONT HERSHEY SIMPLEX ,1 ,(0 ,255 ,0))
7 cv2. imshow (’Hello ’,blank)
8 cv2. waitKey (0)
9 cv2. destroyAllWindows ()
Listing 9: Example draws on image of OpenCV library
• To draw a text on an image: cv2.putText(image, ’Information’, (x, y), font, fontscale, (R, G,
B), thickness, cv2.LINE AA)
Anh H. Vo - vohoanganh@tdtu.edu.vn 5
Ton Duc Thang University
Faculty of Information Technology
1 import cv2
2 img = cv2. imread (’ the scream .jpg ’)
3 blank = np. zeros ((200 ,200 ,3) ,np.uint8)
4 wb ,hb , cb = blank.shape
5 w,h,c = img.shape
6 img [10: wb +10 ,10: hb +10 ,:] = blank [: ,: ,:]
7 cv2. imshow (’Hello ’,img)
8 cv2. waitKey (0)
Listing 11: Example shows how to crop a region image with OpenCV library
Anh H. Vo - vohoanganh@tdtu.edu.vn 6
Ton Duc Thang University
Faculty of Information Technology
4 Exercises
Exercise 1: Write a program to split an input image with RGB space into three images which present on
Red, Green, Blue channels and then save on your drive.
Exercise 2: Write a program to show the histogram of images for each channel which is splitted from the
previous exercise.
Exercise 3: Write a program to crop a region of input image and save it:
• PIL library (hint: image.crop((x, y, w, h)) function )
• OpenCV library (hint: image[y : y + h, x : x + h] with x, y are vertical, horizontal value coordinate
plane of image and w, h are the width and height of region)
Exercise 4: Write a program to draw a rectangle, circle, eclipse shape on image and put text Hello world
on an input image by OpenCV library.
• To draw a circle, you need its center coordinates, radius and thickness.
1 cv2. circle (img ,(xc , yc), r, (0 ,0 ,255) , −1)
• To draw a rectangle, you need top-left corner and bottom-right corner of rectangle.
1 cv2. rectangle (img ,(384 ,0) ,(510 ,128) ,(0 ,255 ,0) ,3)
• To draw the ellipse, we need to pass several arguments. One argument is the center location
(x,y). Next argument is axes lengths (major axis length, minor axis length). angle is the angle of
rotation of ellipse in anti-clockwise direction. startAngle and endAngle denotes the starting and
ending of ellipse arc measured in clockwise direction from major axis
1 cv2. ellipse (img ,(256 ,256) ,(100 ,50) ,0,0,180,255, − 1)
Exercise 5: Write a program to overlay a smaller image on a larger image using OpenCV library.
Exercise 6: Write a program to try on many different glasses for an input of face image.
Exercise 7: Write a program to add, subtract, multiply two image and then the results are saved with
format jpg, bmp. What do you observe?
Exercise 8: Write functions to add two images f1 (x) and f2 (x) based on the following equations:
• g(x, y) = (1 − a)f1 (x, y) + af2 (x, y), where a from [0, 1] (Image blending)
• g(x, y) = a.f1 (x, y) + b.f2 (x, y) + k, where a, b from [0, 1] and k = 0
Exercise 9: Write a function to compute a mean image from many different images and than it is saved
on your drive.
Anh H. Vo - vohoanganh@tdtu.edu.vn 7
Ton Duc Thang University
Faculty of Information Technology
5 References
1. Reference 1
2. Reference 2
Anh H. Vo - vohoanganh@tdtu.edu.vn 8