跳到內容
中文(繁體)

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 的影像每個像素有 4 個數值,順序為 BGRA,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 中選擇每台攝影機的解析度。