Find homography in D?

Ferhat Kurtulmuş aferust at gmail.com
Tue Apr 30 13:26:31 UTC 2024


On Sunday, 21 April 2024 at 14:57:33 UTC, Paolo Invernizzi wrote:
> Hi,
>
> Someone can point me to a D implementation of the classical 
> OpenCV find homography matrix?
>
> Thank you,
> Paolo

Just for future records in the forum.

// 
https://math.stackexchange.com/questions/3509039/calculate-homography-with-and-without-svd

/+dub.sdl:
dependency "lubeck" version="~>1.5.4"
+/
import std;
import mir.ndslice;
import kaleidic.lubeck;

     void main()
     {
         double[2] x_1 = [93,-7];
         double[2] y_1 = [63,0];
         double[2] x_2 = [293,3];
         double[2] y_2 = [868,-6];
         double[2] x_3 = [1207,7];
         double[2] y_3 = [998,-4];
         double[2] x_4 = [1218,3];
         double[2] y_4 = [309,2];

         auto A = [
             -x_1[0], -y_1[0], -1, 0, 0, 0, x_1[0]*x_1[1], 
y_1[0]*x_1[1], x_1[1],
             0, 0, 0, -x_1[0], -y_1[0], -1, x_1[0]*y_1[1], 
y_1[0]*y_1[1], y_1[1],
             -x_2[0], -y_2[0], -1, 0, 0, 0, x_2[0]*x_2[1], 
y_2[0]*x_2[1], x_2[1],
             0, 0, 0, -x_2[0], -y_2[0], -1, x_2[0]*y_2[1], 
y_2[0]*y_2[1], y_2[1],
             -x_3[0], -y_3[0], -1, 0, 0, 0, x_3[0]*x_3[1], 
y_3[0]*x_3[1], x_3[1],
             0, 0, 0, -x_3[0], -y_3[0], -1, x_3[0]*y_3[1], 
y_3[0]*y_3[1], y_3[1],
             -x_4[0], -y_4[0], -1, 0, 0, 0, x_4[0]*x_4[1], 
y_4[0]*x_4[1], x_4[1],
             0, 0, 0, -x_4[0], -y_4[0], -1, x_4[0]*y_4[1], 
y_4[0]*y_4[1], y_4[1]
         ].sliced(8, 9);

         auto svdResult = svd(A);

         auto homography = svdResult.vt[$-1].sliced(3, 3);
     	auto transformedPoint = homography.mtimes([1679,  128, 
1].sliced.as!double.slice);
         transformedPoint[] /= transformedPoint[2];

         writeln(transformedPoint); //[4, 7, 1]
     }


More information about the Digitalmars-d-learn mailing list