RGB 카메라
카메라는 앞에 있는 것의 이미지를 줘요. 벽 토큰을 찾고 읽으려면 카메라가 필요해요. 스타터 로봇에는 camera라는 이름의 64 × 40 픽셀 카메라가 하나 있어요.
픽셀 읽기
섹션 제목: “픽셀 읽기”이미지는 픽셀의 격자예요. 각 픽셀에는 0부터 255까지의 빨강, 초록, 파랑 값이 있고, (0, 0, 0)은 검정, (255, 255, 255)는 흰색이에요.
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())
camera = robot.getDevice("camera")camera.enable(timeStep)width = camera.getWidth()height = camera.getHeight()print("The camera has", width, "x", height, "pixels")
while robot.step(timeStep) != -1: image = camera.getImage() # The colour of the pixel in the center of the image r = camera.imageGetRed(image, width, width // 2, height // 2) g = camera.imageGetGreen(image, width, width // 2, height // 2) b = camera.imageGetBlue(image, width, width // 2, height // 2) print("Center:", r, g, b)#include <iostream>#include <webots/Robot.hpp>#include <webots/Camera.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
Camera *camera = robot->getCamera("camera"); camera->enable(timeStep); int width = camera->getWidth(); int height = camera->getHeight(); cout << "The camera has " << width << " x " << height << " pixels" << endl;
while (robot->step(timeStep) != -1) { const unsigned char *image = camera->getImage(); // The colour of the pixel in the center of the image int r = Camera::imageGetRed(image, width, width / 2, height / 2); int g = Camera::imageGetGreen(image, width, width / 2, height / 2); int b = Camera::imageGetBlue(image, width, width / 2, height / 2); cout << "Center: " << r << " " << g << " " << b << endl; }
delete robot; return 0;}OpenCV 사용하기
섹션 제목: “OpenCV 사용하기”OpenCV는 이미지를 처리하는 라이브러리예요. 모양을 찾거나 색을 비교하는 등 많은 일을 할 수 있어요. Webots 이미지는 픽셀마다 BGRA 순서로 4개의 값을 가지며, OpenCV에서 바로 사용할 수 있어요. 이 프로그램은 이미지와 그 밝은 부분을 두 개의 창에 보여 줘요.
from controller import Robotimport cv2import numpy as np
robot = Robot()timeStep = int(robot.getBasicTimeStep())
camera = robot.getDevice("camera")camera.enable(timeStep)
while robot.step(timeStep) != -1: # Webots images have 4 channels per pixel, in BGRA order image = np.frombuffer(camera.getImage(), np.uint8).reshape((camera.getHeight(), camera.getWidth(), 4)) frame = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) # Bright parts in white
cv2.imshow("camera", cv2.resize(frame, None, fx=4, fy=4, interpolation=cv2.INTER_NEAREST)) cv2.imshow("threshold", cv2.resize(thresh, None, fx=4, fy=4, interpolation=cv2.INTER_NEAREST)) cv2.waitKey(1) # Shows the windows#include <opencv2/opencv.hpp>#include <webots/Robot.hpp>#include <webots/Camera.hpp>
using namespace webots;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
Camera *camera = robot->getCamera("camera"); camera->enable(timeStep);
while (robot->step(timeStep) != -1) { // Webots images have 4 channels per pixel, in BGRA order cv::Mat image(camera->getHeight(), camera->getWidth(), CV_8UC4, (void *)camera->getImage()); cv::Mat frame, gray, thresh; cv::cvtColor(image, frame, cv::COLOR_BGRA2BGR); cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY); cv::threshold(gray, thresh, 200, 255, cv::THRESH_BINARY); // Bright parts in white
cv::Mat big; cv::resize(frame, big, cv::Size(), 4, 4, cv::INTER_NEAREST); cv::imshow("camera", big); cv::resize(thresh, big, cv::Size(), 4, 4, cv::INTER_NEAREST); cv::imshow("threshold", big); cv::waitKey(1); // Shows the windows }
delete robot; return 0;}왼쪽은 문자 피해자 앞에 있는 스타터 로봇의 카메라 이미지예요. 오른쪽은 이 프로그램이 찾아낸 그 이미지의 밝은 부분이에요.

이런 창은 집에서 테스트할 때 쓰는 것이니, 대회에서는 꺼 두세요.
해상도
섹션 제목: “해상도”해상도가 높을수록 더 먼 곳의 토큰을 볼 수 있지만, 비용이 더 들고 처리하는 데 시간이 더 걸려요. 각 카메라의 해상도는 Robot Customizer에서 정해요.
