当前位置:   article > 正文

OPENMV识别黑色同时识别矩形_openmv检测矩形

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)); 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

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的区域

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

注意

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/952870
推荐阅读
相关标签
  

闽ICP备14008679号