我通过“使用Python的Opencv作为示例”进行学习
然后,我不修复它。
python 3.6。,opencv 3.4,pycharm
import argparse
import cv2
import numpy as np
#epipolar geometry
def build_arg_parser():
parser = argparse.ArgumentParser(description='find fundamental matrix using the two input stereo images and draw epipolar lines')
parser.add_argument("--img-left", dest="img_left", required=True, help="image captured from the left view")
parser.add_argument("--img-right", dest="img_right", required=True, help="image captured from the right view")
parser.add_argument("--feature-type", dest="feature_type", required=True, help="Feature extractor that will be used; can be either 'sift' or 'surf'")
return parser
def draw_lines(img_left, img_right, lines, pts_left, pts_right):
h, w = img_left.shape
img_left = cv2.cvtColor(img_left, cv2.COLOR_GRAY2BGR)
img_right = cv2.cvtColor(img_right, cv2.COLOR_GRAY2BGR)
for line, pt_left, pt_right in zip(lines, pts_left, pts_right):
x_start,y_start = map(int, [0, -line[2]/line[1]])
x_end,y_end = map(int, [w, -(line[2]+line[0]*w)/line[1]])
color = tuple(np.random.randint(0,255,2).tolist())
cv2.line(img_left, (x_start,y_start), (x_end,y_end), color, 1)
cv2.circle(img_left, tuple(pt_left), 5, color, -1)
cv2.circle(img_right, tuple(pt_right), 5, color, -1)
return img_left, img_right
def get_descriptors(gray_image, feature):
if feature_type == 'surf':
feature_extractor = cv2.xfeatures2d_SURF.create()
elif feature_type == 'sift':
feature_extractor = cv2.xfeatures2d_SIFT.create()
else :
raise TypeError("invalid feature type; should be either 'surf' or 'sift'")
keypoints, descriptors = feature_extractor.detectAndCompute(gray_image, None)
return keypoints, descriptors
if __name__ == '__main__':
args = build_arg_parser().parse_args()
img_left = cv2.imread('images/left.jpg', 0) # left image
img_right = cv2.imread('images/right.jpg', 0) # right image
feature_type = args.feature_type
if feature_type not in ['sift', 'surf']:
raise TypeError("invalid ")
scaling_factor = 1.0
img_left = cv2.resize(img_left, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
img_right = cv2.resize(img_right, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
kps_left, des_left = get_descriptors(img_left, feature_type)
kps_right, des_right = get_descriptors(img_right, feature_type)
# flann parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
# get the matches based on ter descriptors
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des_left, des_right, k=2)
pts_left_image = []
pts_right_image = []
# radio test to retain only the good matches
for i, (m, n) in enumerate(matches):
if m.distance < 0.7*n.distance:
pts_left_image.append(kps_left[m.queryIdx].pt)
pts_right_image.append(kps_right[m.trainIdx].pt)
pts_left_image = np.float32(pts_left_image)
pts_right_image = np.float32(pts_right_image)
F, mask = cv2.findFundamentalMat(pts_left_image, pts_right_image, cv2.FM_LMEDS)
# selecting only the inliers
pts_left_image = pts_left_image[mask.ravel()==1]
pts_right_image = pts_right_image[mask.ravel()==1]
# drawing the lines on left image and the corresponding feature points on the right image
lines1= cv2.computeCorrespondEpilines(pts_right_image.reshape(-1, 1, 2), 2, F)
lines1 = lines1.reshape(-1, 3)
img_left_lines, img_right_pts = draw_lines(img_left, img_right, lines1, pts_left_image, pts_right_image)
# drawing the lines on right image and the corresponding feature points on the left image
lines2 = cv2.computeCorrespondEpilines(pts_left_image.reshape(-1,1,2), 1, F)
lines2 = lines2.reshape(-1, 3)
img_right_lines, img_left_pts = draw_lines(img_right, img_left, lines2, pts_right_image, pts_left_image)
cv2.imshow('epi lines on left image', img_left_lines)
cv2.imshow('feature points on right image', img_right_pts)
cv2.imshow('epi lines on right image ', img_right_lines)
cv2.imshow('feature points on left image', img_left_pts)
cv2.waitKey()
cv2.destroyAllWindows()
错误消息
用法:Chapter11.py [-h] --img-left IMG_LEFT --img-right IMG_RIGHT --feature-type FEATURE_TYPE第11.py章:错误:需要以下参数:--img-left,--img-right, --feature-type
以退出代码2完成的过程
救救我~~
ps。我的路径中有图片。