OPENMV识别黑色同时识别矩形

OPENMV+Arduino识别黑色同时识别矩形

Arduino代码

#include <Wire.h>
#define BAUD_RATE 9600
#define CHAR_BUF 128  
char buff[CHAR_BUF] = {0};

int lineType =0;
int led = 53;
void setup()
{  pinMode(led, OUTPUT);  
  Serial.begin(BAUD_RATE);   //波特率9600
  Wire.begin();
  delay(500); // 给OpenMV一个启动的时间
}
void getCode(){   
  String temp1;
  String string = String(buff);
  int postion = string.indexOf(" ");
  temp1 = string.substring(0,postion);
  lineType = temp1.toFloat();
}
void get()
{
  int32_t temp = 0; 
    Wire.requestFrom(0x12, 2);
    if (Wire.available() == 2) 
    {  
      temp = Wire.read() | (Wire.read() << 8);
      delay(1); // Give some setup time... 
      Wire.requestFrom(0x12, temp);
      if (Wire.available() == temp) { // got full message? 
        temp = 0;
        while (Wire.available()) buff[temp++] = Wire.read();  
      } else {
        while (Wire.available()) Wire.read(); // Toss garbage bytes.
      }
    } else {
      while (Wire.available()) Wire.read(); // Toss garbage bytes.
    }
  }
  void ledrun()
  {
     if(lineType==1)
    {
      digitalWrite(led,HIGH); 
      }
      else{
        
        digitalWrite(led,LOW); }
    }
void loop()
{ 
    get();  
    getCode();
ledrun();
    Serial.println("L"+String(lineType)); 
} 

OPENMV代码

1.OPENMV代码

#识别黑色同时识别矩形
#使用II2发送数据
import sensor, image, time
import spi
thresholds = [(6, 47, 121, 6, 93, 6), # generic_red_thresholds
              (0, 63, 18, -74, 57, 20), # generic_green_thresholds
              (23, 69, 89, -12, -7, -63),
              (20, 37, 20, 60, -1, 45),
              (24, 36, -1, 20, -55, -25),
              (30, 44, -46, -9, 7, 44),
              (21, 100, 118, 19, 40, -116)]
ROI=(120,100,40,40)
sensor.reset()                      # 初始化sensor
sensor.set_vflip(True)              #摄像头垂直方向翻转
sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7)
sensor.set_framesize(sensor.QVGA)   # 使用QQVGA的速度
sensor.skip_frames(time = 2000)     # 让新的设置生效。
clock = time.clock()                # 跟踪FPS帧率
while(True):
    clock.tick()                    # 追踪两个 之间经过的毫秒数
    img = sensor.snapshot()         # 拍一张照片并返回图像。
    statistics=img.get_statistics(roi=ROI)
    color_l=statistics.l_mode()
    color_a=statistics.a_mode()
    color_b=statistics.b_mode()
   # if color_l<80:          #识别到白色
       # spi.run(1)          #发送1
       # print(color_l)      #打印出数据
       # print(color_l,color_a,color_b)
       # spi.run(0)          #发送0
       #print("未识别到白色")
       # print(color_l,color_a,color_b)
    for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
        if blob.density()>0.5:#理论上矩形和他的外接矩形应该是完全重合
            img.draw_rectangle(blob.rect(), color = (255, 0, 0))
           # print('长方形长',blob.w(),'宽',blob.h())
            if blob.w()>180 or color_l<80:
                spi.run(1)
                print("识别到黑色",blob.w())
            else :
                spi.run(0)
        img.draw_rectangle(ROI, color = (0, 255, 0))#显示ROI的区域

2.spi代码

# Arduino 作为I2C主设备, OpenMV作为I2C从设备。
# 请把OpenMV和Arduino按照下面连线:
# OpenMV Cam Master I2C Data  (P5) - Arduino Uno Data  (A4)
# OpenMV Cam Master I2C Clock (P4) - Arduino Uno Clock (A5)
# OpenMV Cam Ground                - Arduino Ground

import pyb, ustruct
import ujson
from pyb import Pin, Timer

bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12)
bus.deinit() # 完全关闭设备
bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12)
print("Waiting for Arduino...")

def run(lineType):
    data = str(lineType)+" "
    try:
        #print(data)
        bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits).
        try:
            bus.send(data, timeout=10000) # 然后发送数据
            print("Sent Data!") # 没有遇到错误时,会显示
        except OSError as err:
            pass
    except OSError as err:
        pass

注意

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值