ข้ามไปยังเนื้อหา
ไทย

กล้อง 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