How to use homography to calculate the object's position on the plane

Viewed 479

I have to do the following work:

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

What means the point 6b and how to do it?

What I have is this:

enter image description here

Am I right? or isnt this way:?

UPDATE
I tried this code:


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)

but it throws the following error:

error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-czu11tvl\opencv\modules\calib3d\src\fundam.cpp:378: error: (-5:Bad argument) The input arrays should be 2D or 3D point sets in function 'cv::findHomography'

1 Answers

The assignment is a bit unclear. This is what I understand you need to do

  1. All frames below should be taken from a steady camera viewing a planar surface for some angle.
  2. Take one frame with the chessboard on a planar surface.
  3. Take one frame of the planar surface without chessboard (this is the background frame).
  4. Take a set of frames of a moving object on top of the plane (no chessboard).
  5. The result should be a XY plot of the place of the object relative to the given plane. The chessboard actually defines the (0,0) and scale of the plane.

you will also need a template image of the chessboard. I suggest a chessboard of pattern size (9,6) from here.

First, find the homography H from the camera to the template (assuming gray-level image):

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

Use the background frame and subtract from each frame to get only the mask of the object:

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

Take the mean of the mask to get a one pixel coordinate representing your object:

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

Finally, transform the uv_coordinates to template_coordinates for you to later plot frame by frame:

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,)
Related