【2024 DigiKey 创意大赛】”双光融合“智能热像仪第三部分:触摸屏制作/双光融合
# 【2024 DigiKey 创意大赛】”双光融合“智能热像仪第三部分:触摸屏制作/双光融合## 一、触摸屏制作
### 1.1 绘制原理图和PCB:
### 1.2 触摸屏驱动
将下列代码单独放在openmv弹出的U盘中,命名为`screen.py`。
```python
# 作者:程欢欢的智能控制集
import lcd
from pyb import Pin, SPI, millis, hard_reset
import struct
from sensor import alloc_extra_fb, snapshot, RGB565
from time import sleep
x = 0
y = 0
z = 0
press = False
x_original = 0
y_original = 0
z_original = 0
press_threshold = 2800
# Calibration data
calibration_data = [
29532, 796, -89.8, # x_min, x_max, x_step
1976, 30872, 120.4, # y_min, y_max, y_step
27492.0, 17716, -35.0, 10.8667# z_min, z_max, z_x_step, z_y_step
]
calibration_coordinate = ((80, 60), (240, 60), (80, 180), (240, 180))
baudrate = 80
spi2 = SPI(2, SPI.MASTER, baudrate=40*1000000, phase=1)
CS = Pin('P3', Pin.OUT_PP)
RS = Pin('P8', Pin.OUT_PP)
# Functions for writing to the display
def write_c(c):
CS.low()
RS.low()
spi2.send(c)
CS.high()
def write_d(c):
CS.low()
RS.high()
spi2.send(c)
CS.high()
def write_command(c, *data):
write_c(c)
if data:
for d in data:
write_d(d)
def set_resolution(x, y, w, h):
write_c(0x2a)
write_d(int(x / 256))
write_d(x % 256)
write_d(int((x + w - 1) / 256))
write_d((x + w - 1) % 256)
write_c(0x2b)
write_d(int(y / 256))
write_d(y % 256)
write_d(int((y + h - 1) / 256))
write_d((y + h - 1) % 256)
write_c(0x2C)
def init(screen_baudrate=80, pressure=1800):
global calibration_data, baudrate, press_threshold
press_threshold = 5000 - pressure
lcd.init(width=320, height=240, triple_buffer=False, rgb=True)
write_c(0x36)# Set screen direction
write_d(0xa0)
set_resolution(0, 0, 320, 240)# Set screen resolution
baudrate = screen_baudrate
try:
from screen import calibration
calibration_data = calibration.data
except:
touch_calibration()
def touch_calibration():
# Calibration logic
...
def display(img):
# Touch display logic
...
```
## 二、双光融合的实现
### 主要思路:
对于可见光图像和热图像分别创建两张图片(帧缓冲区),按比例缩放至合适的大小,然后将两个图片按比例相叠加产生融合图像。对于触摸屏,根据照片的尺寸大小和MLX90640传回的原始数组数据计算出映射关系,实现触摸屏返回的像素和温度数据相对应
代码如下:
```python
import sensor
import image
import time
import fir
import gc
from screen import screen
from ulab import numpy as np
# Parameters
kernel_size = 1# kernel width = kernel height = (size*2)+1
kernel = [+1, +1, +1, +1, -9, +1, +1, +1, +1]
thresholds_binary = [(50, 200)]# grayscale thresholds
thresholds_track = (0, 100, -7, 69, 25, 127)
white = (255, 255, 255)
frame_count = 0
gc.collect()
# Initialize camera
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.skip_frames(time=2000)
# Initialize thermal sensor
fir.init(type=fir.FIR_MLX90640, refresh=32)
screen.init()
# Allocate buffer for IR image
ir_buffer = image.Image(fir.width(), fir.height(), sensor.RGB565)
# Scale factors
x_scale = sensor.width() / ir_buffer.width()
y_scale = sensor.height() / ir_buffer.height()
clock = time.clock()
while True:
clock.tick()
img = sensor.snapshot()
if frame_count % 10 == 0:
gc.collect()
ta, ir, to_min, to_max = fir.read_ir()
ir_raw = np.array(ir).reshape((fir.width(), fir.height()))
fir.draw_ir(ir_buffer, ir, alpha=256)
img.draw_image(ir_buffer, 0, 0, x_scale=x_scale, y_scale=y_scale, alpha=128, hint=image.BILINEAR)
if screen.press:# If touch screen is pressed
img.draw_circle(screen.x, screen.y, 5, color=(255, 0, 0))
img.draw_string(screen.x + 5, screen.y - 5, "%0.2f C" % ir[(screen.y // 5 - 1) * 32 + screen.y // 5], color=white, scale=1, mono_space=False)
img.draw_string(2, sensor.height() - 10, "Min: %0.2f C" % to_min, color=white, scale=1, mono_space=False)
img.draw_string(2, sensor.height() - 20, "Max: %0.2f C" % to_max, color=white, scale=1, mono_space=False)
img.draw_string(2, sensor.height() - 30, "FPS: %0.2f " % clock.fps(), color=white, scale=1, mono_space=False)
img.compress(quality=90)
screen.display(img)
if frame_count % 30 == 0:
print(clock.fps())
frame_count += 1
``` <p>看着就像是正常的图像蒙上了一层轻轻的热成像</p>
页:
[1]