Homography Source Code


I’m attempting to use some C source code from the following file in the OpenMV Github repo:


To compute the homography between an input camera image and a preferred image perspective on an STM32F746G DISCO.

I’ve extracted the relevant files to successfully compile and perform a homography computation test. (zarray, matd, homography, svd22, fmath, math_util … etc.)

The aim is to find the homography using the following pairs of co-ordinates:

(58,21) → (32,32)
(141,35) → (115,32)
(58,75) → (32,86)
(141,89) → (115,86)

I’ve shown my approach to doing this below:

Declare globally:

matd_t homographyMatrix;
float coord1[] = {58,21,32,32}; // src and dst
float coord2[] = {141,35,115,32}; // src and dst
float coord3[] = {58,75,32,86}; // src and dst
float coord4[] = {141,89,115,86}; // src and dst

In the main:

zarray_t *homographyParameters = zarray_create(sizeof(float[4]));
zarray_add(homographyParameters, &coord1);
zarray_add(homographyParameters, &coord2);
zarray_add(homographyParameters, &coord3);
zarray_add(homographyParameters, &coord4);
homographyMatrix = *homography_compute(homographyParameters,HOMOGRAPHY_COMPUTE_FLAG_SVD);

However the homography matrix I obtain from this is vastly different to the one obtained from using OpenCV in python.

OpenMV Homography Matrix:

M(0,0) = 3.94191378e-034
M(0,1) = 1.08434073e-019
M(0,2) = 0
M(1,0) = -2
M(1,1) = 1.40129846e-045
M(1,2) = 0
M(2,0) = 0
M(2,1) = 0
M(2,2) = 0

OpenCV Homography Matrix:

M(0,0) = 1
M(0,1) = -3.97548050e-18
M(0,2) = -26
M(1,0) = -0.16874699
M(1,1) = 1
M(1,2) = 20.7831325
M(2,0) = 0
M(2,1) = 0
M(2,2) = 1

Furthermore when I use the homography_project() function with the homography calculated by OpenCV the project co-ordinates are as expected e.g. (58,21) maps to (32,32) although this is not the case when I use the homography calculated by OpenMV as the variable ‘zz’ inside homography_project() always seems to be 0 and therefore the program crashes as the ‘xx’ and ‘yy’ values are divided by ‘zz’.

I was hoping you’d possibly be able to shed some light on how the homography_compute() function should be used.

Thanks in advance!


Hi, that code is taken from the AprilTag project. We didn’t actually write it but just ported it.

Question, did you set the points in the homography using the same method AprilTags builds the points? I.e. I would see how they build the matrix and add points to it and follow what they are doing in their code.

Um, so, also, if you want the original source code for that it’s on our GitHub as the AprilTag tracking repo.

Ibrahim wanted it in one file versus being a whole library so that’s why it’s a gaint C file…