AIR UNIVERSITY
Department of Electrical and Computer Engineering
Digital Image Processing Lab
Lab #5: Some useful OpenCV functions
Student Name: Umar Mustafa
Roll No: 200365
Instructor: Engr. M. Farooq Khan
In Lab Tasks
To Do: Read a colored image, access specific location pixel values (RGB values)
(480, 640, 3)
To Do: Read a grayscale image, access specific location pixel values (GrayScale)
array([[ 54, 65, 70, ..., 73, 68, 71],
[ 77, 92, 85, ..., 72, 74, 78],
[100, 101, 104, ..., 79, 77, 76],
...,
[184, 186, 185, ..., 170, 173, 173],
[185, 185, 183, ..., 170, 170, 172],
[180, 180, 180, ..., 171, 168, 170]], dtype=uint8)
File "C:UsersuserAppDataLocalTempipykernel_54083878267003.py", line 1
To Do: Display a text on Image
^
SyntaxError: invalid syntax
Rotate:
cv2.rotate() method is used to rotate a 2D array in multiples of 90 degrees. The function cv::rotate rotates the array in three different ways.
1. Rotate by 90 degrees clockwise: cv2.rotate(image to be rotated, cv2.ROTATE_90_CLOCKWISE)
2. Rotate by 180 degrees clockwise: cv2.ROTATE_180
3. Rotate by 270 degrees clockwise : cv2.ROTATE_90_COUNTERCLOCKWISE
Concatenation of images:
To concatenate images vertically and horizontally with Python, cv2 library comes with two functions as:
1. hconcat(): It is used as cv2.hconcat() to concatenate images horizontally. Here h means horizontal. cv2.hconcat() is used to combine images of same height horizontally.
2. vconcat(): It is used as cv2.vconcat() to concatenate images vertically. Here v means vertical. cv2.vconcat() is used to combine images of same width vertically.
Arithmetic and Logical Operators- Bitwise AND, OR, NOR, XOR
Arithmetic Operations like Addition, Subtraction, and Bitwise Operations(AND, OR, NOT, XOR) can be applied to the input images
1. Addition
2. Subtraction
Bitwise operations are used in image manipulation and used for extracting essential parts in the image. Bitwise operations include:
1. Bitwise AND
2. Bitwise OR
3. Bitwise XOR
4. Bitwise NOT
5. Addition
• Syntax: cv2.add(img1, img2) But adding the pixels is not an ideal situation. So, we use cv2.addweighted(). Remember, both images should be of equal size and depth.
• Syntax: cv2.addWeighted(img1, wt1, img2, wt2, gammaValue)
Parameters:
• img1: First Input Image array(Single-channel, 8-bit or floating-point)
• wt1: Weight of the first input image elements to be applied to the final image
• img2: Second Input Image array(Single-channel, 8-bit or floating-point)
• wt2: Weight of the second input image elements to be applied to the final image
• gammaValue: Measurement of light
2.Subtraction of Image:
Just like addition, we can subtract the pixel values in two images and merge them with the help of cv2.subtract(). The images should be of equal size and depth.
Syntax: cv2.subtract(image1, image2)
1. AND: A bitwise AND is true if and only if both pixels are greater than zero.
2. OR: A bitwise OR is true if either of the two pixels is greater than zero.
3. XOR: A bitwise XOR is true if and only if one of the two pixels is greater than zero, but not both.
4. NOT: A bitwise NOT inverts the “on” and “off” pixels in an image. Syntax:
bitwiseAnd = cv2.bitwise_and(rectangle, circle)
bitwiseOr = cv2.bitwise_or(rectangle, circle)
bitwiseXor = cv2.bitwise_xor(rectangle, circle)
bitwiseNot = cv2.bitwise_not(circle)
Post Lab Tasks
Task 1:
Capture your own image using openCV and display it in gray scale and RGB scale and perform following operations:
1) Find dimension of an image.
2) What are the values of pixel at location (10,10) , (50,100) and (120,201) for both type of images?
3) Read Image from Height 50-200 Pixel and Width 10-40 Pixel
4) Draw a rectangle on an image of red color and line width 5.
5) Write your name and registration number on your image.
1) Find dimension of an image
(480, 640, 3)
2) What are the values of pixel at location (10,10) , (50,100) and (120,201) for both type of images?
c(10,10) = [[[178 201 209]
[179 202 208]
[182 204 202]
...
[ 78 60 60]
[ 66 62 63]
[ 85 90 83]]
[[178 197 203]
[179 200 203]
[183 205 202]
...
[ 68 62 48]
[ 74 53 52]
[100 95 99]]
[[189 200 206]
[180 202 199]
[185 204 202]
...
[ 70 62 60]
[ 68 60 58]
[ 83 84 88]]
...
[[ 46 70 82]
[ 92 111 125]
[ 72 91 108]
...
[ 65 75 66]
[ 64 69 65]
[ 51 57 47]]
[[ 58 83 80]
[ 95 116 121]
[106 130 134]
...
[ 93 94 88]
[ 69 70 56]
[ 62 59 52]]
[[ 71 97 120]
[ 61 76 81]
[ 44 69 91]
...
[ 88 96 85]
[ 67 66 61]
[ 62 65 58]]]
d(50,100) = [[[178 201 209]
[194 214 212]
[197 218 211]
[ 73 52 49]
[ 86 75 71]
[114 104 103]
[ 90 75 68]]
[[180 200 199]
[197 213 210]
[201 216 209]
[ 68 47 44]
[ 87 66 71]
[ 95 95 87]
[ 91 84 76]]
[[181 200 194]
[191 212 203]
[198 214 201]
[ 69 48 45]
[ 82 71 65]
[ 78 73 67]
[105 106 90]]
[[184 199 194]
[189 210 203]
[195 211 198]
[ 12 10 13]
[ 12 11 17]
[ 97 78 64]
[125 120 100]]
[[175 195 193]
[184 205 198]
[195 208 201]
[ 49 36 28]
[ 24 13 17]
[100 87 68]
[133 134 118]]
[[164 192 178]
[180 200 191]
[183 199 189]
[124 106 96]
[ 44 34 32]
[120 118 105]
[144 150 138]]
[[ 52 34 30]
[ 57 42 37]
[ 41 34 42]
[ 68 51 43]
[ 85 61 49]
[ 16 16 16]
[122 130 117]]
[[ 60 42 40]
[ 27 34 44]
[150 169 167]
[ 69 56 50]
[ 16 12 13]
[ 28 17 15]
[103 111 96]]
[[ 47 28 22]
[ 90 103 109]
[110 59 68]
[135 122 88]
[ 37 15 18]
[ 96 46 58]
[100 107 99]]
[[ 46 70 82]
[ 51 63 75]
[120 59 77]
[ 43 11 14]
[107 49 61]
[108 56 58]
[ 80 82 77]]]
e(120,201)= [[[178 201 209]
[195 218 210]
[ 88 78 76]
[ 76 67 52]]
[[181 199 199]
[203 213 205]
[ 21 19 24]
[ 96 90 78]]
[[169 195 182]
[188 201 192]
[ 34 27 34]
[107 110 91]]
[[ 34 40 52]
[119 135 134]
[ 37 26 24]
[112 116 101]]]
3) Read Image from Height 50-200 Pixel and Width 10-40 Pixel
4) Draw a rectangle on an image of red color and line width 5.
5) Write your name and registration number on your image
Task 2:
Write python code to create a border around your own captured image using OpenCV. The length of top and bottom border must be 10% of the original height of the image and length of right
and left borders must be 10% of the original horizontal length of the image. Save the image
Example:
height = 480
length = 640
Task 3
Write a function to create a white image of 500x500 (or any other size entered by the user) and then create 4 boxes of Red, Green, Blue and Black respectively on each corner of the image
as shown below. The size of the colored boxes should be 1/8th the size of the image.
Example:
Task 4
Use the given below images as your input images and perform all arithmetic and logical operations. Display and state your observations for each.
(408, 411, 3)
(410, 413, 3)
Addition
Subtraction
Bitwise AND
Bitwise OR
Bitwise XOR
Bitwise NOT
Task 5
Capture your own image through webcam and save it.
Perform all the types of flipping on the captured image. Show the flipped images.
Perform all the types of rotations on the captured image. Show the rotated images.
Concatenate the original and flipped images into 1 image. Similarly concatenate all the rotated images into 1 images.
Flipping
Rotation
Concatenating Flipped & Original Images
Concatenating Rotated & Original Images
Conclusion
OpenCV is a Python library that allows you to perform image processing and computer vision tasks.
It provides a wide range of features, including object detection, face recognition, and tracking.
I also learnt about differnt operations like rotation and concatenation using open Cv functions.
In [2]: import matplotlib.pyplot as plt
import numpy as np
import cv2
from IPython import display
from PIL import Image
In [8]: capture = cv2.VideoCapture(0)
while True:
ret,frame = capture.read()
cv2.imshow('color',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.imwrite("umar.jpg",frame)
break
capture.release()
cv2.destroyAllWindows()
In [17]: # Load the image
image = plt.imread('DIP.jpg')
# Display the image
plt.imshow(image)
# Show the plot
plt.show()
In [21]: image.shape
Out[21]:
In [22]: gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(gray, cmap = 'gray')
plt.show()
In [24]: graypix = gray[::5,::5]
graypix
Out[24]:
In [25]: output = image.copy()
rectangle = cv2.rectangle(output, (10, 10),(635, 470), (0, 255, 0), 5)
plt.imshow(rectangle)
plt.show()
In [33]: To Do: Display a text on Image
Hint:
putText() Function takes in 7 arguments –
Image
Text to be displayed
Bottom-left corner co-ordinates, from where the text should start
Font
Font size
Color (BGR format)
Line width
output = img.copy()
text = cv2.putText(output, 'Text you want to display', (120, 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
In [45]: output1 = output.copy()
text = cv2.putText(output1, 'Umar Mustafa', (20,460), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
plt.imshow(text)
plt.show()
In [ ]: Flip:
We can flip an image around either the x-axis, y-axis, or even both.
Basic Syntax is :
flipped = cv2.flip(image, value)
Value is 1 for horizontal flipping. Value is 0 for vertical flipping. Value is -1 for both axis.
In [49]: flipped = cv2.flip(text, 1)
plt.imshow(flipped)
plt.show()
In [56]: ninty_degrees = cv2.rotate(text, cv2.ROTATE_90_CLOCKWISE)
one80_degrees = cv2.rotate(text, cv2.ROTATE_180)
two70_degrees = cv2.rotate(text, cv2.ROTATE_90_COUNTERCLOCKWISE)
plt.subplot(331)
plt.imshow(ninty_degrees)
plt.subplot(332)
plt.imshow(one80_degrees)
plt.subplot(333)
plt.imshow(two70_degrees)
plt.show()
In [63]: a = cv2.hconcat([output1,text])
b = cv2.vconcat([output1,text])
plt.subplot(221)
plt.imshow(a)
plt.subplot(222)
plt.imshow(b)
plt.show()
In [ ]:
In [64]: capture = cv2.VideoCapture(0)
while True:
ret,frame = capture.read()
cv2.imshow('color',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.imwrite("umar.jpg",frame)
break
capture.release()
cv2.destroyAllWindows()
In [67]: # Load the image
img = plt.imread('umar.jpg')
# Display the image
plt.imshow(img)
# Show the plot
plt.show()
In [77]: gray1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray1, cmap = 'gray')
plt.show()
In [68]: img.shape
Out[68]:
In [71]: c = img[::10,::10]
d = img[::50,::100]
e = img[::120,::201]
print(f"c(10,10) = {c}")
print(f"d(50,100) = {d}")
print(f"e(120,201)= {e}")
In [76]: roi = img[10:40,50:200]
plt.imshow(roi)
plt.show()
In [75]: output2 = img.copy()
rect = cv2.rectangle(output2, (10, 10),(635, 470), (255, 0, 0), 5)
plt.imshow(rect)
plt.show()
In [74]: output3 = img.copy()
text = cv2.putText(output3, 'Name: Umar Mustafa, Reg_Id: 200365', (20,460), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
plt.imshow(text)
plt.show()
In [86]: # get the image dimensions
height, length, channels = img.shape
print(f"height = {height}")
print(f"length = {length}")
In [95]: # calculate the dimensions of the border
border_width = int(length * 0.1)
border_height = int(height * 0.1)
# create a border around the image
border = cv2.copyMakeBorder(img, border_height, border_height, border_width, border_width, cv2.BORDER_CONSTANT)
plt.imshow(border)
plt.show()
In [3]: from PIL import Image, ImageDraw
def create_image_with_boxes(size):
# create a new white image
img = Image.new("RGB", (size, size), "white")
# calculate the size of each colored box
box_size = size // 8
# create a new drawing context
draw = ImageDraw.Draw(img)
# add red box to top left corner
draw.rectangle((0, 0, box_size, box_size), fill="red")
# add green box to top right corner
draw.rectangle((size-box_size, 0, size, box_size), fill="green")
# add blue box to bottom right corner
draw.rectangle((size-box_size, size-box_size, size, size), fill="blue")
# add black box to bottom left corner
draw.rectangle((0, size-box_size, box_size, size), fill="black")
# return the final image
return img
image = create_image_with_boxes(500)
plt.imshow(image) # Display the image
plt.show() # Show the plot
In [9]: # Load the image
#iimg1 = plt.imread('iimg1.png')
#iimg2 = plt.imread('iimg2.png')
iimg1 = cv2.imread('F:/Semester 6/Signal Processing Lab/iimg1.png')
iimg2 = cv2.imread('F:/Semester 6/Signal Processing Lab/iimg2.png')
plt.subplot(221)
plt.imshow(iimg1)
plt.subplot(222)
plt.imshow(iimg2)
plt.show()
In [18]: g = iimg1.shape
h = iimg2.shape
print(g)
print(h)
In [20]: wt1 = 0.5
wt2 = 0.5
gammaValue = 0
# Resize the arrays to a common size
iimg1 = cv2.resize(iimg1, (410, 410), interpolation=cv2.INTER_AREA)
iimg2 = cv2.resize(iimg2, (410, 410), interpolation=cv2.INTER_AREA)
blended = cv2.addWeighted(iimg1, wt1, iimg2, wt2, gammaValue)
plt.imshow(blended)
plt.show()
In [23]: subtract = cv2.subtract(iimg1, iimg2)
plt.imshow(subtract)
plt.show()
In [24]: bitwiseAnd = cv2.bitwise_and(iimg1, iimg2)
plt.imshow(bitwiseAnd)
plt.show()
In [25]: bitwiseOr = cv2.bitwise_or(iimg1, iimg2)
plt.imshow(bitwiseOr)
plt.show()
In [26]: bitwiseXor = cv2.bitwise_xor(iimg1, iimg2)
plt.imshow(bitwiseXor)
plt.show()
In [27]: bitwiseNot = cv2.bitwise_not(iimg1)
plt.imshow(bitwiseNot)
plt.show()
In [5]: capture = cv2.VideoCapture(0)
while True:
ret,frame = capture.read()
cv2.imshow('color',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.imwrite("myself.jpg",frame)
break
capture.release()
cv2.destroyAllWindows()
In [17]: image1 = plt.imread('myself.jpg') # Load the image
plt.imshow(image1) # Display the image
plt.title('Original Image')
plt.show() # Show the plot
In [18]: flipped1 = cv2.flip(image1, 1) # Horizantal Flipping
flipped2 = cv2.flip(image1, 0) # Vertical Flipping
flipped3 = cv2.flip(image1, -1) # Flipping around both axis
plt.subplot(331)
plt.imshow(flipped1)
plt.title('Horizantal Flipped')
plt.subplot(332)
plt.imshow(flipped2)
plt.title('Vertical Flipped')
plt.subplot(333)
plt.imshow(flipped3)
plt.title('Flipped H&V')
plt.show() # Show the plot
In [25]: ninty_degrees1 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
one80_degrees1 = cv2.rotate(image1, cv2.ROTATE_180)
two70_degrees1 = cv2.rotate(image1, cv2.ROTATE_90_COUNTERCLOCKWISE)
plt.subplot(331)
plt.imshow(ninty_degrees1)
plt.title('90D Rotate')
plt.subplot(332)
plt.imshow(one80_degrees1)
plt.title('180D Rotate')
plt.subplot(333)
plt.imshow(two70_degrees1)
plt.title('270D Rotate')
plt.show()
In [22]: a1 = cv2.hconcat([flipped1,image1]) # Horizantally Concatenate
b1 = cv2.vconcat([flipped1,image1]) # Vertically Concatenate
a2 = cv2.hconcat([flipped2,image1])
b2 = cv2.vconcat([flipped2,image1])
a3 = cv2.hconcat([flipped3,image1])
b3 = cv2.vconcat([flipped3,image1])
plt.subplot(331)
plt.imshow(a1)
plt.title('H Flipped & H Concatenate')
plt.subplot(332)
plt.imshow(b1)
plt.title('H Flipped & V Concatenate')
plt.subplot(333)
plt.imshow(a2)
plt.title('V Flipped & H Concatenate')
plt.subplot(334)
plt.imshow(b2)
plt.title('V Flipped & V Concatenate')
plt.subplot(335)
plt.imshow(a3)
plt.title('All Flipped & H Concatenate')
plt.subplot(336)
plt.imshow(b3)
plt.title('All Flipped & H Concatenate')
plt.show()
In [43]: # resize the images to a common size
ninty_degrees2 = cv2.resize(ninty_degrees1, (image1.shape[1], image1.shape[0]))
one80_degrees2 = cv2.resize(one80_degrees1, (image1.shape[1], image1.shape[0]))
two70_degrees2 = cv2.resize(two70_degrees1, (image1.shape[1], image1.shape[0]))
a4 = cv2.hconcat([ninty_degrees2,image1]) # Horizantally Concatenate
b4 = cv2.vconcat([ninty_degrees2,image1]) # Vertically Concatenate
a5 = cv2.hconcat([one80_degrees2,image1])
b5 = cv2.vconcat([one80_degrees2,image1])
a6 = cv2.hconcat([two70_degrees2,image1])
b6 = cv2.vconcat([two70_degrees2,image1])
plt.subplot(321)
plt.imshow(a4)
plt.title('(90 Rotated & H Concatenate')
plt.subplot(322)
plt.imshow(b4)
plt.title('90 Rotated & V Concatenate')
plt.subplot(323)
plt.imshow(a5)
plt.title('180 Rotated & H Concatenate')
plt.subplot(324)
plt.imshow(b5)
plt.title('180 Rotated & V Concatenate')
plt.subplot(325)
plt.imshow(a6)
plt.title('270 Rotated & H Concatenate')
plt.subplot(326)
plt.imshow(b6)
plt.title('270 Rotated & V Concatenate')
plt.subplots_adjust(hspace=1 , wspace=3)
plt.show()

CE344L-200365-Lab5.pdf

  • 1.
    AIR UNIVERSITY Department ofElectrical and Computer Engineering Digital Image Processing Lab Lab #5: Some useful OpenCV functions Student Name: Umar Mustafa Roll No: 200365 Instructor: Engr. M. Farooq Khan In Lab Tasks To Do: Read a colored image, access specific location pixel values (RGB values) (480, 640, 3) To Do: Read a grayscale image, access specific location pixel values (GrayScale) array([[ 54, 65, 70, ..., 73, 68, 71], [ 77, 92, 85, ..., 72, 74, 78], [100, 101, 104, ..., 79, 77, 76], ..., [184, 186, 185, ..., 170, 173, 173], [185, 185, 183, ..., 170, 170, 172], [180, 180, 180, ..., 171, 168, 170]], dtype=uint8) File "C:UsersuserAppDataLocalTempipykernel_54083878267003.py", line 1 To Do: Display a text on Image ^ SyntaxError: invalid syntax Rotate: cv2.rotate() method is used to rotate a 2D array in multiples of 90 degrees. The function cv::rotate rotates the array in three different ways. 1. Rotate by 90 degrees clockwise: cv2.rotate(image to be rotated, cv2.ROTATE_90_CLOCKWISE) 2. Rotate by 180 degrees clockwise: cv2.ROTATE_180 3. Rotate by 270 degrees clockwise : cv2.ROTATE_90_COUNTERCLOCKWISE Concatenation of images: To concatenate images vertically and horizontally with Python, cv2 library comes with two functions as: 1. hconcat(): It is used as cv2.hconcat() to concatenate images horizontally. Here h means horizontal. cv2.hconcat() is used to combine images of same height horizontally. 2. vconcat(): It is used as cv2.vconcat() to concatenate images vertically. Here v means vertical. cv2.vconcat() is used to combine images of same width vertically. Arithmetic and Logical Operators- Bitwise AND, OR, NOR, XOR Arithmetic Operations like Addition, Subtraction, and Bitwise Operations(AND, OR, NOT, XOR) can be applied to the input images 1. Addition 2. Subtraction Bitwise operations are used in image manipulation and used for extracting essential parts in the image. Bitwise operations include: 1. Bitwise AND 2. Bitwise OR 3. Bitwise XOR 4. Bitwise NOT 5. Addition • Syntax: cv2.add(img1, img2) But adding the pixels is not an ideal situation. So, we use cv2.addweighted(). Remember, both images should be of equal size and depth. • Syntax: cv2.addWeighted(img1, wt1, img2, wt2, gammaValue) Parameters: • img1: First Input Image array(Single-channel, 8-bit or floating-point) • wt1: Weight of the first input image elements to be applied to the final image • img2: Second Input Image array(Single-channel, 8-bit or floating-point) • wt2: Weight of the second input image elements to be applied to the final image • gammaValue: Measurement of light 2.Subtraction of Image: Just like addition, we can subtract the pixel values in two images and merge them with the help of cv2.subtract(). The images should be of equal size and depth. Syntax: cv2.subtract(image1, image2) 1. AND: A bitwise AND is true if and only if both pixels are greater than zero. 2. OR: A bitwise OR is true if either of the two pixels is greater than zero. 3. XOR: A bitwise XOR is true if and only if one of the two pixels is greater than zero, but not both. 4. NOT: A bitwise NOT inverts the “on” and “off” pixels in an image. Syntax: bitwiseAnd = cv2.bitwise_and(rectangle, circle) bitwiseOr = cv2.bitwise_or(rectangle, circle) bitwiseXor = cv2.bitwise_xor(rectangle, circle) bitwiseNot = cv2.bitwise_not(circle) Post Lab Tasks Task 1: Capture your own image using openCV and display it in gray scale and RGB scale and perform following operations: 1) Find dimension of an image. 2) What are the values of pixel at location (10,10) , (50,100) and (120,201) for both type of images? 3) Read Image from Height 50-200 Pixel and Width 10-40 Pixel 4) Draw a rectangle on an image of red color and line width 5. 5) Write your name and registration number on your image. 1) Find dimension of an image (480, 640, 3) 2) What are the values of pixel at location (10,10) , (50,100) and (120,201) for both type of images? c(10,10) = [[[178 201 209] [179 202 208] [182 204 202] ... [ 78 60 60] [ 66 62 63] [ 85 90 83]] [[178 197 203] [179 200 203] [183 205 202] ... [ 68 62 48] [ 74 53 52] [100 95 99]] [[189 200 206] [180 202 199] [185 204 202] ... [ 70 62 60] [ 68 60 58] [ 83 84 88]] ... [[ 46 70 82] [ 92 111 125] [ 72 91 108] ... [ 65 75 66] [ 64 69 65] [ 51 57 47]] [[ 58 83 80] [ 95 116 121] [106 130 134] ... [ 93 94 88] [ 69 70 56] [ 62 59 52]] [[ 71 97 120] [ 61 76 81] [ 44 69 91] ... [ 88 96 85] [ 67 66 61] [ 62 65 58]]] d(50,100) = [[[178 201 209] [194 214 212] [197 218 211] [ 73 52 49] [ 86 75 71] [114 104 103] [ 90 75 68]] [[180 200 199] [197 213 210] [201 216 209] [ 68 47 44] [ 87 66 71] [ 95 95 87] [ 91 84 76]] [[181 200 194] [191 212 203] [198 214 201] [ 69 48 45] [ 82 71 65] [ 78 73 67] [105 106 90]] [[184 199 194] [189 210 203] [195 211 198] [ 12 10 13] [ 12 11 17] [ 97 78 64] [125 120 100]] [[175 195 193] [184 205 198] [195 208 201] [ 49 36 28] [ 24 13 17] [100 87 68] [133 134 118]] [[164 192 178] [180 200 191] [183 199 189] [124 106 96] [ 44 34 32] [120 118 105] [144 150 138]] [[ 52 34 30] [ 57 42 37] [ 41 34 42] [ 68 51 43] [ 85 61 49] [ 16 16 16] [122 130 117]] [[ 60 42 40] [ 27 34 44] [150 169 167] [ 69 56 50] [ 16 12 13] [ 28 17 15] [103 111 96]] [[ 47 28 22] [ 90 103 109] [110 59 68] [135 122 88] [ 37 15 18] [ 96 46 58] [100 107 99]] [[ 46 70 82] [ 51 63 75] [120 59 77] [ 43 11 14] [107 49 61] [108 56 58] [ 80 82 77]]] e(120,201)= [[[178 201 209] [195 218 210] [ 88 78 76] [ 76 67 52]] [[181 199 199] [203 213 205] [ 21 19 24] [ 96 90 78]] [[169 195 182] [188 201 192] [ 34 27 34] [107 110 91]] [[ 34 40 52] [119 135 134] [ 37 26 24] [112 116 101]]] 3) Read Image from Height 50-200 Pixel and Width 10-40 Pixel 4) Draw a rectangle on an image of red color and line width 5. 5) Write your name and registration number on your image Task 2: Write python code to create a border around your own captured image using OpenCV. The length of top and bottom border must be 10% of the original height of the image and length of right and left borders must be 10% of the original horizontal length of the image. Save the image Example: height = 480 length = 640 Task 3 Write a function to create a white image of 500x500 (or any other size entered by the user) and then create 4 boxes of Red, Green, Blue and Black respectively on each corner of the image as shown below. The size of the colored boxes should be 1/8th the size of the image. Example: Task 4 Use the given below images as your input images and perform all arithmetic and logical operations. Display and state your observations for each. (408, 411, 3) (410, 413, 3) Addition Subtraction Bitwise AND Bitwise OR Bitwise XOR Bitwise NOT Task 5 Capture your own image through webcam and save it. Perform all the types of flipping on the captured image. Show the flipped images. Perform all the types of rotations on the captured image. Show the rotated images. Concatenate the original and flipped images into 1 image. Similarly concatenate all the rotated images into 1 images. Flipping Rotation Concatenating Flipped & Original Images Concatenating Rotated & Original Images Conclusion OpenCV is a Python library that allows you to perform image processing and computer vision tasks. It provides a wide range of features, including object detection, face recognition, and tracking. I also learnt about differnt operations like rotation and concatenation using open Cv functions. In [2]: import matplotlib.pyplot as plt import numpy as np import cv2 from IPython import display from PIL import Image In [8]: capture = cv2.VideoCapture(0) while True: ret,frame = capture.read() cv2.imshow('color',frame) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.imwrite("umar.jpg",frame) break capture.release() cv2.destroyAllWindows() In [17]: # Load the image image = plt.imread('DIP.jpg') # Display the image plt.imshow(image) # Show the plot plt.show() In [21]: image.shape Out[21]: In [22]: gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(gray, cmap = 'gray') plt.show() In [24]: graypix = gray[::5,::5] graypix Out[24]: In [25]: output = image.copy() rectangle = cv2.rectangle(output, (10, 10),(635, 470), (0, 255, 0), 5) plt.imshow(rectangle) plt.show() In [33]: To Do: Display a text on Image Hint: putText() Function takes in 7 arguments – Image Text to be displayed Bottom-left corner co-ordinates, from where the text should start Font Font size Color (BGR format) Line width output = img.copy() text = cv2.putText(output, 'Text you want to display', (120, 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2) In [45]: output1 = output.copy() text = cv2.putText(output1, 'Umar Mustafa', (20,460), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) plt.imshow(text) plt.show() In [ ]: Flip: We can flip an image around either the x-axis, y-axis, or even both. Basic Syntax is : flipped = cv2.flip(image, value) Value is 1 for horizontal flipping. Value is 0 for vertical flipping. Value is -1 for both axis. In [49]: flipped = cv2.flip(text, 1) plt.imshow(flipped) plt.show() In [56]: ninty_degrees = cv2.rotate(text, cv2.ROTATE_90_CLOCKWISE) one80_degrees = cv2.rotate(text, cv2.ROTATE_180) two70_degrees = cv2.rotate(text, cv2.ROTATE_90_COUNTERCLOCKWISE) plt.subplot(331) plt.imshow(ninty_degrees) plt.subplot(332) plt.imshow(one80_degrees) plt.subplot(333) plt.imshow(two70_degrees) plt.show() In [63]: a = cv2.hconcat([output1,text]) b = cv2.vconcat([output1,text]) plt.subplot(221) plt.imshow(a) plt.subplot(222) plt.imshow(b) plt.show() In [ ]: In [64]: capture = cv2.VideoCapture(0) while True: ret,frame = capture.read() cv2.imshow('color',frame) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.imwrite("umar.jpg",frame) break capture.release() cv2.destroyAllWindows() In [67]: # Load the image img = plt.imread('umar.jpg') # Display the image plt.imshow(img) # Show the plot plt.show() In [77]: gray1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) plt.imshow(gray1, cmap = 'gray') plt.show() In [68]: img.shape Out[68]: In [71]: c = img[::10,::10] d = img[::50,::100] e = img[::120,::201] print(f"c(10,10) = {c}") print(f"d(50,100) = {d}") print(f"e(120,201)= {e}") In [76]: roi = img[10:40,50:200] plt.imshow(roi) plt.show() In [75]: output2 = img.copy() rect = cv2.rectangle(output2, (10, 10),(635, 470), (255, 0, 0), 5) plt.imshow(rect) plt.show() In [74]: output3 = img.copy() text = cv2.putText(output3, 'Name: Umar Mustafa, Reg_Id: 200365', (20,460), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) plt.imshow(text) plt.show() In [86]: # get the image dimensions height, length, channels = img.shape print(f"height = {height}") print(f"length = {length}") In [95]: # calculate the dimensions of the border border_width = int(length * 0.1) border_height = int(height * 0.1) # create a border around the image border = cv2.copyMakeBorder(img, border_height, border_height, border_width, border_width, cv2.BORDER_CONSTANT) plt.imshow(border) plt.show() In [3]: from PIL import Image, ImageDraw def create_image_with_boxes(size): # create a new white image img = Image.new("RGB", (size, size), "white") # calculate the size of each colored box box_size = size // 8 # create a new drawing context draw = ImageDraw.Draw(img) # add red box to top left corner draw.rectangle((0, 0, box_size, box_size), fill="red") # add green box to top right corner draw.rectangle((size-box_size, 0, size, box_size), fill="green") # add blue box to bottom right corner draw.rectangle((size-box_size, size-box_size, size, size), fill="blue") # add black box to bottom left corner draw.rectangle((0, size-box_size, box_size, size), fill="black") # return the final image return img image = create_image_with_boxes(500) plt.imshow(image) # Display the image plt.show() # Show the plot In [9]: # Load the image #iimg1 = plt.imread('iimg1.png') #iimg2 = plt.imread('iimg2.png') iimg1 = cv2.imread('F:/Semester 6/Signal Processing Lab/iimg1.png') iimg2 = cv2.imread('F:/Semester 6/Signal Processing Lab/iimg2.png') plt.subplot(221) plt.imshow(iimg1) plt.subplot(222) plt.imshow(iimg2) plt.show() In [18]: g = iimg1.shape h = iimg2.shape print(g) print(h) In [20]: wt1 = 0.5 wt2 = 0.5 gammaValue = 0 # Resize the arrays to a common size iimg1 = cv2.resize(iimg1, (410, 410), interpolation=cv2.INTER_AREA) iimg2 = cv2.resize(iimg2, (410, 410), interpolation=cv2.INTER_AREA) blended = cv2.addWeighted(iimg1, wt1, iimg2, wt2, gammaValue) plt.imshow(blended) plt.show() In [23]: subtract = cv2.subtract(iimg1, iimg2) plt.imshow(subtract) plt.show() In [24]: bitwiseAnd = cv2.bitwise_and(iimg1, iimg2) plt.imshow(bitwiseAnd) plt.show() In [25]: bitwiseOr = cv2.bitwise_or(iimg1, iimg2) plt.imshow(bitwiseOr) plt.show() In [26]: bitwiseXor = cv2.bitwise_xor(iimg1, iimg2) plt.imshow(bitwiseXor) plt.show() In [27]: bitwiseNot = cv2.bitwise_not(iimg1) plt.imshow(bitwiseNot) plt.show() In [5]: capture = cv2.VideoCapture(0) while True: ret,frame = capture.read() cv2.imshow('color',frame) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.imwrite("myself.jpg",frame) break capture.release() cv2.destroyAllWindows() In [17]: image1 = plt.imread('myself.jpg') # Load the image plt.imshow(image1) # Display the image plt.title('Original Image') plt.show() # Show the plot In [18]: flipped1 = cv2.flip(image1, 1) # Horizantal Flipping flipped2 = cv2.flip(image1, 0) # Vertical Flipping flipped3 = cv2.flip(image1, -1) # Flipping around both axis plt.subplot(331) plt.imshow(flipped1) plt.title('Horizantal Flipped') plt.subplot(332) plt.imshow(flipped2) plt.title('Vertical Flipped') plt.subplot(333) plt.imshow(flipped3) plt.title('Flipped H&V') plt.show() # Show the plot In [25]: ninty_degrees1 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE) one80_degrees1 = cv2.rotate(image1, cv2.ROTATE_180) two70_degrees1 = cv2.rotate(image1, cv2.ROTATE_90_COUNTERCLOCKWISE) plt.subplot(331) plt.imshow(ninty_degrees1) plt.title('90D Rotate') plt.subplot(332) plt.imshow(one80_degrees1) plt.title('180D Rotate') plt.subplot(333) plt.imshow(two70_degrees1) plt.title('270D Rotate') plt.show() In [22]: a1 = cv2.hconcat([flipped1,image1]) # Horizantally Concatenate b1 = cv2.vconcat([flipped1,image1]) # Vertically Concatenate a2 = cv2.hconcat([flipped2,image1]) b2 = cv2.vconcat([flipped2,image1]) a3 = cv2.hconcat([flipped3,image1]) b3 = cv2.vconcat([flipped3,image1]) plt.subplot(331) plt.imshow(a1) plt.title('H Flipped & H Concatenate') plt.subplot(332) plt.imshow(b1) plt.title('H Flipped & V Concatenate') plt.subplot(333) plt.imshow(a2) plt.title('V Flipped & H Concatenate') plt.subplot(334) plt.imshow(b2) plt.title('V Flipped & V Concatenate') plt.subplot(335) plt.imshow(a3) plt.title('All Flipped & H Concatenate') plt.subplot(336) plt.imshow(b3) plt.title('All Flipped & H Concatenate') plt.show() In [43]: # resize the images to a common size ninty_degrees2 = cv2.resize(ninty_degrees1, (image1.shape[1], image1.shape[0])) one80_degrees2 = cv2.resize(one80_degrees1, (image1.shape[1], image1.shape[0])) two70_degrees2 = cv2.resize(two70_degrees1, (image1.shape[1], image1.shape[0])) a4 = cv2.hconcat([ninty_degrees2,image1]) # Horizantally Concatenate b4 = cv2.vconcat([ninty_degrees2,image1]) # Vertically Concatenate a5 = cv2.hconcat([one80_degrees2,image1]) b5 = cv2.vconcat([one80_degrees2,image1]) a6 = cv2.hconcat([two70_degrees2,image1]) b6 = cv2.vconcat([two70_degrees2,image1]) plt.subplot(321) plt.imshow(a4) plt.title('(90 Rotated & H Concatenate') plt.subplot(322) plt.imshow(b4) plt.title('90 Rotated & V Concatenate') plt.subplot(323) plt.imshow(a5) plt.title('180 Rotated & H Concatenate') plt.subplot(324) plt.imshow(b5) plt.title('180 Rotated & V Concatenate') plt.subplot(325) plt.imshow(a6) plt.title('270 Rotated & H Concatenate') plt.subplot(326) plt.imshow(b6) plt.title('270 Rotated & V Concatenate') plt.subplots_adjust(hspace=1 , wspace=3) plt.show()