【发布时间】:2018-06-03 04:59:05
【问题描述】:
我有一个多摄像头设置,但对于这个问题,我将其缩小到两个摄像头。两个摄像头对齐,摄像头 1 的右侧与摄像头 2 的左侧重叠,以确保没有任何区域处于视线之外。 两个相机共享一个共同的参考系统,即:图像 2 中的 x 轴是图像 1 中 x 轴的连续性。
当对象完全包含在一张图像中时,我对于像下图这样的简单场景没有问题:
我的问题:
当对象穿过这个重叠区域时,我不知道如何继续,如下图所示。对于我的应用程序,我需要为每个检测对象返回:中心点 (x,y),高度边界框和边界框的宽度。
我目前的做法:
我正在使用cv2.findContours 和cv2.boundingRect 这两个图像。如果对象接触图像的任一侧,我会根据矩形的大小和图像的大小进行计算。如果他们不这样做,这很容易,但如果他们这样做了,那我现在就被困住了。我不知道如何在两个图像的值之间进行权衡并从两个轮廓创建一个唯一的 (x,y,w,h) 点。
有我当前的代码:
import numpy as np
import cv2
img1 = cv2.imread('image_from_cam1.png')
img2 = cv2.imread('image_from_cam2.png')
gray1= cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2= cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
img1_width = img1.shape[1]
img2_width = img2.shape[1]
#Thresholds on both images then find the contours
ret1, thresh1 = cv2.threshold(gray1, 127, 255, 1)
ret2, thresh2 = cv2.threshold(gray2, 127, 255, 1)
cnts1, _ = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts2, _ = cv2.findContours(thresh2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
result = [] #result list that will contain [(x1,y1,w1,h1), ...]
overlap = False #is the object overlapping across several images
l1 = [] #storage list for sub-results in image1
l2 = [] #storage list for sub-results in image2
#loop over contours of image1 to draw rectangles and check if they touch either left of right border
for cnt in cnts1:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img1, (x,y), (x+w, y+h), (0,255,0), 2)
#Test if the bounding box touches the border of the screen
if x<2 or (x+w)>(img1_width - 2):
overlap = True
l1.append((x,y,w,h))
else:
#the object is fully contained in image1
result.append((x,y,w,h))
#loop over contours of image2 to draw rectangles and check if they touch either left of right border
for cnt in cnts2:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img2, (x,y), (x+w, y+h), (0,255,0), 2)
#Test if the bounding box touches the border of the screen
if x<2 or (x+w)>(img2_width - 2):
overlap = True
l2.append((x,y,w,h))
else:
#the object is fully contained in image2
result.append((x,y,w,h))
cv2.imshow('cam1',img1)
cv2.imshow('cam2',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
所以同一个对象会给我 2 组不同的 (x,y,w,h) 和 cv2.boundingRect 在两个图像中。如何获得对象的“真实”(x,y,w,h)?或者从我的代码:如果对象重叠,我如何从l1 和l2 创建result?
【问题讨论】:
-
你能展示实际的图片吗?
-
如果你的相机是固定的,那么你可以计算相机之间的变换矩阵,然后将它们拼接在一起,这样就可以得到一个完整的图像。
-
是的,我的相机已经修好了。如果我用这种方法 @Silencer 将它们缝合在一起,得到的整个图像会包含重叠区域吗?
标签: python python-2.7 opencv computer-vision