Skip to content

FindHomography question (some points of the image go in the negative part of the x or y axis) #1087

Description

@MartinStokelj

Hello, this is not an issue, I just need help with WarpPerspective.
After getting homography matrix with FindHomography() and making WarpPerspective() I can see that the output is not a full image (top of the image is missing). How to make offset for warped image?

Example code:

Mat H = getHomography(kpsA, kpsB, featuresA, featuresB, matches, 4);
if (H == null || H.Width == 0 || H.Height == 0)
	throw new Exception("No Homography!!!");

//# Apply panorama correction
double width = trainImg.Width + queryImg.Width;
double height = trainImg.Height + queryImg.Height;

Mat result = new Mat();
Cv2.WarpPerspective(trainImg, result, H, new OpenCvSharp.Size(width, height));

using (Bitmap resultBitmap = BitmapConverter.ToBitmap(result))
	resultBitmap.Save(locationFolder2 + (imageCounter++).ToString().PadLeft(3, '0') + "_trainImg.png", ImageFormat.Png); //5

private Mat getHomography(KeyPoint[] kpsA, KeyPoint[] kpsB, Mat featuresA, Mat featuresB, DMatch[] matches, int reprojThresh)
{
	//# convert the keypoints to numpy arrays
	if (matches.Length > 4)
	{
		List<Point2f> PtA = new List<Point2f>(matches.Length);
		List<Point2f> PtB = new List<Point2f>(matches.Length);
		foreach (var m in matches)
		{
			KeyPoint kpsAI = kpsA[m.QueryIdx];
			KeyPoint kpsBI = kpsB[m.TrainIdx];

			PtA.Add(new Point2f(kpsAI.Pt.X, kpsAI.Pt.Y));
			PtB.Add(new Point2f(kpsBI.Pt.X, kpsBI.Pt.Y));
		}
		InputArray ptsA = InputArray.Create(PtA);
		InputArray ptsB = InputArray.Create(PtB);

		//# estimate the homography between the sets of points
		//step 1: find the homography H with findHomography you will get a classic structure for homography
		Mat H = Cv2.FindHomography(ptsA, ptsB, HomographyMethods.Ransac, reprojThresh);

		Console.WriteLine("Homography:");
		for (var rowIndex = 0; rowIndex < H.Rows; rowIndex++)
		{
			for (var colIndex = 0; colIndex < H.Cols; colIndex++)
			{
				double hVal = H.At<double>(rowIndex, colIndex);
				Console.Write($"{hVal} ");
			}
			Console.WriteLine("");
		}
		/*
			1,00341764165136 -0,0140892817967429 22,6703275561596
			0,00180754198580635 1,00579655474349 -67,6480727682579
			-2,38456747876871E-06 -6,51185532110956E-06 1
		/*

		return H;
	}
	else
		return null;
}

Output:

The output is not a full image (the top of image is missing)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs investigationCause or ownership has not been determined

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions