[Diao Ye Learns Programming] MicroPython Manual OpenMV Cam Automatic White Balance

Insert image description here
MicroPython is a lightweight version of the interpreter designed for running the Python 3 programming language in embedded systems. Compared with regular Python, the MicroPython interpreter is small (only about 100KB) and is compiled into a binary Executable file to run, resulting in higher execution efficiency. It uses a lightweight garbage collection mechanism and removes most of the Python standard library to accommodate resource-constrained microcontrollers.

The main features of MicroPython include:
1. The syntax and functions are compatible with standard Python, making it easy to learn and use. Supports most core syntax of Python.
2. Directly access and control hardware, control GPIO, I2C, SPI, etc. like Arduino.
3. Powerful module system, providing file system, network, graphical interface and other functions.
4. Support cross-compilation to generate efficient native code, which is 10-100 times faster than the interpreter.
5. The amount of code is small and the memory usage is small. It is suitable for running on MCU and development boards with small memory.
6. Open source license, free to use. The Shell interactive environment provides convenience for development and testing.
7. The built-in I/O driver supports a large number of microcontroller platforms, such as ESP8266, ESP32, STM32, micro:bit, control board and PyBoard, etc. There is an active community.

MicroPython application scenarios include:
1. Rapidly build prototypes and user interactions for embedded products.
2. Make some small programmable hardware projects.
3. As an educational tool, it helps beginners learn Python and IoT programming.
4. Build smart device firmware to achieve advanced control and cloud connectivity.
5. Various microcontroller applications such as Internet of Things, embedded intelligence, robots, etc.

Things to note when using MicroPython:
1. Memory and Flash space are limited.
2. The explanation and execution efficiency is not as good as C language.
3. Some library functions are different from the standard version.
4. Optimize the syntax for the platform and correct the differences with standard Python.
5. Use memory resources rationally and avoid frequently allocating large memory blocks.
6. Use native code to improve the performance of speed-critical parts.
7. Use abstraction appropriately to encapsulate underlying hardware operations.

Generally speaking, MicroPython brings Python into the field of microcontrollers, which is an important innovation that not only lowers the programming threshold but also provides good hardware control capabilities. It is very suitable for the development of various types of Internet of Things and intelligent hardware.
Insert image description here
OpenMV Cam is a small, low-power microcontroller board that allows you to easily implement applications using machine vision in the real world. You can program OpenMV Cam using high-level Python scripts (provided by MicroPython OS) instead of C/C++. The technical parameters of OpenMV Cam include the following aspects:

1. Processor: OpenMV Cam H7 Plus uses STM32H743II ARM Cortex M7 processor, running at 480 MHz, with 32MB SDRAM + 1MB SRAM and 32 MB external flash memory + 2 MB internal flash memory. OpenMV Cam M4 V2 uses STM32F427VG ARM Cortex M4 processor running at 180 MHz with 256KB RAM and 1 MB flash memory.
2. Image sensor: Both OpenMV Cam H7 Plus and OpenMV Cam M4 V2 use the OV7725 image sensor, which can capture 320x240 8-bit grayscale images or 320x240 16-bit RGB565 images at 75 FPS at resolutions higher than 320x240. Capable of shooting at 150 FPS at 320x240.
3. I/O interface: OpenMV Cam H7 Plus and OpenMV Cam M4 V2 both have the following I/O interfaces:
(1) Full-speed USB (12Mbs) interface, connected to the computer. When the OpenMV Cam is plugged in, a virtual COM port and a "USB flash drive" will appear on your computer.
(2) The μSD card slot is capable of 100Mbs read/write, allowing your OpenMV Cam to record video and extract machine vision material from the μSD card.
(3) The SPI bus operates at a speed of up to 54Mbs, allowing you to simply transmit image stream data to the LCD expansion board, WiFi expansion board, or other controllers.
(4) I2C bus (up to 1Mb/s), CAN bus (up to 1Mb/s) and asynchronous serial bus (TX/RX, up to 7.5Mb/s) for connection with other controllers or sensors.
(5) A 12-bit ADC and a 12-bit DAC.
(6) There are interrupts and PWM on all I/O pins (there are 9 or 10 I/O pins on the board).
4. LED: Both OpenMV Cam H7 Plus and OpenMV Cam M4 V2 are equipped with one RGB LED (tri-color) and two bright 850nm IR LEDs (infrared).
5. Lens: Both OpenMV Cam H7 Plus and OpenMV Cam M4 V2 are equipped with a standard M12 lens interface and a default 2.8 mm lens. If you want to use a more professional lens with your OpenMV Cam, you can easily purchase it and install it yourself.

Insert image description here
MicroPython's OpenMV Cam has an automatic white balance function, which can automatically adjust the white balance of the image by writing MicroPython code.

main feature:

Automatically adjust white balance: OpenMV Cam's automatic white balance function can automatically adjust the white balance of the image according to the ambient light conditions, making the color of the image more realistic and accurate. It automatically detects lighting conditions in a scene and color corrects images accordingly, eliminating color temperature deviations and color distortion.

Real-time: The automatic white balance function can be applied in real-time during image capture and processing, providing instant white balance effects. This enables OpenMV Cam to maintain the accuracy and stability of image color in real-time image processing and analysis application scenarios.

Simplified development: MicroPython is an easy-to-learn programming language suitable for beginners and education. OpenMV Cam provides an intuitive programming interface and sample code, making the development and debugging of the automatic white balance function easier and more convenient.

Application scenarios:

Computer vision applications: The automatic white balance function is of great significance in computer vision applications. For example, in machine vision systems, automatic white balance can improve the accuracy and reliability of images and ensure the stability and consistency of image processing algorithms.

Image acquisition and processing: OpenMV Cam can be used with a variety of sensors for image acquisition and processing. In these applications, the automatic white balance function can help eliminate color shifts caused by changes in lighting conditions, providing more accurate and reliable image data.

Teaching and learning: Automatic white balance is one of the fundamentals in the field of digital image processing and computer vision. Teachers can use the automatic white balance function of OpenMV Cam to conduct teaching and learning activities to help students understand and master the principles and methods of color correction in image processing.

Things to note:

Ambient light conditions: The effect of automatic white balance is affected by ambient light conditions. When using the automatic white balance function of OpenMV Cam, you need to pay attention to changes in ambient light and special situations, such as strong light, shadows, etc., to ensure the accuracy and stability of the white balance effect.

Parameter tuning: OpenMV Cam provides parameter adjustment options for the automatic white balance function, which can be tuned according to specific application scenarios. When using the automatic white balance function, you can adjust relevant parameters according to actual needs and image characteristics to obtain better white balance effects.

Image quality changes: Automatic white balance may affect image contrast, brightness and other parameters. When using the automatic white balance function, you need to evaluate its impact on image quality and make trade-offs and adjustments based on specific needs.

In summary, the automatic white balance function of MicroPython's OpenMV Cam has the characteristics of automatically adjusting white balance, real-time performance and simplified development. It is suitable for scenarios such as computer vision applications, image acquisition and processing, and teaching and learning. When using it, you need to pay attention to factors such as changes in ambient light conditions, parameter tuning, and image quality changes, and use the automatic white balance function flexibly according to specific needs and application scenarios.

Case 1: Using the automatic white balance function of OpenMV Cam

Analysis: In this case, we will use OpenMV Cam’s automatic white balance function to correct the color temperature of the image. First, we need to import the OpenMV Cam module, and then set the camera resolution and frame rate. Next, we will use a loop to continuously capture images and call the corresponding white balance function after each capture to achieve automatic white balance. Finally, we save the captured image to a file.

Code:

import sensor, image, time

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# 捕获图像并应用自动白平衡
while True:
    img = sensor.snapshot()
    result = img.auto_whitebalance()
    if result:
        img.save("image_{}.jpg".format(time.time()))
    time.sleep(1000)  # 延时1秒

Interpretation of key points: In this case, we use the auto_whitebalance() function of OpenMV Cam to achieve automatic white balance. This function adjusts the color of the image based on its color temperature and light source type to make it closer to the color under natural light. If the automatic white balance is successful, the function will return True, otherwise it will return False.

Case 2: Using the manual white balance function of OpenMV Cam

Analysis: In this case, we will use the manual white balance function of OpenMV Cam to manually adjust the color of the image. First, we need to import the OpenMV Cam module, and then set the camera resolution and frame rate. Next, we will use a loop to continuously capture images and call the corresponding white balance function after each capture to achieve manual white balance. Finally, we save the captured image to a file.

Code:

import sensor, image, time

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# 捕获图像并应用手动白平衡
while True:
    img = sensor.snapshot()
    img.auto_whitebalance(mode=1)  # 设置为手动白平衡模式
    img.wb_mode = 1  # 设置白平衡模式为手动
    img.exposure_speed = 1/60  # 设置曝光速度为1/60秒
    img.save("image_{}.jpg".format(time.time()))
    time.sleep(1000)  # 延时1秒

Interpretation of key points: In this case, we use the auto_whitebalance() function of OpenMV Cam to achieve manual white balance. This function accepts a parameter mode. When mode is set to 1, it means that manual white balance mode is enabled. We also set the wb_mode and exposure_speed parameters to adjust the white balance effect and exposure speed.

Case 3: Using the custom white balance function of OpenMV Cam

Analysis: In this case, we will use OpenMV Cam’s custom white balance function to adjust the color of the image according to a specific scene. First, we need to import the OpenMV Cam module, and then set the camera resolution and frame rate. Next, we will use a loop to continuously capture images and call the corresponding white balance function after each capture to implement a custom white balance. Finally, we save the captured image to a file.

Code:

import sensor, image, time

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# 捕获图像并应用自定义白平衡
while True:
    img = sensor.snapshot()
    # 根据场景设置自定义白平衡参数
    img.wb_mode = 1  # 设置白平衡模式为手动
    img.exposure_speed = 1/60  # 设置曝光速度为1/60秒
    img.color_temperature = 3500  # 设置色温为3500K
    img.save("image_{}.jpg".format(time.time()))
    time.sleep(1000)  # 延时1秒

Interpretation of key points: In this case, we use the wb_mode, exposure_speed and color_temperature parameters of OpenMV Cam to achieve custom white balance. By adjusting these parameters, we can optimize the color performance of the image according to specific scenes.

Case 4: Automatic white balance shooting

import sensor, image, pyb  
  
sensor.reset() # 初始化摄像头  
sensor.set_pixformat(sensor.RGB565) # 设置像素格式  
sensor.set_framesize(sensor.QVGA) # 设置帧大小  
sensor.set_auto_whitebal(True) # 开启自动白平衡  
sensor.skip_frames(time = 2000) # 等待摄像头稳定  
  
while(True):  
    img = sensor.snapshot() # 拍摄照片  
    print(img) # 打印照片

Interpretation of key points:
First initialize the camera and set the pixel format and frame size. Use the set_auto_whitebal() function to turn on automatic white balance. In an infinite loop, use the snapshot() function to take photos in real time and print the photos. Through automatic white balance, the colors of the photos taken are more realistic and natural.

Case 5: Comparison between manual white balance adjustment and automatic white balance

import sensor, image, pyb  
  
sensor.reset() # 初始化摄像头  
sensor.set_pixformat(sensor.RGB565) # 设置像素格式  
sensor.set_framesize(sensor.QVGA) # 设置帧大小  
sensor.skip_frames(time = 2000) # 等待摄像头稳定  
  
# 手动调节白平衡  
for i in range(10):  
    img = sensor.snapshot() # 拍摄照片  
    r, g, b = img.get_pixel(img.width()//2, img.height()//2) # 获取照片中心点的RGB值  
    sensor.set_whitebal([r/2, g/2, b/2]) # 设置白平衡参数为照片中心点的RGB值的一半  
    delay(500) # 延时500毫秒,即0.5秒  
  
# 自动白平衡  
sensor.set_auto_whitebal(True) # 开启自动白平衡  
for i in range(10):  
    img = sensor.snapshot() # 拍摄照片  
    print(img) # 打印照片  
    delay(500) # 延时500毫秒,即0.5秒

Interpretation of key points:
First initialize the camera and set the pixel format and frame size. In a loop, manually adjust the white balance. After each photo is taken, obtain the RGB value of the center point of the photo and set the white balance parameter to half of that value. After a delay of 500 milliseconds, take the photo again. After looping 10 times, use the set_auto_whitebal() function to turn on automatic white balance. Loop again 10 times, taking a photo each time and printing it out. By comparing manual white balance adjustment with automatic white balance, you can observe the differences and effects of the two methods.

Case 6: Test the automatic white balance effect in different lighting environments

import sensor, image, pyb, time  
  
sensor.reset() # 初始化摄像头  
sensor.set_pixformat(sensor.RGB565) # 设置像素格式  
sensor.set_framesize(sensor.QVGA) # 设置帧大小  
sensor.set_auto_whitebal(True) # 开启自动白平衡  
sensor.skip_frames(time = 2000) # 等待摄像头稳定  
  
# 在不同光线环境下测试自动白平衡效果  
for i in range(10):  
    img = sensor.snapshot() # 拍摄照片  
    print(img) # 打印照片  
    delay(500) # 延时500毫秒,即0.5秒  
    time.sleep(1) # 延时1秒,等待光线环境变化

Interpretation of key points:
First initialize the camera and set the pixel format and frame size. Use the set_auto_whitebal() function to turn on automatic white balance. In a loop, 10 photos are taken. After each photo is taken, it is printed and delayed by 500 milliseconds. Then delay for 1 second and wait for the light environment to change. By testing the automatic white balance effect in different light environments, you can observe the adaptability and effect of automatic white balance to different light environments.

Case 7: Automatic white balance and display image:

import sensor
import image
import lcd
import time

lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)

while True:
    img = sensor.snapshot()
    img.auto_gain(1)  # 开启自动增益
    img.auto_whitebal(1)  # 开启自动白平衡
    lcd.display(img)

Interpretation of key points:
This program uses the sensor module and lcd module of OpenMV Cam to perform automatic white balance and display images.
Use lcd.init() to initialize the LCD display.
Use sensor.reset() to reset the camera sensor.
Use sensor.set_pixformat(sensor.RGB565) to set the image pixel format to RGB565.
Use sensor.set_framesize(sensor.QVGA) to set the image frame size to QVGA (320x240).
Use sensor.run(1) to start camera image streaming.
In an infinite loop, make the program continue to do the following:
capture the camera image using sensor.snapshot() and store it in the variable img.
Use img.auto_gain(1) to turn on automatic gain to automatically adjust the gain of the image.
Use img.auto_whitebal(1) to turn on automatic white balance to automatically adjust the white balance of the image.
Use lcd.display(img) to display an image on the LCD display.

Case 8: Automatic white balance and save image:

import sensor
import image
import time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)

while True:
    img = sensor.snapshot()
    img.auto_gain(1)  # 开启自动增益
    img.auto_whitebal(1)  # 开启自动白平衡
    img.save("image.jpg")  # 保存图像为image.jpg
    time.sleep(5000)  # 5秒延时

Interpretation of key points:
This program uses the sensor module of OpenMV Cam to perform automatic white balance and save images.
Use sensor.reset() to reset the camera sensor.
Use sensor.set_pixformat(sensor.RGB565) to set the image pixel format to RGB565.
Use sensor.set_framesize(sensor.QVGA) to set the image frame size to QVGA (320x240).
Use sensor.run(1) to start camera image streaming.
In an infinite loop, make the program continue to do the following:
capture the camera image using sensor.snapshot() and store it in the variable img.
Use img.auto_gain(1) to turn on automatic gain to automatically adjust the gain of the image.
Use img.auto_whitebal(1) to turn on automatic white balance to automatically adjust the white balance of the image.
Use img.save("image.jpg") to save the image as a file named "image.jpg".
Use time.sleep(5000) for a 5 second delay.

Case 9: Automatic white balance and output white balance parameters:

import sensor
import image
import time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)

while True:
    img = sensor.snapshot()
    img.auto_gain(1)  # 开启自动增益
    img.auto_whitebal(1)  # 开启自动白平衡
    print(img.get_statistics().wb_stats())  # 输出白平衡参数
    time.sleep(5000)  # 5秒延时

Interpretation of key points:
This program uses the sensor module of OpenMV Cam to perform automatic white balance and output white balance parameters.
Use sensor.reset() to reset the camera sensor.
Use sensor.set_pixformat(sensor.RGB565) to set the image pixel format to RGB565.
Use sensor.set_framesize(sensor.QVGA) to set the image frame size to QVGA (320x240).
Use sensor.run(1) to start camera image streaming.
In an infinite loop, make the program continue to do the following:
capture the camera image using sensor.snapshot() and store it in the variable img.
Use img.auto_gain(1) to turn on automatic gain to automatically adjust the gain of the image.
Use img.auto_whitebal(1) to turn on automatic white balance to automatically adjust the white balance of the image.
Use img.get_statistics().wb_stats() to obtain the white balance parameters of the image, and use the print() function to output to the console.
Use time.sleep(5000) for a 5 second delay.
These sample codes show how to perform automatic white balance operations in MicroPython and OpenMV Cam. You can modify and extend these sample codes according to your own needs and specific application scenarios.

Please note that the above cases are only for expanding ideas and may contain errors or inapplicability. Different hardware platforms, usage scenarios and MicroPython versions may lead to different usage methods. In actual programming, you need to adjust it according to your hardware configuration and specific needs, and conduct multiple actual tests. It is important to ensure that the hardware is connected correctly and to understand the specifications and characteristics of the sensors and devices used.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/133579499