【CanMV K230】矩形检测

发布于:2024-09-18 ⋅ 阅读:(553) ⋅ 点赞:(0)


请添加图片描述
本篇内容:

  1. 什么是 矩形检测
  2. 矩形检测应用领域
  3. K230应用(包含相应函数及例程)

B站视频链接:已做成合集 (求去点赞,或者发个弹幕也行呀。C友)
抖音链接:已做成合集(求去点赞,或者发个弹幕也行呀。C友)

什么是 矩形检测

矩形检测是图像处理领域中的一项技术,它涉及到识别和定位图像中的矩形结构。这项技术在机器视觉、自动检测、文档处理等多个领域都有应用。矩形检测的基本步骤通常包括图像预处理、特征提取、轮廓检测和矩形验证。

矩形检测应用领域

矩形检测技术在多个领域都有广泛的应用,以下是一些主要的应用领域:

1.目标检测

在工业生产和安防监控等领域,矩形检测用于自动检测图像中的目标,为目标识别和跟踪提供基础。
在这里插入图片描述

2.自动驾驶

自动驾驶系统中,矩形检测技术用于检测道路线、障碍物、车辆等,以实现车辆与周围环境的实时感知和决策。
在这里插入图片描述

3.医学图像处理

在医学图像分析中,矩形检测技术可以帮助检测病灶、肿瘤等,为医生提供诊断依据。
在这里插入图片描述

4.智能零售

智能零售系统中,矩形检测用于商品识别、定位和分类,支持商品管理和库存控制。
在这里插入图片描述

5.图像识别

霍夫矩形检测技术在车牌识别、文字识别等图像识别任务中用于检测和分析图像中的矩形区域。
在这里插入图片描述

6.计算机视觉

在图像分割、目标定位等计算机视觉任务中,霍夫矩形作为基本特征进行处理和分析。
在这里插入图片描述

K230应用

相关函数

find_rects对象

构造函数

image.find_rects([roi=Auto, threshold=10000])

矩形识别函数。返回一个image.rect矩形对象列表。

参数 说明
roi 识别区域(x,y,w,h),未指定则默认整张图片。
threshold 阈值。返回大于或等于threshold的矩形,调整识别可信度。

官方例程

'''
实验名称:矩形检测
实验平台:01Studio CanMV K230
教程:wiki.01studio.cc
说明:推荐使用320x240以下分辨率,分辨率过大会导致帧率下降。
'''

import time, os, sys

from media.sensor import * #导入sensor模块,使用摄像头相关接口
from media.display import * #导入display模块,使用display相关接口
from media.media import * #导入media模块,使用meida相关接口

try:

    sensor = Sensor(width=1280, height=960) #构建摄像头对象
    sensor.reset() #复位和初始化摄像头
    sensor.set_framesize(width=320, height=240) #设置帧大小为LCD分辨率(800x480),默认通道0
    sensor.set_pixformat(Sensor.RGB565) #设置输出图像格式,默认通道0

    Display.init(Display.ST7701, to_ide=True) #同时使用3.5寸mipi屏和IDE缓冲区显示图像,800x480分辨率
    #Display.init(Display.VIRT, sensor.width(), sensor.height()) #只使用IDE缓冲区显示图像

    MediaManager.init() #初始化media资源管理器

    sensor.run() #启动sensor

    clock = time.clock()

    while True:

        os.exitpoint() #检测IDE中断

        ################
        ## 这里编写代码 ##
        ################
        clock.tick()

        img = sensor.snapshot() #拍摄一张图片

        # `threshold` 需要设置一个比价大的值来过滤掉噪声。
        #这样在图像中检测到边缘亮度较低的矩形。矩形
        #边缘量级越大,对比越强…

        for r in img.find_rects(threshold = 10000):
            img.draw_rectangle(r.rect(), color = (255, 0, 0),thickness=2) #画矩形显示
            for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))#四角画小圆形
            print(r)

        #Display.show_image(img) #显示图片

        #显示图片,仅用于LCD居中方式显示
        Display.show_image(img, x=round((800-sensor.width())/2),y=round((480-sensor.height())/2))

        print(clock.fps()) #打印FPS

###################
# IDE中断释放资源代码
###################
except KeyboardInterrupt as e:
    print("user stop: ", e)
except BaseException as e:
    print(f"Exception {e}")
finally:
    # sensor stop run
    if isinstance(sensor, Sensor):
        sensor.stop()
    # deinit display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # release media buffer
    MediaManager.deinit()

在这里插入图片描述## HDMI屏幕使用矩形检测

'''
实验名称:矩形检测测试
实验平台:01Studio CanMV K230
说明:实现图像在HDMI显示器进行矩形检测
测试人:咸鱼浆 2024年9月6日22:02:15
'''

import time, os, sys

from media.sensor import * #导入sensor模块,使用摄像头相关接口
from media.display import * #导入display模块,使用display相关接口
from media.media import * #导入media模块,使用meida相关接口

try:

    sensor = Sensor(width=1280, height=960) #构建摄像头对象,将摄像头长宽设置为4:3
    sensor.reset() #复位和初始化摄像头
    sensor.set_framesize(width=640, height=480) #设置帧大小为(width=640, height=480)太大了就显示不出来了,默认通道0
    sensor.set_pixformat(Sensor.RGB565) #设置输出图像格式,默认通道0

    #使用IDE缓冲区输出图像,显示尺寸和sensor配置一致。
    Display.init(Display.LT9611, to_ide=True)

    MediaManager.init() #初始化media资源管理器

    sensor.run() #启动sensor


    while True:
        os.exitpoint() #检测IDE中断
        ################
        ## 这里编写代码 ##
        ################
        img = sensor.snapshot() #拍摄一张图
        for r in img.find_rects(threshold = 10000):
            img.draw_rectangle(r.rect(), color = (255, 0, 0),thickness=2) #画矩形显示
            for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))#四角画小圆形
            print(r)
        Display.show_image(img, x=round((1920-sensor.width())/2),y=round((1080-sensor.height())/2))
        #Display.show_image(img) #显示图片

###################
# IDE中断释放资源代码
###################
except KeyboardInterrupt as e:
    print("user stop: ", e)
except BaseException as e:
    print(f"Exception {e}")
finally:
    # sensor stop run
    if isinstance(sensor, Sensor):
        sensor.stop()
    # deinit display
    Display.deinit()
    os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
    time.sleep_ms(100)
    # release media buffer
    MediaManager.deinit()

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到