首页 > 解决方案 > 如何检测被阴影遮挡的黄色物体?

问题描述

我尝试检测下图中的黄线,但阴影遮住了黄色道路。我们有什么方法可以解决吗?

在此处输入图像描述

我们可以在这个问题中检测到黄色:About Line detection by using OpenCVHow 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)

标签: pythonopencv

解决方案


这是转换为实验室和自动阈值的代码您必须使用适当的方法检测线条。一种高级解决方案是训练数据集进行分割(神经网络 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()

输出:

在此处输入图像描述


推荐阅读