You are on page 1of 11

Submitted by: Sehrish Rafique(210097)

Group Participant: Asifa Mehmood (210093),


Submitted To: Dr. Ehatisham
Date: October 25th, 2021.
Computer Vision: Assignment # 01

Task # 01: Gaussian and Laplacian Pyramids


Gaussian Pyramid
In this task, we did a multiscale representation of image(flower.jpg). The original image was of size
200x200. which has been then resized into 5 sub-sizes; as exactly half of previous one upto level 5.

There are several different methods of creating image pyramids. We used “OpenCV”. Specifically,
“cv2.pyrDown” function for Gaussian image pyramids sub-sizing and smoothing. The code, input image
and output image in different scales can be seen in the below screenshot.

Implementation (Code):
import cv2
from matplotlib import pyplot as plt
img=cv2.imread('flower.jpg')
img=cv2.resize(img,(200,200))
cv2.imshow("Original",img)
gausian = img.copy()
for i in range (5):
gausian = cv2.pyrDown(gausian)
cv2.imshow(str(i),gausian)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Laplacian Pyramid:
In the below code, we have applied Laplacian pyramid method on the same image. The Laplacian
pyramid is somewhat similar to Gaussian; however, this saves the image of blurred versions
between each levels. In the below code, we have created a Gaussian pyramid in the first block of
code, then implemented Laplacian pyramid on the last level of Gaussian.
The last layer of Gaussian was top layer of Laplacian. Then 5 layers of Laplacian are created CV2
is used for resizing. The pyramids are represented as list of arrays. The code, input image and
output image in different sizes can be seen in the below screenshot.
Implementation (Code):
img = cv2.imread('flower.jpg')
lower = img.copy()

# Create a Gaussian Pyramid


gaussian_pyr = [lower]
for i in range(5):
lower = cv2.pyrDown(lower)
gaussian_pyr.append(lower)

# Last level of Gaussian remains same in Laplacian


laplacian_top = gaussian_pyr[-1]

# Create a Lapla cian Pyramid


laplacian_pyr = [laplacian_top]
for i in range(5,0,-1):
size = (gaussian_pyr[i - 1].shape[1], gaussian_pyr[i - 1].shape[0])
gaussian_expanded = cv2.pyrUp(gaussian_pyr[i], dstsize=size)
laplacian = cv2.subtract(gaussian_pyr[i-1], gaussian_expanded)
laplacian_pyr.append(laplacian)
cv2.imshow('lap-{}'.format(i-1),laplacian)
cv2.waitKey(0)

Output:
Task # 02: Image Blending
We have used image blending in the below code. We have used CV2 module and NumPy arrays in this
task. We have taken 2 images to blend. Using CV2 functions, we read both the images and then resized
them to make of the same size(instead of resizing, images of same size can also be taken).

We have generated Gaussian and Laplacian pyramids for scaling images before blending (as defined in the
above tasks). After generating pyramids, combined right and left halves of images in the form of rows and
columns.

In the second code block, we have blended images using image mask. For this method, first converted the
images into gray scale and set binary thresholds. After that blended the images with the specified mask.

Implementation (Code):
import cv2
import numpy as np
img1 = cv2.imread('Obama1.jpg')
img2 = cv2.imread('Clinton.jpg')
img1=cv2.resize(img1,(512,512))
img2=cv2.resize(img2,(512,512))
print(img1.shape)
print(img2.shape)
img1_img2 = np.hstack((img1[:, :256], img2[:, 256:]))

# generate Gaussian pyramid for apple


img1_copy = img1.copy()
gp_img1 = [img1_copy]
for i in range(6):
img1_copy = cv2.pyrDown(img1_copy)
gp_img1.append(img1_copy)

# generate Gaussian pyramid for orange


img2_copy = img2.copy()
gp_img2 = [img2_copy]
for i in range(6):
img2_copy = cv2.pyrDown(img2_copy)
gp_img2.append(img2_copy)

# generate Laplacian Pyramid for apple


img1_copy = gp_img1[5]
lp_img1 = [img1_copy]
for i in range(5, 0, -1):
gaussian_expanded = cv2.pyrUp(gp_img1[i])
laplacian = cv2.subtract(gp_img1[i-1], gaussian_expanded)
lp_img1.append(laplacian)

# generate Laplacian Pyramid for orange


img2_copy = gp_img2[5]
lp_img2 = [img2_copy]
for i in range(5, 0, -1):
gaussian_expanded = cv2.pyrUp(gp_img2[i])
laplacian = cv2.subtract(gp_img2[i-1], gaussian_expanded)
lp_img2.append(laplacian)

# Now add left and right halves of images in each level


img1_img2_pyramid = []
n=0
for img1_lap, img2_lap in zip(lp_img1, lp_img2):
n += 1
cols, rows, ch = img1_lap.shape
laplacian = np.hstack((img1_lap[:, 0:int(cols/2)], img2_lap[:, int(cols/2):]))
img1_img2_pyramid.append(laplacian)
# now reconstruct
img1_img2_reconstruct = img1_img2_pyramid[0]
for i in range(1, 6):
img1_img2_reconstruct = cv2.pyrUp(img1_img2_reconstruct)
img1_img2_reconstruct = cv2.add(img1_img2_pyramid[i], img1_img2_reconstruct)

cv2.imshow("Obama", img1)
cv2.imshow("Clinton", img2)
cv2.imshow("Blend", img1_img2)
cv2.imshow("Blending reconstruct", img1_img2_reconstruct)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Blended:

Blending Using Image mask:


import cv2
import numpy as np

img1 = cv2.imread("river.jpg")
img2 = cv2.imread("birds.jpg")
img1=cv2.resize(img1,(300,300))
img2=cv2.resize(img2,(300,300))
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

ret, mask = cv2.threshold(img2_gray, 240, 255, cv2.THRESH_BINARY)


mask_inv = cv2.bitwise_not(mask)

river = cv2.bitwise_and(img1, img1, mask=mask)


birds= cv2.bitwise_and(img2, img2, mask=mask_inv)
result = cv2.add(river, birds)

cv2.imshow("image1", img1)
cv2.imshow("image2", img2)
cv2.imshow("River Backgroung", river)
cv2.imshow("birds no background",birds)
cv2.imshow("mask", mask)
cv2.imshow("mask inverse", mask_inv)
cv2.imshow("result(blended)", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

Mask
Blended:

Image Blending Using addWeight function:


The following image is blended by using a simple add weight function. This function simply
combines two images by a certain weight value assigned to each image.

import cv2 as cv
#read the images
img1 = cv.imread('img1.jpeg')
img2 = cv.imread('img2.jpeg')

#Images have to be of the same size to be added


#so resize images
img1=cv.resize(img1, (300,300))
img2=cv.resize(img2, (300,300))

#add(or blend) the images


result = cv.addWeighted(img1, 0.5, img2, 0.5, 0)
#create a resizable window for the image
cv.namedWindow('result',cv.WINDOW_NORMAL)
#show the image on the screen
cv.imshow("Image 1",img1)
cv.imshow("Image 2", img2)
cv.imshow('result',result)
if cv.waitKey()==0:
cv.destroyAllWindows()
Output:

Blended:

Task # 03: Template Matching


Simple Template Matching using Original Image size:
We have applied template matching on an object(circle). We have used 2 images; “template” and
“circle” where the goal was to detect circle in the template containing multiple shapes. CV2 and
NumPy are mainly used for this purpose in our code. Firstly we converted the images in gray scale,
defined thresholds and ran a loop to go through the images for detection using matchTemplate a pre-
defined function in OpenCV.

Implementation (Code):
import cv2
import numpy as np
from matplotlib import pyplot as plt
img_rgb = cv2.imread('template.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('circle.jpg',0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)

cv2.imwrite('res.png',img_rgb)
cv2.imshow("result", img_rgb)
cv2.waitKey(0)
Output:
Template:

Template Matching using Pyramids (Gausian level 1 to 4)


import cv2
import numpy as np
from matplotlib import pyplot as plt

img_rgb = cv2.imread('template.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('circle.jpg',0)
gausian = img_gray.copy()
gausian1=template.copy()
gausian2=img_rgb.copy()
#for i in range (4):

gausian = cv2.pyrDown(gausian)
gausian1=cv2.pyrDown(gausian1)
gausian2=cv2.pyrDown(gausian2)
w, h = gausian1.shape[::-1]

res = cv2.matchTemplate(gausian,gausian1,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(gausian2, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)

cv2.imwrite('res.png',gausian2)
cv2.imshow("template",gausian1)
cv2.imshow("result", gausian2)

cv2.waitKey(0)

Output: (level 1 to 4)

Level 1
Level 2

Level 3

Level 4

Template Matching using pyramids are fast as compared to finding the pattern in original image.
As the size reduces the time find all the patterns also reduced. At at level 4 the threshold value for
matching was increased to 0.9 as 0.8 was also detecting star as a matching template thus increasing
accuracy bcause of small size.

You might also like