计算机视觉图像识别16.1-停车位识别的图像预处理

本章节完成停车场监控视频中停车位实时情况的动态识别。

首先,来做图像的预处理。

下面的两张图片是从一段停车场监控中截取的视频截图:

首先,我们来定义函数select_rgb_white_yellow,用于从函数中提取白色和黄色区域,并把处理后的图像进行返回。

设置颜色范围:lower 表示最低 RGB 值 120,120,120,upper 表示最高 RGB 值 255,255,255

使用 cv2.inRange 创建一个白色掩码 white_mask,筛选出图像中在指定 RGB 范围内的像素。

调用 self.cv_show 显示白色掩码图像。

使用 cv2.bitwise_and 将原始图像与白色掩码结合,生成仅包含白色区域的图像 masked。

调用 self.cv_show 显示处理后的图像。
登录后复制

plain 复制代码
def select_rgb_white_yellow(self,image):
        lower = np.array([120,120,120])
        upper = np.array([255,255,255])
        white_mask = cv2.inRange(image,lower,upper)
        self.cv_show('white_mask',white_mask)

        masked = cv2.bitwise_and(image,image,mask=white_mask)
        self.cv_show('masked',masked)
        return masked

然后进行停车场图像的轮廓提取。
登录后复制

plain 复制代码
def convert_gray_scale(self,image):
        return cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

    def detect_edges(self,image,low_threshold=50,high_threshold=200):
        return cv2.Canny(image,low_threshold,high_threshold)

通过以下方式调用:
登录后复制

plain 复制代码
gray_images = list(map(park.convert_gray_scale,white_yellow_images))
    park.show_img(gray_images)

    edge_images = list(map(lambda image:park.detect_edges(image),gray_images))
    park.show_img(edge_images)

接下来,把关注重心放在停车场区域,无关紧要的范围进行删除。

大致标定停车场轮廓角点:
登录后复制

plain 复制代码
def select_region(self, image):
        # first, define the polygon by vertices
        rows, cols = image.shape[:2]
        pt_1 = [cols * 0.05, rows * 0.90]
        pt_2 = [cols * 0.05, rows * 0.70]
        pt_3 = [cols * 0.30, rows * 0.55]
        pt_4 = [cols * 0.6, rows * 0.15]
        pt_5 = [cols * 0.90, rows * 0.15]
        pt_6 = [cols * 0.90, rows * 0.90]

        vertices = np.array([[pt_1, pt_2, pt_3, pt_4, pt_5, pt_6]], dtype=np.int32)
        point_img = image.copy()
        point_img = cv2.cvtColor(point_img, cv2.COLOR_GRAY2RGB)
        for point in vertices[0]:
            cv2.circle(point_img, (point[0], point[1]), 10, (0, 0, 255), 4)
        self.cv_show('point_img', point_img)

        return self.filter_region(image, vertices)

把无关区域剪除掉:
登录后复制

plain 复制代码
def filter_region(self, image, vertices):
        mask = np.zeros_like(image)
        if len(mask.shape) ==2:
            cv2.fillPoly(mask, vertices, 255)
            self.cv_show('mask', mask)
        return cv2.bitwise_and(image, mask)

调用方法:
登录后复制

plain 复制代码
roi_images = list(map(park.select_region,edge_images))
park.show_img(roi_images)

调用结果:

相关推荐
jay神14 分钟前
深度学习确定baseline之后怎么做改进?
人工智能·深度学习·yolo·计算机视觉·分类
acrel718 分钟前
安科瑞Acrel-1000变电站综合自动化系统在合肥某新材料公司35kV综合自动化项目中的应用分析
大数据·人工智能
UWA25 分钟前
隐藏视频变“常驻刺客”,VideoPlayer与“N/A”纹理该怎么查
人工智能·性能优化·音视频·memory·cpu·游戏开发
V哥AI增长28 分钟前
小红书笔记在生成式AI引擎中的可见性机制解析:从抓取原理到GEO工程化实践
大数据·人工智能·笔记
ajassi20001 小时前
AI语音智能体开发日记(十二)GX8006 固件定制指南——从双唤醒词到 UART 音频传输
人工智能·ai·ai编程
大模型momo1 小时前
告别单体 Agent:多智能体设计模式(Multi-Agent Patterns)深度实战手册
人工智能·agent·multi-agent·多agent
DisonTangor1 小时前
【SeeDream开源平替】MiniMax H3 重磅开源:全模态视频生成新标杆,2K 画质 + 原生立体声,15 秒大片一键生成!
人工智能·ai作画·开源·aigc·音视频
很楠爱上1 小时前
(附上相关学习资源)适合新手做的第一个agent项目*ovo*Dify Agent 全栈实战:从智能选车顾问到新能源汽车之家的端到端开发
人工智能·汽车·agent·项目·开发笔记
IT智慧客07311 小时前
2026年7月前端面试高频考点与趋势分析(面20个前端后的总结)
人工智能
不瘦80斤不改名2 小时前
01-vibe-coding-起源与本质
人工智能·python