【问题标题】:How to use homography to calculate the object's position on the plane如何使用单应性计算物体在平面上的位置
【发布时间】:2021-10-09 21:55:36
【问题描述】:

我必须做以下工作:

1. Draw a chessboard with at least 9x6 squares.
2. Take a video using a still camera and place the chessboard on the scene to find a Homography.
3. Remove the calibration target from the scene and compute the background model.
4. Place a moving object in the scene on the same plane as the Chessboard. Just a few seconds are enough
5. Calculate a homography using the frame with the chessboard
6. For each frame with the object: a. Subtract the background and calculate the object's position in the image. B. Use a homography and calculate the object's position on the plane where the chessboard was
7. Draw on a graph as found and connect the dots

第6b点是什么意思,怎么做?

我拥有的是这样的:

我说的对吗?还是不是这样:?

更新
我试过这段代码:


chessboard_frame = cv.imread('pen.png',0)          # queryImage
chessboard_template = cv.imread('boardTemplate.png',0) 

pattern_size = (10,10)
_, corners1 = cv.findChessboardCorners(chessboard_frame, pattern_size)
_, corners2 = cv.findChessboardCorners(chessboard_template, pattern_size)
H, _ = cv.findHomography(corners1, corners2)

但它会引发以下错误:

错误:OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-czu11tvl\opencv\modules\calib3d\src\fundam.cpp:378:错误:(- 5:Bad argument) 输入数组应该是函数 'cv::findHomography' 中的 2D 或 3D 点集

【问题讨论】:

  • 查看下面的更新答案

标签: opencv homography


【解决方案1】:

任务有点不清楚。这就是我理解你需要做的事情

  1. 下面的所有帧均应从以某个角度观察平面表面的稳定相机拍摄。
  2. 将棋盘放在平面上的一帧。
  3. 在没有棋盘的平面中取一帧(这是背景帧)。
  4. 在平面顶部拍摄一组移动物体的帧(无棋盘)。
  5. 结果应该是对象相对于给定平面的位置的 XY 图。棋盘实际上定义了平面的 (0,0) 和比例。

您还需要棋盘的模板图像。我建议来自here 的图案大小(9,6)的棋盘。

首先,找到相机到模板的单应性H(假设灰度图像):

pattern_size = (9,6)
_, corners1 = cv.findChessboardCorners(chessboard_frame, pattern_size)
_, corners2 = cv.findChessboardCorners(chessboard_template, pattern_size)
H, _ = cv.findHomography(corners1, corners2)

使用背景帧,从每一帧中减去,只得到对象的蒙版:

object_mask =  np.abs(frame-bg_frame) < threshold

取蒙版的平均值,得到一个代表你的对象的一个​​像素坐标:

uv_coordinates = np.mean(np.argwhere(object_mask), axis=0)

最后,将uv_coordinates 转换为template_coordinates 以便您以后逐帧绘制:

uv1_coordinates = np.append(uv_coordinates,1).reshape(-1,1) # now size 3X1
template_xy1_coordinates = H@uv1_coordinates # matrix multiplication
template_coordinates = template_xy1_coordinates.flatten()[:2] # result shape (2,)

【讨论】:

  • @HelloWorldd 尝试模式大小 (9,9) 并请阅读 findchessboardcorners 文档以查看该代码的返回是否实际有效。我相信你的错误是存在的,但如果没有完整的例子和你的原始图像,我什么都做不了。
  • 此外,我建议您使用不对称板 - 请参阅我的教程:aiismath.com/pages/c_07_camera_calibration/multi_plane_calib_nb
  • 9,9 错误仍然存​​在。 'boardTemplate.png' | 'pen.png' | 'pen3.png'
  • 使用带有笔的电路板图像也是一个问题,因为这会隐藏角落。请再次阅读我对作业的解释。
  • 事实上我没有正确理解要求,但我知道我必须在场景中移动一个对象并显示一个图表,其中包含板和对象之间的点,或者类似的东西
猜你喜欢
  • 2011-10-05
  • 1970-01-01
  • 2017-08-30
  • 2018-04-03
  • 1970-01-01
  • 2013-09-06
  • 2015-10-04
  • 1970-01-01
  • 2012-07-22
相关资源
最近更新 更多