|
基于openmv閾值分割的代碼
圖片1.png (43.67 KB, 下載次數(shù): 61)
下載附件
2019-7-10 15:18 上傳
圖片2.png (42.93 KB, 下載次數(shù): 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()
復(fù)制代碼
下載:
單個(gè)色環(huán)識(shí)別uart.zip
(1.05 KB, 下載次數(shù): 23)
2019-7-10 15:20 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|