콘텐츠로 이동
한국어

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)

OpenCV는 이미지를 처리하는 라이브러리예요. 모양을 찾거나 색을 비교하는 등 많은 일을 할 수 있어요. Webots 이미지는 픽셀마다 BGRA 순서로 4개의 값을 가지며, OpenCV에서 바로 사용할 수 있어요. 이 프로그램은 이미지와 그 밝은 부분을 두 개의 창에 보여 줘요.

from controller import Robot
import cv2
import 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

왼쪽은 문자 피해자 앞에 있는 스타터 로봇의 카메라 이미지예요. 오른쪽은 이 프로그램이 찾아낸 그 이미지의 밝은 부분이에요.

문자 피해자를 찍은 카메라 이미지와, 흰색으로 표시한 밝은 부분

이런 창은 집에서 테스트할 때 쓰는 것이니, 대회에서는 꺼 두세요.

해상도가 높을수록 더 먼 곳의 토큰을 볼 수 있지만, 비용이 더 들고 처리하는 데 시간이 더 걸려요. 각 카메라의 해상도는 Robot Customizer에서 정해요.