error Traceback (most recent call last) Cell In[31], line 1 ----> 1…
- User
error Traceback (most recent call last) Cell In[31], 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")
Cell In[30], line 29, in main(image0_path, image1_path) 27 plt.title("Image 1 keypoints") 28 #fig.savefig(args.results_dir+'/keypoints.png', dpi=fig.dpi) ---> 29 cv2.imshow('keypoints', fig) 30 cv2.waitKey(10) 32 # Match descriptors between images
error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'imshow'
Overload resolution failed:
- mat is not a numpy array, neither a scalar
- Expected Ptrcv::cuda::GpuMat for argument 'mat'
- Expected Ptrcv::UMat for argument 'mat'
- Assistant
The error is caused by passing the wrong argument to the
cv2.imshow()function. The second argument should be a NumPy array or OpenCV image, but instead a Matplotlib Figure object is passed. To fix this, the Figure object should be converted to a NumPy array before passing it tocv2.imshow().Here's an updated version of the code:
import cv2 import matplotlib.pyplot as plt import numpy as np def main(image0_path, image1_path): # Load images image0 = cv2.imread(image0_path, cv2.IMREAD_GRAYSCALE) image1 = cv2.imread(image1_path, cv2.IMREAD_GRAYSCALE) # Find ORB keypoints and descriptors in both images orb = cv2.ORB_create() keypoints0, descriptors0 = orb.detectAndCompute(image0, None) keypoints1, descriptors1 = orb.detectAndCompute(image1, None) # Visualize keypoints img0 = cv2.drawKeypoints(image0, keypoints0, None, color=(0,255,0)) img1 = cv2.drawKeypoints(image1, keypoints1, None, color=(0,255,0)) cv2.imshow('Image 0 keypoints', img0) cv2.waitKey(0) cv2.imshow('Image 1 keypoints', img1) cv2.waitKey(0) # Match descriptors between images bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(descriptors0, descriptors1) matches = sorted(matches, key=lambda x:x.distance) # Visualize matches img_matches = cv2.drawMatches(image0, keypoints0, image1, keypoints1, matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) cv2.imshow('Matches', img_matches) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == '__main__': main("fishbowl-00.png", "fishbowl-01.png") - Assistant
Is there anything else you need help with?
- User
how to add results directory def main(image0_path, image1_path, results_dir):
- Assistant
To add a results directory to the
main()function, you can modify the function definition to include a new argumentresults_dir, which will be the path to the directory where the results will be saved. Then, you can modify the code to save the output images to the specified directory instead of displaying them using . - User
import os
- Assistant
osis a built-in Python module that provides a way of using operating system dependent functionality. It provides functions for working with files and directories, process management, and more.Some common functions in the
osmodule include:os.getcwd()- returns the current working directoryos.listdir(path)- returns a list of files and directories in the specified directorypathos.path.join(*paths)- joins multiple path components into a single pathos.path.exists(path)- returnsTrueif the specified path existsos.path.isdir(path)- returnsTrueif the specified path is a directory
By importing the
osmodule, you can use these functions to perform file and directory operations in your code. - User
NameError Traceback (most recent call last) Cell In[46], 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")
Cell In[45], line 30, in main(image0_path, image1_path, results_dir) 28 plt.imshow(cv2.drawKeypoints(gray1, kp1, None), cmap="gray") 29 plt.title("Image 1 keypoints") ---> 30 fig.savefig(args.results_dir+'/keypoints.png', dpi=fig.dpi) 31 cv2.imshow('keypoints', fig) 32 cv2.waitKey(10)
NameError: name 'args' is not defined
- Assistant
The error is caused by referring to
args.results_dirinstead ofresults_dirin the call tofig.savefig(). In the code provided,argsis not defined, which is why theNameErroris raised.To fix this, replace
args.results_dirwithresults_dirin thefig.savefig()call:fig.savefig(os.path.join(results_dir, 'keypoints.png'), dpi=fig.dpi)This should save the image to the
results_dirwithout referring toargs. - User
NameError Traceback (most recent call last) Cell In[8], 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")
Cell In[7], line 70, in main(image0_path, image1_path, results_dir) 67 dst = keypoints0[matches[:, 0]][:, ::-1] 69 # Find best matches using Ransac or LMedS ---> 70 if(args.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')
NameError: name 'args' is not defined
- Assistant
The error is caused by referring to
args.lmedsin theifstatement, butargsis not defined.Assuming that
lmedsis a boolean argument that specifies whether to use RANSAC or LMedS for matching, you can modify the function to include a new Boolean argument, , which will be used to determine whether to use LMedS or RANSAC. - User
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, inliersModify this code to perform lmeds instead of ransac
- Assistant
To modify the
ransac()function to perform LMedS instead of RANSAC, you can replace the RANSAC algorithm with the LMedS algorithm. LMedS (Least Median of Squares) is a variation of the RANSAC algorithm that is more resilient to outliers. - 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
- Assistant
The error is caused by trying to unpack a
NoneTypeobject in the linehomography, inliers01 = lmeds(...). This means that thelmeds()function is not returning a value, but is instead returningNone.This can happen if the L
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.