python - 如何检测被阴影遮挡的黄色物体?
问题描述
我尝试检测下图中的黄线,但阴影遮住了黄色道路。我们有什么方法可以解决吗?
我们可以在这个问题中检测到黄色:About Line detection by using OpenCV和How to delete the other object from figure by using opencv? .
编码如下:
import cv2
import numpy as np
image = cv2.imread('Road.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
low_yellow = np.array([18, 94, 140])
up_yellow = np.array([48, 255, 255])
mask = cv2.inRange(hsv, low_yellow, up_yellow)
edges = cv2.Canny(mask, 75, 150)
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, maxLineGap=250)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 5)
# cv2.imshow('image', img)
cv2.imwrite("result.jpg", edges)
解决方案
这是转换为实验室和自动阈值的代码您必须使用适当的方法检测线条。一种高级解决方案是训练数据集进行分割(神经网络 Ex:Unet)
import cv2
import numpy as np
img = cv2.imread('YourImagePath.jpg')
cv2.imshow("Original", img)
k = cv2.waitKey(0)
# Convert To lab
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# display b channel
cv2.imshow("Lab", lab[:, :, 2])
k = cv2.waitKey(0)
# auto threshold using Otsu
ret , mask = cv2.threshold(lab[:, :, 2] , 0 , 255 , cv2.THRESH_BINARY+
cv2.THRESH_OTSU)
#display Binary
cv2.imshow("Binary", mask)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
推荐阅读
- django - 在哪里使用 django 在部署和生产中提供文件
- php - 表单的未定义索引和未定义偏移量
- arrays - 无法在 React 中显示 JSON 文件中的数据
- android - 在钥匙串中找不到指定的项目
- java - 如何在 Runnable 对象中使用来自 RecyclerView 适配器的设置器?
- python - 如何在 rdflib 的 sparql 查询中获取令牌位置
- c# - 如何在以下上下文中进行某个 int 变量更改
- ios - 不明白“通话中的额外参数'repeatedValue'”
- c++ - C++ Makefile 错误 - 类链接在主文件中不起作用
- c++ - 读取文本文件时出现分段错误(核心转储)