# Please do not modify this file.
import numpy as np
import cv2
import pickle
def im2single(im):
im = im.astype(np.float32) / 255
return im
def single2im(im):
im *= 255
im = im.astype(np.uint8)
return im
def rgb2gray(rgb):
"""Convert RGB image to grayscale
Args:
- rgb: A numpy array of shape (m,n,c) representing an RGB image
Returns:
- gray: A numpy array of shape (m,n) representing the corresponding grayscale image
"""
return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])
def load_image(path):
"""
Args:
- path: string representing a filepath to an image
"""
return im2single(cv2.imread(path))[:, :, ::-1]
def save_image(path, im):
"""
Args:
- path:
- im: A numpy array of shape
"""
return cv2.imwrite(path, single2im(im.copy())[:, :, ::-1])
def cheat_interest_points(eval_file, scale_factor):
"""
Args:
- eval_file: string representing the file path to the list of known correspondences
- scale_factor: Python float representing the scale needed to map from the original
image coordinates to the resolution being used for the current experiment.
Returns:
- x1: A numpy array of shape (k,) containing ground truth x-coordinates of imgA correspondence pts
- y1: A numpy array of shape (k,) containing ground truth y-coordinates of imgA correspondence pts
- x2: A numpy array of shape (k,) containing ground truth x-coordinates of imgB correspondence pts
- y2: A numpy array of shape (k,) containing ground truth y-coordinates of imgB correspondence pts
"""
with open(eval_file, 'rb') as f:
d = pickle.load(f, encoding='latin1')
return d['x1'] * scale_factor, d['y1'] * scale_factor, d['x2'] * scale_factor,\
d['y2'] * scale_factor
def hstack_images(imgA, imgB):
"""
Stacks 2 images side-by-side and creates one combined image.
Args:
- imgA: A numpy array of shape (M,N,3) representing rgb image
- imgB: A numpy array of shape (D,E,3) representing rgb image
Returns:
- newImg: A numpy array of shape (max(M,D), N+E, 3)
"""
Height = max(imgA.shape[0], imgB.shape[0])
Width = imgA.shape[1] + imgB.shape[1]
newImg = np.zeros((Height, Width, 3), dtype=imgA.dtype)
newImg[:imgA.shape[0], :imgA.shape[1], :] = imgA
newImg[:imgB.shape[0], imgA.shape[1]:, :] = imgB
return newImg
def show_interest_points(img, X, Y):
"""
Visualized interest points on an image with random colors
Args:
- img: A numpy array of shape (M,N,C)
- X: A numpy array of shape (k,) containing x-locations of interest points
- Y: A numpy array of shape (k,) containing y-locations of interest points
Returns:
- newImg: A numpy array of shape (M,N,C) showing the original image with
colored circles at keypoints plotted on top of it
"""
newImg = img.copy()
for x, y in zip(X.astype(int), Y.astype(int)):
cur_color = np.random.rand(3)
newImg = cv2.circle(newImg, (x, y), 10, cur_color, -1, cv2.LINE_AA)
return newImg
def show_correspondence_circles(imgA, imgB, X1, Y1, X2, Y2):
"""
Visualizes corresponding points between two images by plotting circles at
each correspondence location. Corresponding points will have the same random color.
Args:
- imgA: A numpy array of shape (M,N,3)
- imgB: A numpy array of shape (D,E,3)
- x1: A numpy array of shape (k,) containing x-locations of keypoints in imgA
- y1: A numpy array of shape (k,) containing y-locations of keypoints in imgA
- x2: A numpy array of shape (j,) containing x-locations of keypoints in imgB
- y2: A numpy array of shape (j,) containing y-locations of keypoints in imgB
Returns:
- newImg: A numpy array of shape (max(M,D), N+E, 3)
"""
newImg = hstack_images(imgA, imgB)
shiftX = imgA.shape[1]
X1 = X1.astype(np.int)
Y1 = Y1.astype(np.int)
X2 = X2.astype(np.int)
Y2 = Y2.astype(np.int)
for x1, y1, x2, y2 in zip(X1, Y1, X2, Y2):
cur_color = np.random.rand(3)
green = (0, 1, 0)
newImg = cv2.circle(newImg, (x1, y1), 10, cur_color, -1, cv2.LINE_AA)
newImg = cv2.circle(newImg, (x1, y1), 10, green, 2, cv2.LINE_AA)
newImg = cv2.circle(newImg, (x2+shiftX, y2), 10, cur_color, -1, cv2.LINE_AA)
newImg = cv2.circle(newImg, (x2+shiftX, y2), 10, green, 2, cv2.LINE_AA)
return newImg
def show_correspondence_lines(imgA, imgB, X1, Y1, X2, Y2, line_colors=None):
"""
Visualizes corresponding points between two images by drawing a line segment
between the two images for each (x1,y1) (x2,y2) pair.
Args:
- imgA: A numpy array of shape (M,N,3)
- imgB: A numpy array of shape (D,E,3)
- x1: A numpy array of shape (k,) containing x-locations of keypoints in imgA
- y1: A numpy array of shape (k,) containing y-locations of keypoints in imgA
- x2: A numpy array of shape (j,) containing x-locations of keypoints in imgB
- y2: A numpy array of shape (j,) containing y-locations of keypoints in imgB
- line_colors: A numpy array of shape (N x 3) with colors of correspondence lines (optional)
Returns:
- newImg: A numpy array of shape (max(M,D), N+E, 3)
"""
newImg = hstack_images(imgA, imgB)
shiftX = imgA.shape[1]
X1 = X1.astype(np.int)
Y1 = Y1.astype(np.int)
X2 = X2.astype(np.int)
Y2 = Y2.astype(np.int)
dot_colors = np.random.rand(len(X1), 3)
if line_colors is None:
line_colors = dot_colors
for x1, y1, x2, y2, dot_color, line_color in zip(X1, Y1, X2, Y2, dot_colors,
line_colors):
newImg = cv2.circle(newImg, (x1, y1), 5, dot_color, -1)
newImg = cv2.circle(newImg, (x2+shiftX, y2), 5, dot_color, -1)
newImg = cv2.line(newImg, (x1, y1), (x2+shiftX, y2), line_color, 2,
cv2.LINE_AA)
return newImg
def show_ground_truth_corr(imgA, imgB, corr_file, show_lines=True):
"""
Show the ground truth correspondeces
Args:
- imgA: string, representing the filepath to the first image
- imgB: string, representing the filepath to the second image
- corr_file: filepath to pickle (.pkl) file containing the correspondences
- show_lines: boolean, whether to visualize the correspondences as line segments
"""
imgA = load_image(imgA)
imgB = load_image(imgB)
with open(corr_file, 'rb') as f:
d = pickle.load(f)
if show_lines:
return show_correspondence_lines(imgA, imgB, d['x1'], d['y1'], d['x2'], d['y2'])
else:
# show circles
return show_correspondence_circles(imgA, imgB, d['x1'], d['y1'], d['x2'], d['y2'])
def load_corr_pkl_file(corr_fpath):
""" Load ground truth correspondences from a pickle (.pkl) file. """
with open(corr_fpath, 'rb') as f:
d = pickle.load(f, encoding='latin1')
x1 = d['x1'].squeeze()
y1 = d['y1'].squeeze()
x2 = d['x2'].squeeze()
y2 = d['y2'].squeeze()
return x1,y1,x2,y2
def evaluate_correspondence(imgA, imgB, corr_fpath, scale_factor, x1_est, y1_est,
x2_est, y2_est, confidences=None, num_req_matches=100):
"""
Function to evaluate estimated correspondences against ground truth.
Args:
- imgA: A numpy array of shape (M,N,C) representing a first image
- imgB: A numpy array of shape (M,N,C) representing a second image
- corr_fpath: string, representing a filepath to a .pkl file containing ground truth correspondences
- scale_factor: scale factor on the size of the images
- x1_est: A numpy array of shape (k,) containing estimated x-coordinates of imgA correspondence pts
- y1_est: A numpy array of shape (k,) containing estimated y-coordinates of imgA correspondence pts
- x2_est: A numpy array of shape (k,) containing estimated x-coordinates of imgB correspondence pts
- y2_est: A numpy array of shape (k,) containing estimate
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
计算机视觉作业(二)特征匹配是图像处理和计算机视觉的核心组成部分。在本次实验中,我们将创建一个局部特征匹配算法,并尝试匹配真实场景的多个视图。将实现一个简化版本的sift,用于解决局部特征匹配问题,使检测到的特征对遮挡和杂波具有鲁棒性。由于特性是本地的,可以在一张图像中生成数百或数千个特性,同时能够实现实时性能。我们使用Harris角点检测器和sift特征描述符来生成关键点,同时也使用了自适应非最大抑制来获得图像上的均匀分布的角。
资源推荐
资源详情
资源评论
收起资源包目录
Local-Feature-Matching-master.zip (34个子文件)
Local-Feature-Matching-master
results
vis_circles.jpg 3.81MB
vis_lines.jpg 4.13MB
eval2.jpg 3.54MB
vis_circles3.jpg 3.79MB
vis_lines1.jpg 3.24MB
vis_circles2.jpg 3.19MB
eval1.jpg 3.27MB
vis_lines3.jpg 4.09MB
eval13.jpg 4.04MB
eval.jpg 4.07MB
vis_circles1.jpg 2.9MB
code
.ipynb_checkpoints
proj2-checkpoint.ipynb 8KB
utils.py 10KB
student_harris.py 3KB
proj2.ipynb 190KB
student_sift.py 2KB
__pycache__
utils.cpython-37.pyc 10KB
student_sift.cpython-37.pyc 2KB
student_feature_matching.cpython-37.pyc 1KB
student_harris.cpython-37.pyc 2KB
student_feature_matching.py 1KB
data
Mount Rushmore
9021235130_7c2acd9554_o_to_9318872612_a255c874fb_o.pkl 11KB
9318872612_a255c874fb_o.jpg 1.66MB
9021235130_7c2acd9554_o.jpg 1.54MB
Notre Dame
4191453057_c86028ce1f_o.jpg 692KB
921919841_a30df938f2_o.jpg 926KB
921919841_a30df938f2_o_to_4191453057_c86028ce1f_o.pkl 13KB
Episcopal Gaudi
4386465943_8cf9776378_o.jpg 310KB
4386465943_8cf9776378_o_to_3743214471_1b5bbfda98_o.pkl 13KB
3743214471_1b5bbfda98_o.jpg 1.65MB
annotate_correspondences
sydney_opera_house1.jpg 643KB
sydney_opera_house_correspondences.pkl 860B
collect_ground_truth_corr.py 4KB
sydney_opera_house2.jpg 544KB
共 34 条
- 1
资源评论
路由跳变
- 粉丝: 2738
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功