【发布时间】:2017-02-10 00:01:37
【问题描述】:
我的任务是编写一个程序,为我的火箭俱乐部从高度检测和区分三个“目标”。这些目标是 3 个大型防水布,我有 RGB 值。
当我开始这个项目时,我使用防水布的确切 RGB 值覆盖了具有 3 个矩形的 GoogleEarth 图像,并且我的代码完美运行。但是,当我真正收到防水布并开始在地面上拍照时,我的代码无法识别具有我规定的 RGB 颜色边界的防水布。
我尝试将图像转换为 HSV 色彩空间,但无法正常工作。我还考虑过使用轮廓 - 试图让程序识别绑定每个目标的 4 条直线。问题是这些图像将在户外拍摄,因此我无法控制环境照明条件。
有没有人知道什么色彩空间或计算机视觉方法可以让我识别和区分这些目标,而不管室外照明如何?
代码如下:
import cv2
import numpy as np
image = cv2.imread('2000 ft.png', 1)
#hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#cv2.waitKey(0)
cv2.destroyAllWindows()
# define target strings
targ = ['Target 1 - Blue', 'Target 2 - Yellow', 'Target 3 - Red']
i = 0
# BGR boundaries of colors
boundaries = [
# 0, 32, 91
([40, 10, 0], [160, 60, 20]),
# 255, 209, 0
([0, 180, 220], [20, 230, 255]),
# 166, 9, 61
([40, 0, 150], [80, 30, 185]),
]
# colors for rectangle outlines
colors = [
([91, 32, 0]), ([0, 209, 255]), ([61, 9, 166])
]
# # loop over the boundaries
for (lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype = "uint16")
upper = np.array(upper, dtype = "uint16")
# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask = mask)
# frame threshold
frame_threshed = cv2.inRange(image, lower, upper)
imgray = frame_threshed
# iteratively view masks
cv2.imshow('imgray',imgray)
cv2.waitKey(0)
cv2.destroyAllWindows()
ret,thresh = cv2.threshold(frame_threshed,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Find the index of the largest contour
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cont=contours[max_index]
# putting text and outline rectangles on image
x,y,w,h = cv2.boundingRect(cont)
cv2.rectangle(image,(x,y),(x+w,y+h),colors[i],2)
cv2.putText(image, targ[i], (x-50, y-10), cv2.FONT_HERSHEY_PLAIN, 0.85, (0, 255, 0))
# cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),4)
cv2.imshow("Show",image)
cv2.waitKey()
cv2.destroyAllWindows()
i += 1
cv2.destroyAllWindows()
我正在使用我没有太多经验的 OpenCV 库用 Python 编写这段代码,我在这方面经验丰富。任何帮助将不胜感激!
【问题讨论】:
标签: python image opencv color-detection