我的任務是編寫一個程序,從我的火箭俱樂部的高度檢測和區分三個「目標」。這些目標是我擁有RGB值的3大油布。Python和OpenCV圖像目標檢測和分化
當我開始這個項目時,我用三個矩形覆蓋了一個GoogleEarth圖像,使用防水布的精確RGB值,我的代碼完美無瑕地工作。但是,當我實際收到油布並開始在地面上拍攝照片時,我的代碼無法識別帶有我規定的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()
我用Python寫的代碼,我有一個良好的金額的經驗,使用我沒有太多經驗的OpenCV庫。任何幫助將不勝感激!
羅莎您好,我用這個代碼,它與我最初發布的圖像的偉大工程。不過,我今天拍攝了更多樣本圖片,紅色和藍色目標未被識別。不同的顏色邊界是否有助於識別這些目標?下面是我試圖分析的圖片之一:http://www.jpeg-optimizer.com/uploaded_images_final/IMG_2654__1486844593_152.7.224.5.jpg – RB1994
理想情況下,您應該瞭解目標顏色,但我認爲如果您要採樣顏色從幾個(非連續)幀,並設置目標顏色的樣本的意思,你會得到合理的結果 –
無論如何,我更新代碼使用CIELab而不是RGB,它應該更好地工作 –