发现比如寻找最大色块例程的视图是存在明显畸变的,所以就将把寻找最大色块的代码与畸变校正代码做了结合,欲实现校正后的画面里寻找到最大色块并较为准确的发送其中心坐标的目的。
代码如下:
# Blob Detection and uart transport
import sensor, image, time
from pyb import UART
import json
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
green_threshold = (25, 100, -69, -10, -72, 127)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
#sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
#sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
#sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
uart = UART(3, 115200)
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
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(strength = 1.7, zoom = 1.0)
# img = sensor.snapshot() # Take a picture and return the image.
print(clock.fps())
blobs = img.find_blobs([green_threshold])
if blobs:
max_blob=find_max(blobs)
print('sum :', len(blobs))
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
output_str="[%d,%d]" % (max_blob.cx(),max_blob.cy()) #方式1
#output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
print('you send:',output_str)
uart.write(output_str+'\r\n')
else:
print('not found!')代码做以上结合以后,畸变视图得以校正,色块也得以识别并打印中心坐标。
但,还是觉得麻烦。。。
问题来了:
【其它算法视图如何无痛痒且方便地置于畸变校正后的图像中?】
@kidswong999 @yuan
什么意思。。。img调用一个lens_corr()方法,哪里麻烦了?
@kidswong999
emmmm......整体而言代码是不多,不过譬如畸变校正参数确定后,这个调用函数以及相关定义函数都可像个模块一样,不用再在新的算法代码当中重新进行删改添加定义什么的,就好比植入头文件了那种感觉。
你这个编程思路我觉得怪怪的
lens_corr是image的一个方法,就1行,想用就用,很优雅简洁啊。。。
@kidswong999 明白了小小智,蟹蟹~
是我犯轴了