设 I 是来自 360° 视频流的 w x h
帧。
设 R 是该帧上的一个红色矩形。 R 小于图像的宽度。
要计算这个矩形的质心,我们需要区分两种情况:
- 情况 1,其中 R 位于边缘
- 情况 2 R 完全在框架内
如您所见,在情况 1 中使用经典方法计算质心会出现问题。请注意,我只关心水平重叠。
目前我是这样做的。首先我们检测我们找到的第一个点并将其用作引用,然后我们归一化 dx
这是一个点和引用之间的差异,然后我们累加:
width = frame.width
rectangle_pixel = (255,0,0)
first_found_coord = (-1,-1)
centroid = (0,0)
centroid_count = 0
for pixel, coordinates in image:
if(pixel != rectangle_pixel):
continue
if(first_found_coord == (-1,-1)):
first_found_coord = coordinates
centroid = coordinates
continue
dx = coordinates.x - first_found_coord.x
if(dx > width/2):
dx -= width
else if(dx < - width/2):
dx -= width
centroid += (dx, coordinates.y)
centroid_count++
final_centroid = centroid / centroid_count
但它并没有像预期的那样工作。问题出在哪里,有没有更快的解决方案?
最佳答案
这是一个基于过渡点的解决方案,即当您从红色移动到非红色时,或以其他方式移动时。要捕获水平中心,我需要以下信息:
gridSize.x
:矩形可以放置的空间的宽度。
w
:矩形的宽度。
伪代码:
redPixel = (255,0,0);
transitionPoints = [];
betweenTransitionsColor = -1;
// take i and i+1 pixel+position, increment i by one at each step.
for (pixel1, P1), (pixel1, P2) in gridX : // horizontal points for a fixed `y`
if pixel1 != pixel2: // one is red, the other white
nonRedPosition = (pixel1 != redPixel ? P1 : P2)
transitionPoints.append(nonRedPosition)
continue
if(transitionPoints.length == 1 && betweenTransitionsColor == -1):
betweenTransitionsColor = pixel2
if transitionPoints.length == 2:
break
//Case where your rectangle is on the edge (left or right)
if(transitionPoints.length == 1):
if(abs(transitionPoints[0].x - w) < 2):
xCenter = w/2
else:
xCenter = gridSize.x - w/2
else:
[tP1, tP2] = transitionPoints
// case 1 : The rectangle is splitted
if betweenTransitionsColor != redPixel:
xCenter = (tP2.x - gridSize.x + tP1.x)/2
else:
xCenter = (tP1.x + tP1.x)/2
注意:
您必须从可以得到红色像素的 y
位置开始。这应该不是很难实现。如果您的矩形的高度
大于gridSize.y/2
,您可以从gridSize.y/2
开始。否则,您可以搜索第一个红色像素,并将 y 设置为相应的位置。
关于algorithm - 如何在 360° 视频中找到具有相同颜色的像素的质心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51589732/