بینایی ماشین در ربات ها با OpenCV: از کالیبراسیون دوربین تا مارکر ArUco / Machine Vision in Robots with OpenCV: From Camera Calibration to ArUco Markers
بینایی ماشین به ربات اجازه می دهد محیط را ببیند، اشیا را تشخیص دهد و بر اساس آنچه می بیند تصمیم بگیرد. کتابخانه متن باز OpenCV پرکاربردترین ابزار این کار است؛ مجموعه ای از توابع آماده پردازش تصویر که روی بردهای نهفته ای مانند Raspberry Pi و Jetson نیز اجرا می شود و به ربات ها «چشم» می دهد.
خط لوله معمول بینایی در ربات چند مرحله دارد: دریافت تصویر با VideoCapture، پیش پردازش (تبدیل به خاکستری، تاری و آستانه گذاری یا کار در فضای رنگ HSV)، تشخیص ویژگی یا کانتور، تشخیص و ردیابی شیء، تخمین موقعیت و ارسال فرمان به ربات. ماژول DNN مدل های آماده یادگیری عمیق مانند YOLO و MobileNet را اجرا می کند.
پیش از هر اندازه گیری دقیق، دوربین باید کالیبره شود. کالیبراسیون با الگوی شطرنجی انجام می شود: تابع findChessboardCorners گوشه ها را می یابد، cornerSubPix آن ها را پالایش می کند و calibrateCamera پارامترها را محاسبه می کند؛ حداقل ۱۰ الگو توصیه می شود. خروجی شامل ماتریس دوربین ۳×۳ و ضرایب اعوجاج است. تصویر با undistort اصلاح می شود؛ هرچه خطای بازتابش به صفر نزدیک تر باشد، کالیبراسیون دقیق تر است.
یکی از ابزارهای محبوب موقعیت یابی، مارکر ArUco است: مربعی باینری با حاشیه سیاه پهن و ماتریس داخلی که شناسه (id) مارکر را تعیین می کند؛ یک مارکر با چهار گوشه اش برای تخمین موقعیت کافی است. دیکشنری ها اندازه و تعداد مارکر را مشخص می کنند؛ برای نمونه DICT_6X6_250 یعنی ۶×۶ بیت، ۲۵۰ مارکر و حداقل فاصله همینگ ۱۱. تابع ArucoDetector::detectMarkers گوشه ها و شناسه ها را برمی گرداند و با solvePnP و پارامترهای کالیبراسیون، بردارهای چرخش (rvec) و جابه جایی (tvec) به دست می آید.
اجرای این پردازش ها روی بردهای نهفته چالش هایی دارد. برای Raspberry Pi، نسخه ۴ یا ۸ گیگابایت RAM، منبع ۵ ولت/۵ آمپر و خنک کننده فعال توصیه می شود. در مثال های OpenCV، طبقه بندی تصویر با MobileNetV2 زمان استنتاج ۲۸٫۸۹ تا ۶۹٫۶۹ میلی ثانیه و حافظه حدود ۵۴۳ تا ۶۱۳ مگابایت داشت.
نکته مهم این است که OpenCV به تنهایی یک سامانه SLAM کامل نیست؛ بلکه بلوک های جلویی SLAM بصری را می دهد: تشخیص و تطبیق ویژگی، جریان نوری، تخمین موقعیت دوربین و کالیبراسیون. نقشه برداری و بستن حلقه در سامانه های SLAM جداگانه روی همین بلوک ها ساخته می شود. با این ترکیب، ربات های متحرک هم زمان نقشه می سازند و در آن موقعیت یابی می کنند.
Machine vision lets a robot see its environment, recognise objects, and decide based on what it sees. The open-source OpenCV library is the most widely used tool for this task: a set of ready-made image-processing functions that also runs on embedded boards such as Raspberry Pi and Jetson, giving robots "eyes".
The typical vision pipeline has several stages: capturing the image with VideoCapture, preprocessing (converting to grayscale, blurring, and thresholding, or working in HSV colour space), feature or contour detection, object detection and tracking, position estimation, and sending a command to the robot. The DNN module runs ready-made deep-learning models such as YOLO and MobileNet.
Before any accurate measurement, the camera must be calibrated. Calibration uses a chessboard pattern: findChessboardCorners finds the corners, cornerSubPix refines them, and calibrateCamera computes the parameters; at least 10 patterns are recommended. The output includes the 3×3 camera matrix and the distortion coefficients. The image is corrected with undistort; the closer the re-projection error is to zero, the more accurate the calibration.
One popular positioning tool is the ArUco marker: a binary square with a wide black border and an inner matrix that sets the marker's id; a single marker, with its four corners, is enough to estimate a pose. Dictionaries define the size and number of markers; for example, DICT_6X6_250 means 6×6 bits, 250 markers, and a minimum Hamming distance of 11. ArucoDetector::detectMarkers returns the corners and ids, and with solvePnP and the calibration parameters, the rotation (rvec) and translation (tvec) vectors are obtained.
Running this processing on embedded boards is challenging. For Raspberry Pi, 4 or 8 GB of RAM, a 5 V/5 A supply, and active cooling are recommended. In OpenCV examples, image classification with MobileNetV2 took 28.89 to 69.69 ms per inference and about 543 to 613 MB of memory.
Importantly, OpenCV alone is not a complete SLAM system; it provides the front-end blocks of visual SLAM: feature detection and matching, optical flow, camera pose estimation, and calibration. Mapping and loop closure are built in separate SLAM systems on these blocks. With them, mobile robots build a map and localise within it at the same time.
کلیدواژه ها: بینایی ماشین، OpenCV، کالیبراسیون دوربین، مارکر ArUco، تشخیص شیء، SLAM بصری، Raspberry Pi؛ Machine vision, OpenCV, camera calibration, ArUco marker, object detection, visual SLAM, Raspberry Pi
منابع:
- OpenCV 4.13.0, "Detection of ArUco Markers", https://docs.opencv.org/4.13.0/d5/dae/tutorial_aruco_detection.html
- OpenCV 4.13.0, "Camera Calibration", https://docs.opencv.org/4.13.0/dc/dbb/tutorial_py_calibration.html
- OpenCV, "Configuring Raspberry Pi for OpenCV: Camera & Cooling Workflow (2025 Refresh)", https://opencv.org/configuring-raspberry-pi-for-opencv-camera-cooling/
- NVIDIA Developer, "OpenCV with CUDA for Jetson Nano", https://developer.nvidia.com/embedded/community/jetson-projects/opencv
- OpenCV, "OpenCV DNN Module", https://opencv.org/opencv-dnn-module/