SlideShare a Scribd company logo
1 of 1
Download to read offline
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()

More Related Content

Similar to CE344L-200365-Lab5.pdf

Introduction to Computer Vision using OpenCV
Introduction to Computer Vision using OpenCVIntroduction to Computer Vision using OpenCV
Introduction to Computer Vision using OpenCVDylan Seychell
 
Templateless Marked Element Recognition Using Computer Vision
Templateless Marked Element Recognition Using Computer VisionTemplateless Marked Element Recognition Using Computer Vision
Templateless Marked Element Recognition Using Computer Visionshivam chaurasia
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphicsroxlu
 
Color image encryption decryption using smt
Color image encryption decryption using smtColor image encryption decryption using smt
Color image encryption decryption using smtZiadAlqady
 
Color image encryption decryption using smt
Color image encryption decryption using smtColor image encryption decryption using smt
Color image encryption decryption using smtZiadAlqady
 
Color image encryption decryption using smt
Color image encryption decryption using smtColor image encryption decryption using smt
Color image encryption decryption using smtZiadAlqady
 
Writeup advanced lane_lines_project
Writeup advanced lane_lines_projectWriteup advanced lane_lines_project
Writeup advanced lane_lines_projectManish Jauhari
 
Digital Image Processing (Lab 06)
Digital Image Processing (Lab 06)Digital Image Processing (Lab 06)
Digital Image Processing (Lab 06)Moe Moe Myint
 
Introduction To Advanced Image Processing
Introduction To Advanced Image ProcessingIntroduction To Advanced Image Processing
Introduction To Advanced Image ProcessingSuren Kumar
 
Intro to computer vision in .net
Intro to computer vision in .netIntro to computer vision in .net
Intro to computer vision in .netStephen Lorello
 
CE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdfCE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdfUmarMustafa13
 
Help me fix the error shown above in my code of image.cpp please~I.pdf
Help me fix the error shown above in my code of image.cpp please~I.pdfHelp me fix the error shown above in my code of image.cpp please~I.pdf
Help me fix the error shown above in my code of image.cpp please~I.pdffedosys
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfssuserb4d806
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALVivek Kumar Sinha
 
Simple Matlab tutorial using matlab inbuilt commands
Simple Matlab tutorial using matlab inbuilt commandsSimple Matlab tutorial using matlab inbuilt commands
Simple Matlab tutorial using matlab inbuilt commandsLakshmi Sarvani Videla
 
Image Classification
Image ClassificationImage Classification
Image ClassificationAnwar Jameel
 
The Ring programming language version 1.3 book - Part 37 of 88
The Ring programming language version 1.3 book - Part 37 of 88The Ring programming language version 1.3 book - Part 37 of 88
The Ring programming language version 1.3 book - Part 37 of 88Mahmoud Samir Fayed
 
Image Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingImage Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingAshok Kumar
 

Similar to CE344L-200365-Lab5.pdf (20)

Introduction to Computer Vision using OpenCV
Introduction to Computer Vision using OpenCVIntroduction to Computer Vision using OpenCV
Introduction to Computer Vision using OpenCV
 
Templateless Marked Element Recognition Using Computer Vision
Templateless Marked Element Recognition Using Computer VisionTemplateless Marked Element Recognition Using Computer Vision
Templateless Marked Element Recognition Using Computer Vision
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphics
 
Color image encryption decryption using smt
Color image encryption decryption using smtColor image encryption decryption using smt
Color image encryption decryption using smt
 
Color image encryption decryption using smt
Color image encryption decryption using smtColor image encryption decryption using smt
Color image encryption decryption using smt
 
Color image encryption decryption using smt
Color image encryption decryption using smtColor image encryption decryption using smt
Color image encryption decryption using smt
 
Writeup advanced lane_lines_project
Writeup advanced lane_lines_projectWriteup advanced lane_lines_project
Writeup advanced lane_lines_project
 
Digital Image Processing (Lab 06)
Digital Image Processing (Lab 06)Digital Image Processing (Lab 06)
Digital Image Processing (Lab 06)
 
Introduction To Advanced Image Processing
Introduction To Advanced Image ProcessingIntroduction To Advanced Image Processing
Introduction To Advanced Image Processing
 
Intro to computer vision in .net
Intro to computer vision in .netIntro to computer vision in .net
Intro to computer vision in .net
 
Pygame presentation
Pygame presentationPygame presentation
Pygame presentation
 
CE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdfCE344L-200365-Lab7.pdf
CE344L-200365-Lab7.pdf
 
Help me fix the error shown above in my code of image.cpp please~I.pdf
Help me fix the error shown above in my code of image.cpp please~I.pdfHelp me fix the error shown above in my code of image.cpp please~I.pdf
Help me fix the error shown above in my code of image.cpp please~I.pdf
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
Simple Matlab tutorial using matlab inbuilt commands
Simple Matlab tutorial using matlab inbuilt commandsSimple Matlab tutorial using matlab inbuilt commands
Simple Matlab tutorial using matlab inbuilt commands
 
Image Classification
Image ClassificationImage Classification
Image Classification
 
The Ring programming language version 1.3 book - Part 37 of 88
The Ring programming language version 1.3 book - Part 37 of 88The Ring programming language version 1.3 book - Part 37 of 88
The Ring programming language version 1.3 book - Part 37 of 88
 
Dip syntax 4
Dip syntax 4Dip syntax 4
Dip syntax 4
 
Image Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image ProcessingImage Processing using Matlab . Useful for beginners to learn Image Processing
Image Processing using Matlab . Useful for beginners to learn Image Processing
 

More from UmarMustafa13

CEA Technical English.pptx
CEA Technical English.pptxCEA Technical English.pptx
CEA Technical English.pptxUmarMustafa13
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
CE344L-200365-Lab6.pdf
CE344L-200365-Lab6.pdfCE344L-200365-Lab6.pdf
CE344L-200365-Lab6.pdfUmarMustafa13
 
CE344L-200365-Lab4.pdf
CE344L-200365-Lab4.pdfCE344L-200365-Lab4.pdf
CE344L-200365-Lab4.pdfUmarMustafa13
 
CE344L-200365-Lab3.pdf
CE344L-200365-Lab3.pdfCE344L-200365-Lab3.pdf
CE344L-200365-Lab3.pdfUmarMustafa13
 
CE344L-200365-Lab8.pdf
CE344L-200365-Lab8.pdfCE344L-200365-Lab8.pdf
CE344L-200365-Lab8.pdfUmarMustafa13
 

More from UmarMustafa13 (7)

CEA Technical English.pptx
CEA Technical English.pptxCEA Technical English.pptx
CEA Technical English.pptx
 
CEA-3-Activity.pptx
CEA-3-Activity.pptxCEA-3-Activity.pptx
CEA-3-Activity.pptx
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
CE344L-200365-Lab6.pdf
CE344L-200365-Lab6.pdfCE344L-200365-Lab6.pdf
CE344L-200365-Lab6.pdf
 
CE344L-200365-Lab4.pdf
CE344L-200365-Lab4.pdfCE344L-200365-Lab4.pdf
CE344L-200365-Lab4.pdf
 
CE344L-200365-Lab3.pdf
CE344L-200365-Lab3.pdfCE344L-200365-Lab3.pdf
CE344L-200365-Lab3.pdf
 
CE344L-200365-Lab8.pdf
CE344L-200365-Lab8.pdfCE344L-200365-Lab8.pdf
CE344L-200365-Lab8.pdf
 

Recently uploaded

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 

Recently uploaded (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 

CE344L-200365-Lab5.pdf

  • 1. 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()