Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

ypeError Traceback (most recent call last) Cell In[9], line 1 ----> 1…

  1. User

    ypeError Traceback (most recent call last) Cell In[9], line 1 ----> 1 main(r"C:\Users\new\OneDrive\Master of informatics\CIT690G Computer vision\Assignments and tasks\Assignment 2\partII\partII\fishbowl\fishbowl-00.png", r"C:\Users\new\OneDrive\Master of informatics\CIT690G Computer vision\Assignments and tasks\Assignment 2\partII\partII\fishbowl\fishbowl-01.png", "c/results", True)

    Cell In[8], line 71, in main(image0_path, image1_path, results_dir, use_lmeds) 69 # Find best matches using Ransac or LMedS 70 if(use_lmeds): ---> 71 homography, inliers01 = lmeds((src, dst), threshold_distance=0.8, threshold_inliers=0.3, max_trials=500) 72 print('3. Using LMedS to find the best matching') 73 title = '3. Best matching after LMedS'

    TypeError: cannot unpack non-iterable NoneType object

  2. Assistant

    This error occurs because the function lmeds() is returning a NoneType object, which cannot be unpacked into variables. This could happen if the function is not able to find any good matches or inliers between the two images. A possible solution could be to add some conditions to handle such cases, or to check the input parameters to the function to make sure they are correct.

  3. User

    modify this code to implement lmeds instead of ransac: #RANSAC Algorithm def ransac(data, threshold_distance, threshold_inliers=0, max_trials=200): random_state = np.random.mtrand._rand num_samples = len(data[0]) for num_trials in range(max_trials): # Sample Random 4 point pairs correspondences spl_idxs = random_state.choice(num_samples, 4, replace=False) samples = [d[spl_idxs] for d in data] # Estimate homography H = dlt(samples[0], samples[1]) # Reproject All points using H and compute reprojection error src_pts = np.insert(data[0], 2, 1, axis=1).T dst_pts = np.insert(data[1], 2, 1, axis=1).T projected_pts = np.dot(H, src_pts) error = np.sqrt(np.sum(np.square(dst_pts - (projected_pts / projected_pts[-1])), axis=0)) count_inliers = np.count_nonzero(error < threshold_distance) #Select inliers consistent with the estimate if count_inliers / num_samples > threshold_inliers: inliers = error < threshold_distance src_inliers = src_pts[:, np.argwhere(error < threshold_distance).flatten()][:-1].T dst_inliers = dst_pts[:, np.argwhere(error < threshold_distance).flatten()][:-1].T H = dlt(src_inliers, dst_inliers) break return H, inliers

  4. Assistant

    Here is the modified code implementing LMedS algorithm in place of RANSAC:

    def lmeds(data, threshold_distance, threshold_inliers=0, max_trials=200):
        random_state = np.random.mtrand._rand
        num_samples = len(data[0])
        best_H = None
        best_inliers = None
        best_error = float('inf')
        
        for num_trials in range(max_trials):
            # Sample Random N point pair correspondences
            spl_idxs = random_state.choice(num_samples, 2, replace=False)
            samples = [d[spl_idxs] for d in data]
            
            # Estimate homography H using samples
            H = dlt(samples[0], samples[1])
            
            # Compute reprojection error and select inliers within the threshold distance
            src_pts = np.insert(data[0], 2, 1, axis=1).T
            dst_pts = np.insert(data[1], 2, 1, axis=1).T
            projected_pts = np.dot(H, src_pts)
            error = np.sqrt(np.sum(np.square(dst_pts - (projected_pts / projected_pts[-1])), axis=0))
            inliers = error < threshold_distance
            
            # Compute the median error for inliers
            inlier_error = error[inliers]
            median_error = np.median(inlier_error) if len(inlier_error) > 0 else float('inf')
            
            # If number of inliers surpasses the threshold and the median error is improved,
            # use the current H as the best estimate
            if len(inliers) / num_samples > threshold_inliers and median_error < best_error:
                best_H = H
                best_inliers = inliers
                best_error = median_error
        
        return best_H, best_inliers
    

    This function is similar to the previous RANSAC function, but instead of selecting a fixed number of points at random each time, it selects only two points and estimates a homography using them. It then computes the reprojection error and inliers based on the threshold distance, and selects the best estimate based on the number of inliers and the median error of those inliers. The function returns the best homography estimate and the inliers.

  5. User

    TypeError Traceback (most recent call last) Cell In[17], line 1 ----> 1 main(r"C:\Users\new\OneDrive\Master of informatics\CIT690G Computer vision\Assignments and tasks\Assignment 2\partII\partII\fishbowl\fishbowl-00.png", r"C:\Users\new\OneDrive\Master of informatics\CIT690G Computer vision\Assignments and tasks\Assignment 2\partII\partII\fishbowl\fishbowl-01.png", "c/results", True)

    Cell In[16], line 71, in main(image0_path, image1_path, results_dir, use_lmeds) 69 # Find best matches using Ransac or LMedS 70 if(use_lmeds): ---> 71 homography, inliers01 = lmeds((src, dst), threshold_distance=0.8, threshold_inliers=0.3, max_trials=500) 72 print('3. Using LMedS to find the best matching') 73 title = '3. Best matching after LMedS'

    TypeError: cannot unpack non-iterable NoneType object

  6. Assistant

    This error is similar to the one mentioned earlier, where the function lmeds() is returning a NoneType object, which cannot be unpacked into variables. The issue might be that the function is not able to find good matches or inliers between the two input images, causing it to return None.

    To handle this error, we can add some conditions to check the output of the lmeds() function before trying to unpack it into variables. For example, we can check if the returned homography matrix and inliers array are not NoneType objects before using them. If they are None, we can print an error message or return None to indicate that the function failed to find good matches.

    Here is an example modification of the code to handle this error:

    # Find best matches using Ransac or LMedS
    if use_lmeds:
        homography, inliers = lmeds((src, dst), threshold_distance=0.8, threshold_inliers=0.3, max_trials=500)
        if homography is not None and inliers is not None:
            print('3. Using LMedS to find the best matching')
            title = '3. Best matching after LMedS'
            inlier_matches = [cv2.DMatch(i, i, 0) for i in range(len(inliers)) if inliers[i]]
            result_img = cv2.drawMatches(image0, kp0, image1, kp1, inlier_matches, None, flags=2)
            # Save the result image
            cv2.imwrite(os.path.join(results_dir, 'lmeds_result.png'), result_img)
        else:
            print('Error: Could not find inliers using LMedS')
            homography = None
            inliers = None
    

    Here, we check if the output of lmeds() is not None before proceeding to draw and save the result image. If either homography or inliers is None, we print an error message and set them to None to indicate that the function failed. This way, the program will not try to use the NoneType object in the subsequent processing steps.

  7. User

    def main(image0_path, image1_path, results_dir, use_lmeds=True):

    if not os.path.exists(results_dir):
        os.makedirs(results_dir)
    
    image0 = cv2.imread(image0_path)
    gray0 = cv2.cvtColor(image0, cv2.COLOR_BGR2GRAY)
    image1 = cv2.imread(image1_path)
    gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
    
    # Using Scale Invariant Feature Transform (SIFT) to detect image features
    sift = cv2.SIFT_create(400)
    print('1. Using Scale Invariant Feature Transform (SIFT) to detect image features')
    
    # Detect keypoints in pano0 using OpenCV SIFT detect function
    kp0, des0 = sift.detectAndCompute(gray0, None)
    
    # Detect keypoints in pano1 using OpenCV SIFT detect function
    kp1, des1 = sift.detectAndCompute(gray1, None)
    
    # Visualize the detected and matched features
    fig, ax = plt.subplots(1, 2, figsize=(10, 5))
    fig.suptitle('1. Detected and matched features', fontsize=20)
    plt.subplot(121)
    plt.imshow(cv2.drawKeypoints(gray0, kp0, None), cmap="gray")
    plt.title("Image 0 keypoints")
    plt.subplot(122)
    plt.imshow(cv2.drawKeypoints(gray1, kp1, None), cmap="gray")
    plt.title("Image 1 keypoints")
    fig.savefig(os.path.join(results_dir, 'keypoints.png'), dpi=fig.dpi)
    cv2.imshow('keypoints', cv2.imread(os.path.join(results_dir, 'keypoints.png')))
    cv2.waitKey(10)
    
    # Match descriptors between images
    matches = match_descriptors(des0, des1, cross_check=True)
    print('2. Implement a simple feature matching and visualize the detected and matched features')
    
    # Restore the openCV style keypoints into a 2d array type keypoints
    keypoints0 = []
    a = 0
    for i in kp0:
        keypoints0.append(list(kp0[a].pt)[::-1])
        a += 1
    keypoints0 = np.array(keypoints0)
    
    keypoints1 = []
    b = 0
    for j in kp1:
        keypoints1.append(list(kp1[b].pt)[::-1])
        b += 1
    keypoints1 = np.array(keypoints1)
    
    # 
    fig, ax = plt.subplots(1, 1, figsize=(10, 5))
    plot_matches(ax, image0, image1, keypoints0, keypoints1, matches)
    ax.axis('off')
    fig.suptitle('2. Initial matching', fontsize=20)
    fig.savefig(os.path.join(results_dir, 'Initial matching.png'), dpi=fig.dpi)
    cv2.imshow('Initial matching', cv2.imread(os.path.join(results_dir, 'Initial matching.png')))
    cv2.waitKey(10)
    
    # Select keypoints from
    #   * source (image to be registered): pano1
    #   * target (reference image): pano0
    src = keypoints1[matches[:, 1]][:, ::-1]
    dst = keypoints0[matches[:, 0]][:, ::-1]
    
    # Find best matches using Ransac or LMedS
    if(use_lmeds):
        homography, inliers01 = lmeds((src, dst), threshold_distance=0.8, threshold_inliers=0.3, max_trials=500)
        print('3. Using LMedS to find the best matching')
        title = '3. Best matching after LMedS'
    else:
        homography, inliers01 = ransac((src, dst), threshold_distance=0.8, threshold_inliers=0.3, max_trials=500)
        print('3. Using RANSAC to find the best matching')
        title = '3. Best matching after RANSAC'
    
    # Best match subset for pano0 -> pano1
    fig, ax = plt.subplots(1, 1, figsize=(10, 5))
    plot_matches(ax, image0, image1, keypoints0, keypoints1, matches[inliers01])
    ax.axis('off')
    fig.savefig(os.path.join(results_dir, 'RANSAC matching.png'), dpi=fig.dpi)
    cv2.imshow('RANSAC MATCH', cv2.imread(os.path.join(results_dir, 'RANSAC matching.png')))
    cv2.waitKey(10)
    
    # Image warping and stitching
    print('4. Perform Image warping and stitching')
    result = stitching(homography, gray0, image0, image1)
    fig, ax = plt.subplots(1, 1, figsize=(10, 5))
    plt.imshow(result, cmap="gray")
    fig.suptitle('4. Image Stitching Result', fontsize=20)
    fig.savefig(os.path.join(results_dir, 'LMedS matching.png'), dpi=fig.dpi)
    cv2.imshow('LMedS MATCH', cv2.imread(os.path.join(results_dir, 'LMedS matching.png')))
    print("You can also review the generated visualization results in the 'results' folder. Press any key to exit.")
    cv2.waitKey()
    

    def stitching(homography, gray0, image0, image1): # Shape registration target r, c = gray0.shape[:2]

    # Note that transformations take coordinates in (x, y) format,
    # not (row, column), in order to be consistent with most literature
    corners = np.array([[0, 0, 1],
                        [0, r, 1],
                        [c, 0, 1],
                        [c, r, 1]])
    
    # Warp the image corners to their new positions
    warped_corners01 = np.dot(homography, corners.T)
    warped_corners01 = warped_corners01[:2, :].T
    
    # Find the extents of both the reference image and the warped
    # target image
    all_corners = np.vstack((warped_corners01, corners[:, :2]))
    
    # The overally output shape will be max - min
    corner_min = np.min(all_corners, axis=0)
    corner_max = np.max(all_corners, axis=0)
    output_shape = (corner_max - corner_min)
    
    # Ensure integer shape with np.ceil and dtype conversion
    output_shape = np.ceil(output_shape[::-1]).astype(int)
    
    # This in-plane offset is the only necessary transformation for the middle image
    offset1 = SimilarityTransform(translation=-corner_min)
    tform = ProjectiveTransform(homography)
    
    # Warp pano1 to pano0 using 3rd order interpolation
    transform01 = (tform + offset1).inverse
    image1_warped = warp(image1, transform01, order=3,
                     output_shape=output_shape, cval=-1)
    
    image1_mask = (image1_warped != -1)  # Mask == 1 inside image
    image1_warped[~image1_mask] = 0  # Return background values to 0
    
    # Translate pano0 into place
    image0_warped = warp(image0, offset1.inverse, order=3,
                     output_shape=output_shape, cval=-1)
    
    image0_mask = (image0_warped != -1)  # Mask == 1 inside image
    image0_warped[~image0_mask] = 0  # Return background values to 0
    
    # Add the images together. This could create dtype overflows!
    # We know they are are floating point images after warping, so it's OK.
    merged = (image0_warped + image1_warped)
    
    # Track the overlap by adding the masks together
    overlap = (image0_mask * 1.0 +  # Multiply by 1.0 for bool -> float conversion
               image1_mask)
    
    # Normalize through division by `overlap` - but ensure the minimum is 1
    normalized = merged / np.maximum(overlap, 1)
    return normalized
    

    Modify this code to use the previous lmeds algorithm

  8. Assistant

    Here is the modified code that uses the lmeds algorithm instead of RANSAC:

    def main(image0_path, image1_path, results_dir, use_lmeds=True):
        
        if not os.path.exists(results_dir):
            os.makedirs(results_dir)
    
        image0 = cv2.imread(image0_path)
        gray0 = cv2.cvtColor(image0, cv2.COLOR_BGR2GRAY)
        image1 = cv2.imread(image1_path)
        gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
    
        # Using Scale Invariant Feature Transform (SIFT) to detect image features
        sift = cv2.SIFT_create(400)
        print('1. Using Scale Invariant Feature Transform (SIFT) to detect image features')
    
        # Detect keypoints in pano0 using OpenCV SIFT detect function
        kp0, des0 = sift.detectAndCompute(gray0, None)
    
        # Detect keypoints in pano1 using OpenCV SIFT detect function
        kp1, des1 = sift.detectAndCompute(gray1, None)
    
        # Visualize the detected and matched features
        fig, ax = plt.subplots(1, 2, figsize=(10, 5))
        fig.suptitle('1. Detected and matched features', fontsize=20)
        plt.subplot(121)
        plt.imshow(cv2.drawKeypoints(gray0, kp0, None), cmap="gray")
        plt.title("Image 0 keypoints")
        plt.subplot(122)
        plt.imshow(cv2.drawKeypoints(gray1, kp1, None), cmap="gray")
        plt.title("Image 1 keypoints")
        fig.savefig(os.path.join(results_dir, 'keypoints.png'), dpi=fig.dpi)
        cv2.imshow('keypoints', cv2.imread(os.path.join(results_dir, 'keypoints.png')))
        cv2.waitKey(10)
    
        # Match descriptors between images
        matches = match_descriptors(des0, des1, cross_check=True)
        print('2. Implement a simple feature matching and visualize the detected and matched features')
    
        # Restore the openCV style keypoints into a 2d array type keypoints
        keypoints0 = []
        a = 0
        for i in kp0:
            keypoints0.append(list(kp0[a].
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.