如果没有OpenMV配套LCD屏,而手边恰好有TFT屏,那怎么能用TFT平替一下呢,今天就把它当作一个独立的OpenMV扩展板给各位父老乡亲们享用(不占用UART端口),屏幕本身所占引脚比OpenMV官网模块还少,而且成本就屏幕本身20块钱。
下面就用追踪黄色小球举例
IO接线:
LED ---> 3.3V (背光控制)
SCK ---> P2(SCLK) SCLK 时钟线
SDA ---> P0(MOSI) SDA 数据线
AO ---> P8 数据命令选择线
RESET ---> P7 复位线
CS ---> P3 片选线
GND ---> GND
VCC ---> 3.3V
#OpenMV连接TFT追踪黄色小球
import sensor, image, time, math, json, pyb, ustruct ,lcd
from pyb import UART
from pyb import Timer
from machine import UART
# 颜色跟踪阈值(L Min, L Max, A Min, A Max, B Min, B Max)
thresholds1 =[
(44, 60, 23, 52, 73, 51)
]
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_contrast(0)
sensor.set_auto_gain(False) # 颜色跟踪必须关闭自动增益
sensor.set_auto_whitebal(False) # 颜色跟踪必须关闭白平衡
sensor.set_auto_exposure(True,1)
clock = time.clock()
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
lcd.init() # Initialize the lcd screen.
left_roi = [10,10,140,100] ##感性区域,设计颜色识别的有效区域-
start_x=80
start_y=120
interval_x=80
interval_y=80
roi_temp = 20
roi_sum = 0
roi_r=12
roi_x=roi_r
roi_y=roi_r
left_1_1 = (start_x, start_y, roi_r) ##通过比例计算框位
left_1_2 = (start_x+interval_x+(roi_x*2), start_y, roi_r)
ROI=(75,55,10,10)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob = blob
max_size = blob.pixels()
return max_blob
time_log=5
def tick(timer): #这里开启了一个定时器
time_log = 1
tim = Timer(2, freq=1)
tim.callback(tick)
Lmin = 1
Lmax = 1
Amin = 1
Amax = 1
Bmin = 1
Bmax = 1
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.0)
lcd.display(img) # Take a picture and display the image.
statistics=img.get_statistics(roi=ROI) ###查询这个区域的颜色值
color_l=statistics.l_mean()
color_a=statistics.a_mean()
color_b=statistics.b_mean()
#print(color_l,color_a,color_b)
img.draw_rectangle(left_roi) ##绘制主框
img.draw_rectangle(ROI,color=(255,255,100)) ##绘制校准区域
Lmin = color_l - 10
Lmax = color_l + 10
Amin = color_a - 10
Amax = color_a + 10
Bmin = color_b - 10
Bmax = color_b + 10
blobs = img.find_blobs(thresholds1,roi=left_roi,pixels_threshold=50, area_threshold=50,merge = True)
if blobs:
max_blob1 = find_max(blobs)
img.draw_edges(max_blob1.min_corners(), color=(0,255,128))
img.draw_cross(max_blob1[5], max_blob1[6],color=(0,255,128))
data = ustruct.pack("
注意1.8寸TFT屏分辨率为160*128,所以选择图像为QQVGA或QQVGA2。 sensor.set_framesize(sensor.QQVGA)
保存完运行,得到如下结果。