为什么我的代码总是告诉NotImplementedError?

时间:2020-11-03 11:00:54

标签: python numpy

我尝试读取两个图像,但它告诉我NotImplementedError。我正在使用Python的OpenCV在我们的solution()函数内部拼接两个图像代码,但在HW代码中仍然是NotImplementError。

这是我的完整代码

import cv2
import numpy as np
import random


def solution(left_img, right_img):

    # left_img = cv2.imread('left.jpg')
    # right_img = cv2.imread('right.jpg')
    global homography
    left_image1 = cv2.cvtColor(left_img, cv2.COLOR_BGR2GRAY)
    right_image1 = cv2.cvtColor(right_img, cv2.COLOR_BGR2GRAY)

    sift = cv2.xfeatures2d.SIFT_create()

    # find key points
    keypoints_1, descriptors_1 = sift.detectAndCompute(right_image1, None)
    keypoints_2, descriptors_2 = sift.detectAndCompute(left_image1, None)

    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    match = cv2.FlannBasedMatcher(index_params, search_params)

    # match = cv2.BFMatcher()
    matches = match.knnMatch(descriptors_1, descriptors_2, k=2)

    good = []
    for m, n in matches:
        if m.distance < 0.03 * n.distance:
            good.append(m)

    draw_params = dict(matchColor=(0, 255, 0), singlePointColor=None, flags=2)

    img3 = cv2.drawMatches(right_img, keypoints_1, left_img, keypoints_2, good, None,     **draw_params)

    MIN_MATCH_COUNT = 10
    if len(good) > MIN_MATCH_COUNT:
        src_pts = np.float32([keypoints_1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
        dst_pts = np.float32([keypoints_2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

        homography, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
        h, w = right_image1.shape

        pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
        dst = cv2.perspectiveTransform(pts, homography)

        img2 = cv2.polylines(left_image1, [np.int32(dst)], True, 255, 3, cv2.LINE_AA)

    dst = cv2.warpPerspective(right_img, homography, (left_img.shape[1] + right_img.shape[1], left_img.shape[0]))
    dst[0:left_img.shape[0], 0:left_img.shape[1]] = left_img

    # final_result = cv2.imshow("task1 result.jpg", dst)
    # return final

    raise NotImplementedError



if __name__ == "__main__":
    left_img = cv2.imread('left.jpg')
    right_img = cv2.imread('right.jpg')
    result_image = solution(left_img, right_img)
    cv2.imwrite('results/task1_result.jpg', result_image)

输出始终为NotImplementedError,O对这个问题一无所知

有人可以支持我修复该错误吗? 非常感谢

1 个答案:

答案 0 :(得分:1)

因为您在NotImplementedError函数的末尾抬起了solution。删除该行,并可能将其替换为return final_result