我如何改进我的 opencv 程序以仅检测人行横道?

2023-12-01

我想检测下图中的人行横道,并用红色填充它,但程序也会检测其他东西。这是我的代码:

import cv2
import numpy as np

img = cv2.imread("zebra_lane.jpg")
cv2.imshow("kep" ,img)
imgContour=img.copy()

def getContours(img, imgContour):
    contours, hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

    for cnt in contours:
        area=cv2.contourArea(cnt)
        if area>500:
            cv2.drawContours(imgContour, contours, -1, (0, 0, 255), 2)

            # peri=cv2.arcLength(cnt,True)
            # approx = cv2.approxPolyDP(cnt,0.02*peri,True)
            # # print(len(approx))
            # if len(approx)==4:
            #     x,y,w,h =cv2.boundingRect(approx)
            #     cv2.rectangle(imgContour,(x,y),(x+w,y+h), (0,0,255),1)


imgblur=cv2.GaussianBlur(img,(7,7),1)
imggray=cv2.cvtColor(imgblur,cv2.COLOR_BGR2GRAY)


imgcanny=cv2.Canny(imggray,150,90)
cv2.imshow("kep" ,imgcanny)

kernel=np.ones((1,1))
imgDil = cv2.dilate(imgcanny,kernel,iterations=1)
cv2.imshow("kep" ,imgDil)


getContours(imgDil,imgContour)
cv2.imshow("contour",imgContour)

这是在 Python/OpenCV 中执行此操作的一种方法。

  • 读取输入
  • 白色/灰色人行道条纹上的阈值
  • 应用形态学打开和关闭
  • 获取外部轮廓
  • 过滤区域轮廓并保持良好轮廓
  • 在输入上绘制良好的轮廓
  • 组合轮廓
  • 计算组合轮廓的凸包
  • 在输入上绘制凸包
  • 保存结果

Input:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('walkway.jpg')

# threshold on white/gray sidewalk stripes
lower = (100,130,130)
upper = (180,200,200)
thresh = cv2.inRange(img, lower, upper)


# apply morphology close to fill interior regions in mask
kernel = np.ones((3,3), np.uint8)
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = np.ones((5,5), np.uint8)
morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)

# get contours
cntrs = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

# filter on area
contours = img.copy()
good_contours = []
for c in cntrs:
    area = cv2.contourArea(c)
    if area > 200:
        cv2.drawContours(contours, [c], -1, (0,0,255), 1)
        good_contours.append(c)

# combine good contours
contours_combined = np.vstack(good_contours)

# get convex hull
result = img.copy()
hull = cv2.convexHull(contours_combined)
cv2.polylines(result, [hull], True, (0,0,255), 2)

# write result to disk
cv2.imwrite("walkway_thresh.jpg", thresh)
cv2.imwrite("walkway_morph.jpg", morph)
cv2.imwrite("walkway_contours.jpg", contours)
cv2.imwrite("walkway_result.jpg", result)

# display it
cv2.imshow("THRESH", thresh)
cv2.imshow("MORPH", morph)
cv2.imshow("CONTOURS", contours)
cv2.imshow("RESULT", result)
cv2.waitKey(0)

阈值图像:

enter image description here

形态图像:

enter image description here

轮廓图:

enter image description here

Result:

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我如何改进我的 opencv 程序以仅检测人行横道? 的相关文章

  • 在 Robot 框架中的测试套件中设置会话 cookie

    我的应用程序是一个 RESTful API 仅当会话 cookie 存在时才有效 不幸的是 我总是需要在一个网页登录获取 cookie 并传递会话cookie到 API 来建立会话 我能够找出解决方案来验证会话 cookie 并将其传递给
  • Pandas - 按每个可能的键组合聚合

    我有一个 DataFrame Pandas 我想通过 A B C 和 D 列的组合尽可能按数据进行分组 假设它具有以下形式 A B C D E F G 0 Y X Y Z 1 2 7 1 Y X Y Z 3 4 8 2 X Y U V 1
  • django 管理站点导航侧边栏搞砸了

    我最近在我的项目中添加了一个包并做了一个pip freeze gt requirements txt然后 然后我做了pip install r requirements txt到我的本地 它添加了一个侧边栏 I did a pip inst
  • 属性错误:未找到下层;在 scikit-learn 中使用带有 CountVectorizer 的 Pipeline

    我有一个这样的语料库 X train this is an dummy example in reality this line is very long here is a last text in the training set 和一
  • 从两个字典创建一个新列表

    这是一个关于Python的问题 我有以下字典列表 listA t 1 tid 2 gtm 3 c1 4 id 111 t 3 tid 4 gtm 3 c1 4 c2 5 id 222 t 1 tid 2 gtm 3 c1 4 c2 5 id
  • Python 包?

    好吧 我认为无论我做错了什么 它可能都是显而易见的 但我无法弄清楚 我已经阅读并重新阅读了有关包的教程部分 我唯一能想到的是这不起作用 因为我直接执行它 这是目录设置 eulerproject init py euler1 py euler
  • 使用 theano 进行多处理

    我正在尝试将 theano 与 cpu 多处理和神经网络库 Keras 结合使用 I use device gpu标记并加载 keras 模型 然后 为了提取超过一百万张图像的特征 我使用多处理池 该函数看起来像这样 from keras
  • 在 Django 中上传文件

    我在 Django 1 6 版本 中上传文件时遇到问题 当我尝试做的时候new file data save 在我的views py 中我收到此错误 quiz patent 22 medical record 2 exams 处的属性错误
  • 包含可变数据的正则表达式 - ply.lex

    我正在使用 python 模块ply lex编写一个词法分析器 我用正则表达式指定了一些标记 但现在我卡住了 我有一个list of Keywords谁应该是token data是一个包含大约 1000 个关键字的列表 这些关键字都应该被识
  • 散景服务器获取鼠标位置

    我正在开发一个带有散景 0 12 2 的交互式应用程序 它根据特定的交互更新绘图 现在 我使用滑块来更改图中字形的位置 但实际上我想访问鼠标在特定图中的位置 数据集是一个多维矩阵 张量 密集数据 每个图在特定位置显示一个维度 如果我更改一个
  • Seaborn 条形图条之间没有空格

    我使用下面的代码创建了一个 Seaborn 条形图 它来自https www machinelearningplus com plots top 50 matplotlib visualizations the master plots p
  • Python Pandas:将参数传递给 agg() 中的函数

    我试图通过使用不同类型的函数和参数值来减少 pandas 数据框中的数据 但是 我无法更改聚合函数中的默认参数 这是一个例子 gt gt gt df pd DataFrame x 1 np nan 2 1 y a a b b gt gt g
  • 使用 Python gdata 和 oAuth 2 对日历进行身份验证

    我正在将一个 Python 应用程序从 oAuth 1 迁移到 oAuth 2 该应用程序读取用户的 Google 日历提要 使用 oAuth 1 如果用户可以使用他的 GMail 进行身份验证 我的应用程序将打开浏览器 帐户并授权访问 我
  • 如何检查两个数据集的匹配列之间的相关性?

    如果我们有数据集 import pandas as pd a pd DataFrame A 34 12 78 84 26 B 54 87 35 25 82 C 56 78 0 14 13 D 0 23 72 56 14 E 78 12 31
  • 使用每日频率格式化 x 轴

    我正在尝试获取每日数据图 我有 3 个月的数据 每天都很难指出 如何格式化 x 轴 以便我可以获得每个日期 可以使用以下命令更改主要刻度的频率set major locator mdates DayLocator interval 5 如下
  • Python:多重分配与单独分配速度

    我一直在寻求从我的代码中挤出更多的性能 最近 在浏览时这个 Python 维基页面 https wiki python org moin PythonSpeed 我发现了这个说法 多重分配比单独分配慢 例如 x y a b 比 x a y
  • Django ConnectionAbortedError:[WinError 10053]已建立的连接被主机中的软件中止

    我将 django 与 postgresql 一起使用 每当我尝试保存或删除任何内容时 都会发生此错误 Traceback most recent call last File c program files x86 python35 32
  • 有没有办法只从 python 列表中输出数字?

    简单的问题 list 1 asdada 1 123131 131 blaa adaraerada 0 000001 34 12451235265 stackoverflow is awesome 我想创建一个list 2这样它只包含数字 l
  • 如何在Python中从stdin中逐行读取

    每个人都知道如何在 C 中计算 STDIN 中的字符 但是 当我尝试在 python3 中执行此操作时 我发现这是一个难题 计数器 py import sys chrCounter 0 for line in sys stdin readl
  • Pandas:根据是否为 ​​NaN 来移动列

    我有一个像这样的数据框 phone number 1 clean phone number 2 clean phone number 3 clean NaN NaN 8546987 8316589 8751369 NaN 4569874 N

随机推荐