algorithm - 如何在 360° 视频中找到具有相同颜色的像素的质心?

标签 algorithm math computer-vision 360-degrees

I 是来自 360° 视频流的 w x h 帧。 设 R 是该帧上的一个红色矩形。 R 小于图像的宽度。

要计算这个矩形的质心,我们需要区分两种情况:

  • 情况 1,其中 R 位于边缘
  • 情况 2 R 完全在框架内

Case 1 and 2 illustrated

如您所见,在情况 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/

相关文章:

image - 先进的图像色彩算法

ios - String SDK 的替代品 (http ://string. co)。用什么代替

algorithm - 从一种数字系统转换为另一种数字系统后会有多少位数字

javascript - 如何根据另一个数组的排序方式对一个数组进行排序? (JavaScript)

algorithm - 寻找平截头体的最小包围球

math - 证明 n!对于任何常数自然数 p 不在 O(n^p) 中

java - 用 C 和 Java 解决数字难题

VBA .TopPadding 和点到英寸转换数学问题

image - 展开 MNIST - 弹性变形 MATLAB

python-3.x - 无法从相机OpenCV读取帧