Ir al contenido
Español

Cámara RGB

La cámara da una imagen de lo que tiene delante. La necesitas para encontrar y leer las marcas en las paredes. El robot inicial tiene una cámara, llamada camera, de 64 × 40 píxeles.

Una imagen es una cuadrícula de píxeles. Cada uno tiene un valor de rojo, verde y azul de 0 a 255, donde (0, 0, 0) es negro y (255, 255, 255) es blanco.

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 es una biblioteca para procesar imágenes: encontrar formas, comparar colores y mucho más. Las imágenes de Webots tienen 4 valores por píxel, en orden BGRA, que OpenCV puede usar directamente. Este programa muestra la imagen, y sus partes brillantes, en dos ventanas:

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

A la izquierda, una imagen de la cámara del robot inicial delante de una víctima de letra. A la derecha, sus partes brillantes, tal como las encuentra este programa.

Una imagen de cámara de una víctima de letra y sus partes brillantes en blanco

Ventanas como estas son para hacer pruebas en casa: desactívalas para la competición.

Las resoluciones más altas ven las marcas desde más lejos, pero cuestan más y tardan más en procesarse. Eliges la resolución de cada cámara en el Robot Customizer.