使用OpenCV与Python识别ArUco标记时出现异常图像伪影
Hey there! Let's break down the potential issues in your code and fix those unexpected problems you're hitting with OpenCV 3.4 and Python 3.6.
First, let's go over the immediate syntax and logic issues in your snippet:
Missing quotes around the image path: When you call
cv2.imread(test.jpg), Python will interprettest.jpgas a variable name instead of a string path. This will throw aNameErrorunless you've defined that variable elsewhere. Fix this by wrapping the filename in quotes:img = cv2.imread("test.jpg")Overwriting the original image when drawing rejects: The
drawDetectedMarkersfunction modifies the input image in-place. When you first draw detected markers onimg, then immediately draw rejected candidates on the sameimg, you're overwriting or mixing the two sets of annotations. To keep them separate, make a copy of the original image before drawing rejects:# Draw detected markers on a copy of the original image markerImg = aruco.drawDetectedMarkers(img.copy(), corners) # Draw rejected candidates on a fresh copy rejectImg = aruco.drawDetectedMarkers(img.copy(), rejects)No check for successful image loading: If the image path is incorrect (or the file doesn't exist),
cv2.imread()will returnNone, which will causearuco.detectMarkers()to crash. Add a quick check to handle this:img = cv2.imread("test.jpg") if img is None: print("Error: Could not load the image! Double-check the file path.") exit()
Here's the revised version of your code with these fixes, plus some optional parameter tuning for better detection:
import numpy as np import cv2 import cv2.aruco as aruco # Initialize the ArUco dictionary test_dict = aruco.getPredefinedDictionary(aruco.DICT_6X6_50) # Optional: Configure detector parameters for better accuracy parameters = aruco.DetectorParameters_create() parameters.adaptiveThreshConstant = 7 # Adjust based on your lighting conditions # Load and validate the image img = cv2.imread("test.jpg") if img is None: print("Error: Could not load the image! Double-check the file path.") exit() # Detect markers corners, ids, rejects = aruco.detectMarkers(img, test_dict, parameters=parameters) # Draw results on separate image copies (include IDs for clarity) markerImg = aruco.drawDetectedMarkers(img.copy(), corners, ids) rejectImg = aruco.drawDetectedMarkers(img.copy(), rejects) # Display results (add this if you haven't already) cv2.imshow("Detected Markers", markerImg) cv2.imshow("Rejected Candidates", rejectImg) cv2.waitKey(0) cv2.destroyAllWindows()
Additional tips to debug unexpected behavior:
- Print the values of
corners,ids, andrejectsto confirm what the detector is picking up. For example:print("Detected IDs:", ids) - Ensure the printed ArUco markers match the dictionary you're using (
DICT_6X6_50means 6x6 markers from a set of 50) - Check lighting conditions: ArUco detection works best with even lighting—avoid harsh shadows or overexposure on the markers
- If markers are small, try resizing the image to a larger resolution before detection
内容的提问来源于stack exchange,提问作者pri0ritize

