Hw2_functions.py:
1. getImagePts(im1, im2, varName1, varName2, nPoints)
Input: im1, im2 - grayscale images in the range [0..255] . Not necessarily same size. varName1, varName2 – strings that represent the names of variables to be saved as (for example if I want to save the pointset as imagePts1.npy and imagePts2.npy, I will pass the names varName1 = “imagePts1” and varName2 = “imagePts2”. nPoints – number of points the user chooses.
Output: None.
Method: Allows user to select corresponding pairs of points, one set in im1 and one in im2 (in same order). Function opens a figure of im1 and let’s the user select nPoints on the image. Then does the same for im2. Coordinates (x,y) of the points in each image are collected in imagePts1, imagePts2 – np arrays Nx2. After collecting all coordinates, round them using np.round and add a third dimension of ones which will make them Nx3 arrays, then save the array using: np.save(“imagePts1.npy”, imagePts1) np.save(“imagePts2.npy”, imagePts2) This will save the arrays as files in the project folder. later, you can load them instead of choosing points again, by using:
imagePts1 = np.load(“imagePts1.npy”) imagePts2 = np.load(“imagePts2.npy”)
Use plt.ginput to interactively choose points Documentation: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.ginput.html
Pay very close attention to the x,y order. In Image assignment image[x,y] – x is the rows, but in x,y = ginput – x is the horizontal axis.
2. findAffineTransform (pointsSet1,pointsSet2)
Input: pointsSet1, pointsSet2 - arrays Nx3 of coordinates representing corresponding points between the 2 sets. Output: T – a 3x3 np matrix representing an affine transformation. Method: Calculates the parameters of the affine transform that best maps points in pointsSet1 to corresponding points in pointsSet2 in the least mean square sense.
Note: use functions np.matmul, np.linalg.pinv, np.reshape. (see slides 43-49 in lecture Lesson 5: Geometric Operations )
Note: you can loop over the N points.
3. findProjectiveTransform (pointsSet1,pointsSet2)
Input: pointsSet1, pointsSet2 - arrays Nx3 of coordinates. representing corresponding points between the 2 sets.
Output: T – a 3x3 np matrix representing a projective transformation.
Method: Calculates the parameters of the projective transform that best maps points in pointsSet1 to corresponding points in pointsSet2 in the least mean square sense.
(see slides 50-56 in lecture Lesson 5: Geometric Operations )
Note: you can loop over the N points.
4. mapImage (im,T,sizeOutIm)
Input: im - a grayscale image array in the range [0..255]. T - a 3x3 matrix representing a transformation. sizeOutIm – a tuple (numRows, numCols) representing the size of the output image.
Output: newIm - a grayscale image in the range [0..255] of size sizeOutIm containing the transformed image.
Method: Create newIm as an empty array of size sizeOutIm. For every coordinate of newIm, apply inverse mapping to determine source coordinates. Map value of image im at the source coordinates to the new image. Use bilinear Interpolation (tip: implement nearest neighbor interpolation first as it is easier to code, once everything is running – replace it with bilinear interpolation). Ensure that source coordinates do not fall outside im range. (points whose source are outside source image are assigned gray value zero).
Note: use functions np.linalg.inv , np.meshgrid, np.vstack, np.matmul, np.round, np.any, np.delete.
Note: Do not iterate over pixels of im. You don’t need a for loop to implement this function.
5. createMorphSequence (im1,imagePts1, im2,imagePts2, t_vec, transformType)
Input: im1, im2 - grayscale image arrays in the range [0..255].
imagePts1, imagePts2 - np arrays Nx3 of chosen coordinates of im1, im2. t_vec – a vector with t values where t is in [0..1]. You can use np.linspace(0,1,M) (outside the function) to create a vector with M equal steps between 0 and 1. transformType – a scalar. 0 = affine, 1 = projective.
Output: images – a list of images. The same size as t_vec.
Method: Input images must be of same size. Calculate the transforms T12 and T21 that map im1 to im2 and im2 to im1 respectively. For every t do the following: Calculate T12_t, the transformation from im1 to im2, t parts of the way.
As we did in the tutorial:
Calculate T21_1_t, the transformation from im2 to im1, (1-t) parts of the way:
Map im1 using T12_t, producing newIm1 Map im2 using T21_1_t, producing newIm2 Crossdisolve newIm1 and newIm2 with weights associated with t.
Note: use function np.eye(3) to create I matrix of size 3x3.
use functions inside createMorphSequence:
findProjectiveTransform/findAffineTransform to acquire transform.
mapImage to map to newIm1, newIm2.
Use below template to complete this task:
import numpy as np
import cv2
import matplotlib.pyplot as plt
def writeMorphingVideo(image_list, video_name):
out = cv2.VideoWriter(video_name+".mp4", cv2.VideoWriter_fourcc(*'MP4V'), 20.0, image_list[0].shape, 0)
for im in image_list:
out.write(im)
out.release()
def createMorphSequence (im1, im1_pts, im2, im2_pts, t_list, transformType):
if transformType:
# TODO: projective transforms
else:
# TODO: affine transforms
ims = []
for t in t_list:
# TODO: calculate nim for each t
ims.append(nim)
return ims
def mapImage(im, T, sizeOutIm):
im_new = np.zeros(sizeOutIm)
# create meshgrid of all coordinates in new image [x,y]
# add homogenous coord [x,y,1]
# calculate source coordinates that correspond to [x,y,1] in new image
# find coordinates outside range and delete (in source and target)
# interpolate - bilinear
# apply corresponding coordinates
# new_im [ target coordinates ] = old_im [ source coordinates ]
def findProjectiveTransform(pointsSet1, pointsSet2):
N = pointsSet1.shape[0]
# iterate iver points to create x , x'
for i in range(0, N):
# calculate T - be careful of order when reshaping it
return T
def findAffineTransform(pointsSet1, pointsSet2):
N = pointsSet1.shape[0]
# iterate iver points to create x , x'
for i in range(0, N):
# calculate T - be careful of order when reshaping it
return T
return T
def getImagePts(im1, im2,varName1,varName2, nPoints):
imagePts1 =
imagePts2 =
np.save(varName1+".npy", imagePts1)
np.save(varName2+".npy", imagePts2)
We are providing all advance level image processing assignment help services. If you need any help in Image processing related task or need help in above code to complete then text msg at here:
realcode4you@gmail.com
Comments