def ar_func(ind,ar_src, cv_cover, cv_cov_vid, opts): im = ar_src[44:311,:,:] cropped_im = cv2.resize(im,(im.shape[1],cv_cover.shape[0])) matches, locs1, locs2 = matchPics(cv_cover,cv_cov_vid,opts) locs1 = locs1[matches[:,0],0:2] locs2 = locs2[matches[:,1],0:2] bestH2to1, inliers = computeH_ransac(locs1, locs2, opts) resize_im = cv2.resize(im,(im.shape[1],cv_cover.shape[0])) cropped_im = resize_im[:, int(cropped_im.shape[1]/2)-(int(cv_cover.shape[1]/2)) : int(cropped_im.shape[1]/2)+(int(cv_cover.shape[1]/2)),:] composite_img = compositeH(bestH2to1, cropped_im, cv_cov_vid) return composite_img
def ablation(opts, cv_cover, cv_desk): sigmas = np.arange(0.1, 1.0, 0.1) ratios = np.arange(0.1, 1.0, 0.1) for s in sigmas: for r in ratios: opts.ratio = r opts.sigma = s print("Using ratio {:.2f}, sigma {:.2f}".format(r, s)) cvc = cv_cover.copy() cvd = cv_desk.copy() matches, locs1, locs2 = matchPics(cvc, cvd, opts) plotMatches(cv_cover, cv_desk, matches, locs1, locs2, opts)
def stitch(img1, img2, black_removal=False): # Images dimensions h1, w1, _ = img1.shape h2, w2, _ = img2.shape # Match matches, locs1, locs2 = matchPics(img1, img2, opts) # plotMatches(img1, img2, matches, locs1, locs2, opts) locs1 = locs1[matches[:, 0]] locs2 = locs2[matches[:, 1]] locs1[:, [0, 1]] = locs1[:, [1, 0]] locs2[:, [0, 1]] = locs2[:, [1, 0]] # Homography H, inliers = computeH_ransac(locs1, locs2, opts) # Determine final size of panorama corners = np.array([[0, 0, 1], [w2, 0, 1], [0, h2, 1], [w2, h2, 1]], dtype=np.float) corners = np.matmul(H, corners.T) corners = np.divide(corners, corners[-1, :]).T # print(corners) # Warp if black_removal: # Define the width min_w = int(min(corners[1, 0], corners[3, 0])) width_right = max(min_w, w1) width = width_right # Define the height max_h = int(np.max(corners[:2, 1])) height_top = max(0, max_h) min_h = int(np.min(corners[2:, 1])) height_low = min(min_h, h1) height = height_low - height_top pano = cv2.warpPerspective(img2, H, (width, height)) pano[:height, :w1, :] = img1[:height, :] pano = pano[height_top:, :] else: width = w1 + int(0.5 * w2) height = h1 + int(0.5 * h2) pano = cv2.warpPerspective(img2, H, (width, height)) pano[:h1, :w1, :] = img1 return pano
def func_result(cv_cover, frame, ar_f, opts): matches, locs1, locs2 = matchPics(cv_cover, frame, opts) x1 = locs1[matches[:, 0], 0:2] x2 = locs2[matches[:, 1], 0:2] H2to1, inliers = computeH_ransac(x1, x2, opts) ar_f = ar_f[45:310, :, :] cover_width = cv_cover.shape[1] width = int(ar_f.shape[1] / ar_f.shape[0]) * cv_cover.shape[0] resized_ar = cv2.resize(ar_f, (width, cv_cover.shape[0]), interpolation=cv2.INTER_AREA) h, w, d = resized_ar.shape cropped_ar = resized_ar[:, int(w / 2) - int(cover_width / 2):int(w / 2) + int(cover_width / 2), :] result = compositeH(H2to1, cropped_ar, frame) return result
def createVideoFiles(args): opts, numframes, ar_source, book, cv_cover, bookdim = args for i in range(numframes): bookframe = book[i, :, :, :] pandaframe = ar_source[i, :, :] panda_crop = pandaframe[40:320, 208:431, :] panda = cv2.resize(panda_crop, bookdim, interpolation=cv2.INTER_AREA) matches, locs1, locs2 = matchPics(cv_cover, bookframe, opts) # plotMatches(cv_cover, bookframe, matches, locs1, locs2) locs1 = locs1[matches[:, 0]] locs2 = locs2[matches[:, 1]] locs1[:, [1, 0]] = locs1[:, [0, 1]] locs2[:, [1, 0]] = locs2[:, [0, 1]] H2to1, inliers = computeH_ransac(locs1, locs2, opts) image = compositeH(H2to1, panda, bookframe) np.save("../test1/img{}.npy".format(i), image)
def createvideo(panda, book_frames, edge_crop, x_start, x_end, cv_cover): for idx in range(frame_no): panda_img = panda[idx] # crop the panda frame panda_img = panda_img[edge_crop:-edge_crop, x_start:x_end] panda_img = cv2.resize(panda_img, (cv_cover.shape[1], cv_cover.shape[0])) #book_frame book_img = book_frames[idx] matches, locs1, locs2 = matchPics(book_img, cv_cover, opts) pair1 = locs1[matches[:, 0]] pair2 = locs2[matches[:, 1]] homography = computeH_ransac(pair1, pair2, opts) # panda_warped = cv2.warpPerspective(panda_img,homography,(book_img.shape[1],book_img.shape[0])) # mask = compositeH(homography, book_img, panda_warped) merge_frame = compositeH(homography, book_img, panda_img) #merge frame # merge_frame = (1- mask)*(book_img) + mask * panda_warped # print(merge_frame.shape) ar_avi.write(merge_frame)
def rotTest(): opts = get_opts() ratio = opts.ratio #'ratio for BRIEF feature descriptor' sigma = opts.sigma #'threshold for corner detection using FAST feature detector' #Q2.1.6 #Read the image and convert to grayscale cv_cover = cv2.imread('../data/cv_cover.jpg') img = cv_cover locs = [] # N x 2 matrix containing x,y coords or matched point pairs hist = [] num_matches = [] bin_list = [] for i in range(36): print(i) #Rotate Image rotImg = ndimage.rotate(img, i * 10, reshape=True) #Compute features, descriptors and Match features img_matches, locs1, locs2 = matchPics(rotImg, img, opts) #plotMatches(rotImg, img, img_matches, locs1, locs2) # display matches between both pictures num_matches.append(len(img_matches)) print(len(img_matches)) #plt.hist(num_matches, bins=36, range=None, density=False) ## put shape of matches in histogram plt.bar(i * 10, height=num_matches[i]) ## put shape of matches in histogram plt.title('Histogram of matches') plt.ylabel('Number of matches') plt.xlabel('Rotation') plt.show() return
# Just for testing # n_frames = 24 print("Writing AR video...") for i in range(n_frames): print(f"Frame {i}/{n_frames}") ar_frame = ar_frames[i] ar_frame = ar_frame[50:-50, int((w2-w1)/2):int((w2+w1)/2)] ar_frame = cv2.resize(ar_frame, dsize=(w1, h1)) # print(ar_cover.shape, h1) # cv2.imshow('ar_c',ar_cover) # Match features matches, locs1, locs2 = matchPics(cv_cover, bk_frames[i], opts) # plotMatches(cv_cover, hp_cover, matches, locs1, locs2, opts) locs1 = locs1[matches[:, 0]] locs1[:, [0, 1]] = locs1[:, [1, 0]] locs2 = locs2[matches[:, 1]] locs2[:, [0, 1]] = locs2[:, [1, 0]] # Homography and warping H, inliers = computeH_ransac(locs1, locs2, opts) composite_img = compositeH(H, ar_frame, bk_frames[i]) # cv2.imshow('warped', composite_img) video_out.write(composite_img) video_out.release() print("Done...")
3. Uses the computed homography to warp hp cover.jpg to the dimensions of the cv desk.png image using the OpenCV function cv2.warpPerspective function. 4. At this point you should notice that although the image is being warped to the correct location, it is not filling up the same space as the book. Why do you think this is happening? How would you modify hp cover.jpg to fix this issue? 5. Implement the function: composite img = compositeH( H2to1, template, img ) to now compose this warped image with the desk image as in in Figure 4 6. Include your result in your write-up. ''' cv_cover = cv2.imread('../data/cv_cover.jpg') cv_desk = cv2.imread('../data/cv_desk.png') #print(cv_desk.shape) hp_cover=cv2.imread('../data/hp_cover.jpg') #print(hp_cover.shape) matches1,locs1,locs2=matchPics(cv_desk,cv_cover, opts)#Match between first two images locs1[:,[0,1]] = locs1[:,[1,0]] locs2[:,[0,1]] = locs2[:,[1,0]] plotMatches(cv_desk,cv_cover, matches1,locs1,locs2) print('locs1m1 Shape :',locs1.shape) print('locs2m1 Shape :',locs2.shape) print('matches :',matches1.shape) bestH2to1,inliers=computeH_ransac(locs1[matches1[:,0]],locs2[matches1[:,1]],opts) #print('Best :',bestH2to1) dim=(cv_cover.shape[1],cv_cover.shape[0])
from opts import get_opts from helper import plotMatches import matplotlib.pyplot as plt opts = get_opts() #Q2.1.6 #Read the image and convert to grayscale, if necessary cv_cover = cv2.imread('../data/cv_cover.jpg') matchesnumber = np.zeros((36)) for i in range(36): #Rotate Image cv_cover_rot = scipy.ndimage.rotate(cv_cover, 10 * i) #Compute features, descriptors and Match features matches, locs1, locs2 = matchPics(cv_cover, cv_cover_rot, opts) #Update histogram matchesnumber[i] = np.size(matches, axis=0) if (i == 3): plotMatches(cv_cover, cv_cover_rot, matches, locs1, locs2) if (i == 15): plotMatches(cv_cover, cv_cover_rot, matches, locs1, locs2) if (i == 30): plotMatches(cv_cover, cv_cover_rot, matches, locs1, locs2) #Display histogram # plt.hist(matchesnumber, bins = 36) # plt.show() # x = np.arange(36)
import numpy as np import cv2 from opts import get_opts #Import necessary functions from matchPics import matchPics from planarH import computeH_ransac, compositeH #Write script for Q2.2.4 opts = get_opts() cv_cover = cv2.imread('../data/cv_cover.jpg') cv_desk = cv2.imread('../data/cv_desk.png') hp_cover = cv2.imread('../data/hp_cover.jpg') matches, locs1, locs2 = matchPics(np.transpose(cv_cover, (1, 0, 2)), np.transpose(cv_desk, (1, 0, 2)), opts) bestH2to1, inliers = computeH_ransac(matches, locs1, locs2, opts) composite_img = compositeH(bestH2to1, hp_cover, cv_desk, cv_cover)
im = cv2.imread("../data/cv_cover.jpg") # cv2.imshow('image', im) # cv2.waitKey(0) match_count = np.zeros((36, 2)) for i in range(36): # Rotate Image rot = (i + 1) * 10 im_rot = scipy.ndimage.rotate(im, rot) # cv2.imshow(f'rot {i}', im_rot) # cv2.waitKey(0) # cv2.destroyWindow(f'rot {i}') # Compute features, descriptors and Match features matches, locs1, locs2 = matchPics(im, im_rot, opts) # Plot them opts.name = f"rot_{rot}_m_{len(matches)}" # plotMatches(im, im_rot, matches, locs1, locs2, opts) # Update histogram print(f"Matches {len(matches)} Angle {rot}") match_count[i] = [rot, len(matches)] print(match_count) # Display histogram plt.bar(match_count[:, 0], match_count[:, 1], width=8) plt.xlabel('Orientation') plt.ylabel('Matches')
import numpy as np import cv2 import skimage.io import skimage.color #Import necessary functions from matchPics import matchPics from helper import plotMatches from planarH import * #Write script for Q2.2.4 cv_cover = cv2.imread('../data/cv_cover.jpg') cv_desk = cv2.imread('../data/cv_desk.png') hp_cover = cv2.imread('../data/hp_cover.jpg') H, W, _ = cv_cover.shape hp_cover_resized = cv2.resize(hp_cover, dsize=(W, H)) matches, locs1, locs2 = matchPics(cv_desk, hp_cover) H2to1, inliers = computeH_ransac(locs1[matches[:, 0]], locs2[matches[:, 1]]) warped_img = cv2.warpPerspective(hp_cover_resized, H2to1, dsize=(cv_desk.shape[1], cv_desk.shape[0])) cv2.imwrite('./result/HarryPotter_made.jpg', warped_img) cv2.imshow('HarryPotter_made', warped_img) cv2.waitKey(0) composite_img = compositeH(H2to1, hp_cover_resized, cv_desk) cv2.imwrite('./result/HarryPotter_Desk.jpg', composite_img) cv2.imshow('HarryPotter_Desk', composite_img) cv2.waitKey(0)
import cv2 from matchPics import matchPics from helper import plotMatches from opts import get_opts opts = get_opts() cv_cover = cv2.imread('../data/cv_cover.jpg') cv_desk = cv2.imread('../data/cv_desk.png') matches, locs1, locs2 = matchPics(cv_cover, cv_desk, opts) #display matched features plotMatches(cv_cover, cv_desk, matches, locs1, locs2)
ar=np.concatenate((ar,pad),axis=0) #print("Shape of ar after padding:",ar.shape) #Write script for Q3.1 opts = get_opts() locs1_arr=[] locs2_arr=[] matches_arr=[] bestH2to1_arr=[] cv_cover = cv2.imread('../data/cv_cover.jpg') composite_list=[] for i in range(book.shape[0]): print('i :{} and book :{}'.format(i,book[i,:,:,:].shape)) matches,locs1,locs2=matchPics(book[i,:,:,:],cv_cover,opts) locs1[:,[0,1]] = locs1[:,[1,0]] locs2[:,[0,1]] = locs2[:,[1,0]] #plotMatches(book[i,:,:,:],cv_cover,matches,locs1,locs2) bestH2to1,inliers=computeH_ransac(locs1[matches[:,0]],locs2[matches[:,1]],opts) dim=(cv_cover.shape[1],cv_cover.shape[0]) aspect_ratio=cv_cover.shape[1]/cv_cover.shape[0]#w/h #print('Aspect ratio :',aspect_ratio) video_cover=ar[i,:,:,:] video_cover=video_cover[44:-44,:]#We chop off the black portions H,W,C=video_cover.shape #print('Shape of ar:',video_cover_cropped.shape) h=H/2
# #Compute features, descriptors and Match features # matches , locs1, locs2 = matchPics(img_ori,img_rotate , opts) # # #Update histogram # match_hist[i] = matches.shape[0] # # x = np.linspace(0,10*(iteration - 1), iteration) # plt.bar(x,match_hist) # plt.show() ###################### # For visualizing rotate images #rotate image at 20 degree img_rotate = ndimage.rotate(img_ori, 20) matches, locs1, locs2 = matchPics(img_ori, img_rotate, opts) fig, ax = plt.subplots(nrows=1, ncols=1) plt.axis('off') skimage.feature.plot_matches(ax, img_ori, img_rotate, locs1, locs2, matches, matches_color='r', only_matches=True) plt.show() #rotate image at 60 degree img_rotate = ndimage.rotate(img_ori, 60)
import opts #Q2.1.6 #Read the image and convert to grayscale, if necessary #%% img = cv2.imread( 'D:/Academic/CMU/Course/2020Fall/CV/Homework/HW2_Handout/HW2_Handout/data/cv_cover.jpg' ) #%% x = [] y = [] opts1 = opts.get_opts() for i in range(36): #Rotate Image img_rotate = sci.rotate(img, i * 10) #Compute features, descriptors and Match features matches, locs1, locs2 = matchPics(img, img_rotate, opts1) #Update histogram # degree = i * 10 x.append(i * 10) y.append(matches.shape[0]) # match_count = matches.shape[0] #Display histogram plt.bar(x, y, 5) # plt.bar(x[0],y[0],5) # plt.bar(x[1],y[1],5) # plt.bar(x[2],y[2],5)
#Read the image and convert to grayscale, if necessary from opts import get_opts opts = get_opts() cv_cover = cv2.imread('../data/cv_cover.jpg') matches_hist = list() for i in range(36): #Rotate Image rotated_img = rotate(cv_cover, 10 * (i)) #Compute features, descriptors and Match features matches, locs1, locs2 = matchPics(cv_cover, rotated_img, opts) #Update histogram matches_hist.append(len(matches)) if (i % 11 == 0): plotMatches(cv_cover, rotated_img, matches, locs1, locs2) print(matches_hist) #Display histogram m_hist = np.asarray(matches_hist) ang = np.asarray([i for i in range(0, 360, 10)]).T y = np.arange(len(ang)) plt.bar(y, m_hist.T, align='center') plt.xticks(y, ang) plt.ylabel('matches')
from opts import get_opts #Import necessary functions from matchPics import matchPics from planarH import computeH_ransac, compositeH from loadVid import loadVid #Write script for Q3.1 opts = get_opts() cv_cover = cv2.imread('../data/cv_cover.jpg') # 440, 350, 3 ar_source = loadVid('../data/ar_source.mov') np.save("ar_source.npy", ar_source) book = loadVid('../data/book.mov') np.save("book.npy", book) # ar_source = np.load('ar_source.npy', allow_pickle=True) # 511, 360, 640, 3 # book = np.load('book.npy', allow_pickle=True) # 641, 480, 640, 3 #Processing the video one frame at a time cap = cv2.VideoWriter('ar.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 15.0, (book.shape[0], book.shape[1])) for frame_num in range(ar_source.shape[0]): print(frame_num) frame_source = ar_source[frame_num] frame_book = book[frame_num] matches, locs1, locs2 = matchPics(np.transpose(cv_cover, (1, 0, 2)), np.transpose(frame_book, (1, 0, 2)), opts) bestH2to1, inliers = computeH_ransac(matches, locs1, locs2, opts) frame_source = frame_source[48:-48, 145:495] # crop black part from top and bottom of ar_cource video composite_img = compositeH(bestH2to1, frame_source, frame_book, cv_cover) # cv2.imwrite('ar_final/frame_{}.png'.format(frame_num), composite_img) cap.write(composite_img) cap.release()
from matchPics import matchPics from opts import get_opts from matplotlib import pyplot as plt # Q2.1.6 # Read the image and convert to grayscale, if necessary img = cv2.imread('../data/cv_cover.jpg') opts = get_opts() x = [] for i in range(36): # Rotate Image rotated_img = scipy.ndimage.rotate(img, i * 10) # Compute features, descriptors and Match features matches, locs1, locs2 = matchPics(img, rotated_img, opts) # Update histogram x.append(len(matches)) # Display histogram angles = [i for i in range(0, 360, 10)] angles = np.asarray(angles) plt.bar(angles, x, 5, color='red') plt.xlabel('Rotation Angle') plt.ylabel('Number of Matches') plt.title('Number of Matches vs Rotation Angle') plt.savefig("../results/histogram.png") plt.show() # plt.hist(x, bins=angles)
[h2, w2]]), axis=1) # perspectiveTransform works for set of points unlike warpPerspective which works on entire image img2_corners = cv2.perspectiveTransform(img2_corners, H) composite_corners = np.r_[img1_corners, img2_corners] [x_min, y_min] = np.int32(composite_corners.min(axis=0).flatten() - 0.5) [x_max, y_max] = np.int32(composite_corners.max(axis=0).flatten() + 0.5) rotation_matrix = np.array([[1, 0, -x_min], [0, 1, -y_min], [0, 0, 1]]) # Warp second image to the complete scene composite_img = cv2.warpPerspective(img2, rotation_matrix.dot(H), (x_max - x_min, y_max - y_min)) composite_img[-y_min:w1 - y_min, -x_min:h1 - x_min] = img1 return composite_img # Read the images using cv2 img1 = cv2.imread('../ec/img_left.JPG') img2 = cv2.imread('../ec/img_right.JPG') # get opts for matching points and ransac opts = get_opts() matches, locs1, locs2 = matchPics(np.transpose(img1, (1, 0, 2)), np.transpose(img2, (1, 0, 2)), opts) bestH2to1, inliers = computeH_ransac(matches, locs1, locs2, opts) img = get_panaroma(img1, img2, bestH2to1) cv2.imwrite('panaroma.png', img)
import matplotlib.pyplot as plt #Q2.1.5 #Read the image and convert to grayscale, if necessary I1 = cv2.imread('../data/cv_cover.jpg') # img = cv2.cvtColor(I1, cv2.COLOR_BGR2GRAY) # hist = [] rot = [10, 20, 30] hist_sum = [] for i in range(36): print('%d th rot processing.....' % i) #Rotate Image rot_img = rotate(I1, i * 10, reshape=True) #Compute features, descriptors and Match features matches, locs, rot_locs = matchPics(I1, rot_img) # if i in rot: # plotMatches(I1, rot_img, matches, locs, rot_locs, '%d rot match.jpeg'%i) # Update histogram hist_, bin_ = np.histogram(matches[:, 0]) hist_sum.append(sum(hist_)) # plt.figure() # if n ==0: # hist = hist_ # bin = bin_ # else: # hist = np.vstack((hist, hist_)) # bin = np.vstack((bin, bin_)) # n += 1 #Display histogram
from matchPics import matchPics from helper import plotMatches from matplotlib import pyplot as plt opts = get_opts() #Write script for Q4.2x pano_left = cv2.imread('../IMG-7543.jpg') pano_right = cv2.imread('../IMG-7544.jpg') H1 = np.size(pano_left, axis = 0) W1 = np.size(pano_left, axis = 1) H2 = np.size(pano_right, axis = 0) W2 = np.size(pano_right, axis = 1) matches, locs1, locs2 = matchPics(pano_left, pano_right, opts) plotMatches(pano_left, pano_right, matches, locs1, locs2) locs1 = locs1[matches[:,0]] locs2 = locs2[matches[:,1]] locs1[:,[1,0]] = locs1[:,[0,1]] locs2[:,[1,0]] = locs2[:,[0,1]] H2to1, inliers = computeH_ransac(locs1, locs2, opts) corners_right = np.array([[0,0,W2-1,W2-1],[0,H2-1,H2-1,0],[1,1,1,1]]) corners_left_pred = H2to1 @ corners_right corners_left_pred = (corners_left_pred/corners_left_pred[-1,:]).round().astype(int)
from helper import plotMatches from opts import get_opts # Q2.1.6 opts = get_opts() # Read the image and convert to grayscale, if necessary cover = cv2.imread('../data/cv_cover.jpg') histogram = [] choice = [1, 17, 35] for i in range(36): # Rotate Image cover_rotate = scipy.ndimage.rotate(cover, i * 10, reshape=False) # Compute features, descriptors and Match features matches, locs1, locs2 = matchPics(cover, cover_rotate, opts) if i in choice: plotMatches(cover, cover_rotate, matches, locs1, locs2) # Update histogram histogram.append(matches.shape[0]) # Display histogram chosen_histogram = [histogram[1]] + [histogram[17]] + [histogram[35]] plt.bar(np.arange(3), chosen_histogram) plt.xlabel('Rotation Degrees') plt.ylabel('Matches Count') plt.xticks(np.arange(3), choice * 10) plt.title('Number of Matches for BRIEF Rotation') plt.show()
import numpy as np import cv2 import skimage.io import skimage.color from opts import get_opts #Import necessary functions from matchPics import matchPics from planarH import computeH_ransac from helper import plotMatches #Write script for Q2.2.4 opts = get_opts() # Read cv_cover.jpg, cv_desk.png and hp_cover.jpg img1 = cv2.imread('D:/Academic/CMU/Course/2020Fall/CV/Homework/HW2_Handout/HW2_Handout/data\cv_cover.jpg',1) img2 = cv2.imread('D:/Academic/CMU/Course/2020Fall/CV/Homework/HW2_Handout/HW2_Handout/data\cv_desk.png',1) img3 = cv2.imread('D:/Academic/CMU/Course/2020Fall/CV/Homework/HW2_Handout/HW2_Handout/data\hp_cover.jpg',1) matches, locs1, locs2 = matchPics(img1,img1, opts) locs1 = locs1[matches[:,0],:] locs2 = locs2[matches[:,1],:] plotMatches(img1, img2, matches, locs1, locs2) result = computeH_ransac(locs2, locs1, opts)
#Write script for Q3.1 opts = get_opts() #load vids and template image panda_source = loadVid("../data/ar_source.mov") book_source = loadVid("../data/book.mov") cv_cover = cv2.imread('../data/cv_cover.jpg') for frame in range(book_source.shape[0]): # assigned each video frame to variable: book_frame print(frame) #print frame number to track processing book_frame = book_source[frame] #compute matches matches, locs1, locs2 = matchPics(book_frame, cv_cover, opts) #compute ransac matched_locs1 = np.array([locs1[i] for i in matches[:0]]) matched_locs2 = np.array([locs2[i] for i in matches[:1]]) bestH2to1, inliers = computeH_ransac( matched_locs1, matched_locs2, opts) # buggy computeH.ranac function, unable to produce output #bestH2to1, inlirs = cv2.findHomography(matched_locs1, matched_locs2, cv2.RANSAC,5.0) - for debugging #crop and resize width_newPanda = int(cv.cover.shape[1] / cv_cover.shape[0] * panda_source.shape[1]) # w x h crop = int(panda_source.shape[2] - width_newPanda) // 2 ##crop = int(panda_source.shape[2]- width_newPanda)/2 for debugging
import numpy as np import cv2 import pdb import scipy import skimage.io import skimage.color from PIL import Image, ImageDraw, ImageFilter from matplotlib import pyplot as plt from opts import get_opts from matplotlib import pyplot as plt from matchPics import matchPics from statistics import stdev from skimage import img_as_float64 opts = get_opts() left = cv2.imread('../data/pano_left.jpg') #use PIL right = cv2.imread('../data/pano_right.jpg') matches, locs1, locs2 = matchPics(np.transpose(left, (1, 0, 2)), np.transpose(right, (1, 0, 2)), opts) bestH2to1, inliers = computeH_ransac(matches, locs1, locs2, opts) warped_im = cv2.warpPerspective(right, (bestH2to1), (np.max(im1.shape[1]), left.shape[0]))
import matplotlib.pyplot as plt from imutils import paths import argparse import imutils #Write script for Q2.2.4 opts = get_opts() #read in images img_left = cv2.imread('../data/pano_left.jpg') img_right = cv2.imread('../data/pano_right.jpg') print(img_left.shape) print(img_right.shape) img_left = cv2.cvtColor(img_left, cv2.COLOR_BGR2RGB) img_right = cv2.cvtColor(img_right, cv2.COLOR_BGR2RGB) matches, locs1, locs2 = matchPics(img_left, img_right, opts) pair1 = locs1[matches[:, 0]] pair2 = locs2[matches[:, 1]] homography = computeH_ransac(pair1, pair2, opts) right_warped = cv2.warpPerspective(img_right, homography, (img_left.shape[1], img_left.shape[0])) images = [] images.append(img_left) images.append(img_right) stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) if status == 0: # write the output stitched image to disk
# Write script for Q3.1 opts = get_opts() ar_frames = loadVid('../data/ar_source.mov') book_frames = loadVid('../data/book.mov') cv_cover = cv2.imread('../data/cv_cover.jpg') f, H, W = ar_frames.shape[:3] ar_frames = ar_frames[:, 44:-44, 200:430, :] if not os.path.exists('../result'): os.mkdir('../result') fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') writer = cv2.VideoWriter('../result/ar.avi', fourcc, 25, (book_frames.shape[2], book_frames.shape[1]), True) for i in range(f): book = book_frames[i] ar = ar_frames[i] matches, locs1, locs2 = matchPics(book, cv_cover, opts) if matches.shape[0] >= 4: x1 = locs1[matches[:, 0], :] x2 = locs2[matches[:, 1], :] x1[:, [0, 1]] = x1[:, [1, 0]] x2[:, [0, 1]] = x2[:, [1, 0]] bestH2to1, inliers = computeH_ransac(x1, x2, opts) ar = cv2.resize(ar, (345, 444)) composite_img = compositeH(bestH2to1, ar, book) writer.write(composite_img)