Real-time color detection using webcam and Arduino

Опубликовано: 19 Октябрь 2024
на канале: Robotique Site
4,348
37

Real-time color detection is the process of detecting the color of an object or image in real-time using a camera and a computer. It is commonly used in robotics, automation, and computer vision applications.
There are several steps involved in real-time color detection:
1- Capture the image: The first step is to capture an image using a camera or a video stream.
2- Preprocess the image: The image is then preprocessed to remove noise and unwanted details, and to enhance the contrast and brightness.
3- Color space conversion: The image is converted from the RGB color space to a color space that separates the color information, such as the HSV or HSL color space.
4- Thresholding: The color of interest is then isolated by thresholding the image based on the color range in the selected color space. This creates a binary mask that highlights the pixels that belong to the object of interest.
5- Contour detection: The contours of the object are detected using the binary mask. This step helps to identify the shape of the object and its location in the image.
6- Object tracking: The position and size of the object are then tracked over time to enable real-time color detection.

Real-time color detection can be implemented using programming languages such as Python, MATLAB, and OpenCV. There are also several pre-built libraries and tools available that make it easier to implement real-time color detection in your applications.

Real-time color detection using a webcam and an Arduino board can be achieved by following these steps:
1- Connect the webcam to your computer and install the necessary drivers.
2- Install the OpenCV library and Python on your computer.
3- Connect the Arduino board to your computer using a USB cable.
4- Write a Python script that captures frames from the webcam, processes them to detect the color of interest, and sends the data to the Arduino board.
5- Write an Arduino sketch that receives the data from the Python script and controls an RGB LED based on the detected color.

Here is an example Python script that detects the color red in real-time using the OpenCV library:
import cv2
import serial
ser = serial.Serial('COM3', 9600) # Replace COM3 with the serial port of your Arduino board
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_red = np.array([0, 50, 50])
upper_red = np.array([10, 255, 255])
mask = cv2.inRange(hsv, lower_red, upper_red)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) superior 0:
c = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if w superior 50 and h superior 50:
ser.write(b'r')
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

The script captures frames from the webcam and applies a threshold to isolate the color red. It then finds the contours of the red object and sends a 'r' character to the Arduino board if the object is large enough. Finally, it displays the frame with the detected object highlighted in green.

Here is an example Arduino sketch that controls an RGB LED based on the data received from the Python script:

scss
Copy code
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop() {
if (Serial.available() superior 0) {
char color = Serial.read();
if (color == 'r') {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
} else if (color == 'g') {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
} else if (color == 'b') {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
} else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
}
}
The sketch reads the data sent by the Python script and sets the color of the RGB LED based on the received character. In this example, if the Python script detects the color red, the Arduino board will turn on the red LED and turn off the green and blue LEDs.

The script captures frames from the webcam and applies a threshold to isolate the color red. It then finds the contours of the red object and sends a 'r' character to the Arduino board if the object is large enough. Finally, it displays the frame with the detected object highlighted in green.



For more details you can see this article:
https://www.robotique.tech/robotics/r...