标题:
基于openmv的单个色环阈值分割python代码
[打印本页]
作者:
杨9999
时间:
2019-7-10 15:20
标题:
基于openmv的单个色环阈值分割python代码
基于openmv阈值分割的代码
图片1.png
(43.67 KB, 下载次数: 61)
下载附件
2019-7-10 15:18 上传
图片2.png
(42.93 KB, 下载次数: 64)
下载附件
2019-7-10 15:18 上传
import sensor, image, time, lcd, math,ujson, pyb ,json
from pyb import UART
from pyb import LED
obj = [[12,0],[10,12],[22,10],[99,11]] #json
threshold_index = 0 # 0 for red, 1 for green, 2 for blue
objthresholds = [(51, 76, 8, 77, 7, 23), # generic_red_thresholds
(37, 60, -32, 33, 0, 67), # generic_green_thresholds
(37, 60, -5, 33, -60, 67)] # generic_blue_thresholds
graythreshold=[(100,255)]
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
uart = UART(3, 115200)
red_led.off()
green_led.off()
blue_led.off()
def compareBlob(blob1, blob2):
tmp = blob1.pixels() - blob2.pixels()
if tmp == 0:
return 0;
elif tmp > 0:
return 1;
else:
return -1;
A=6
while(True):
img = sensor.snapshot()
#img.lens_corr(1.0)
#accepet
if(uart.any()):
r = uart.readline()
if r == b'R':
A=6
elif r == b'G' :
A=7
elif r == b'B':
A=8
else:
A=0
if(A==6):
img.binary([objthresholds[0]])
img.dilate(2)
blobs=img.find_blobs(graythreshold, pixels_threshold=2025, area_threshold=1600, merge=True)
if len(blobs) == 0:
continue
bigBlob = blobs[0]
for blob in blobs:
if compareBlob(bigBlob, blob) == -1:
bigBlob = blob
img.draw_rectangle(bigBlob.rect())
print(bigBlob.cx(), bigBlob.cy())
#output_str=json.dumps([bigBlob.cx(),bigBlob.cy()])
data_out = 'O'
red_led.on()
green_led.off()
blue_led.off()
uart.write(data_out)
print(data_out)
if(A==7):
img.binary([objthresholds[1]])
img.dilate(4)
blobs=img.find_blobs(graythreshold, pixels_threshold=2025, area_threshold=1600, merge=True)
if len(blobs) == 0:
continue
bigBlob = blobs[0]
for blob in blobs:
if compareBlob(bigBlob, blob) == -1:
bigBlob = blob
img.draw_rectangle(bigBlob.rect())
print(bigBlob.cx(), bigBlob.cy())
#output_str=json.dumps([bigBlob.cx(),bigBlob.cy()])
data_out ='P'
red_led.off()
green_led.on()
blue_led.off()
uart.write(data_out)
print(data_out)
if(A==8):
img.binary([objthresholds[2]])
img.dilate(4)
blobs=img.find_blobs(graythreshold, pixels_threshold=900, area_threshold=900, merge=True)
if len(blobs) == 0:
continue
bigBlob = blobs[0]
for blob in blobs:
if compareBlob(bigBlob, blob) == -1:
bigBlob = blob
img.draw_rectangle(bigBlob.rect())
print(bigBlob.cx(), bigBlob.cy())
data_out ='Q'
red_led.off()
green_led.off()
blue_led.on()
uart.write(data_out)
print(data_out)
if(A==0):
red_led.on()
time.sleep(50)
red_led.off()
复制代码
下载:
单个色环识别uart.zip
(1.05 KB, 下载次数: 23)
2019-7-10 15:20 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
heyangfengyue
时间:
2019-7-13 22:51
深度学习,谢谢楼主!!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1