Professional Documents
Culture Documents
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()
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:]))
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:
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)
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:
import cv2 as cv
#read the images
img1 = cv.imread('img1.jpeg')
img2 = cv.imread('img2.jpeg')
Blended:
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:
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.