I'm trying to do a perspective transform on a bitmap that I capture via the camera. The user adjusts a bounding quad (as depicted by the white box) around a rectangular object. I then attempt to transform this to a rectangular image using the code below:
public static Bitmap perspectiveTransformation(Bitmap bitmap,BoundingQuad boundingQuad)
{
Matrix matrix = new Matrix();
float[] dst = new float[] {
0,
0,
bitmap.getWidth(),
0,
bitmap.getWidth(),
bitmap.getHeight(),
0,
bitmap.getHeight()
};
float[] src = new float[] {
boundingQuad.topLeft.x,
boundingQuad.topLeft.y,
boundingQuad.topRight.x,
boundingQuad.topRight.y,
boundingQuad.bottomRight.x,
boundingQuad.bottomRight.y,
boundingQuad.bottomLeft.x,
boundingQuad.bottomLeft.y
};
matrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
However my resulting image contains image data that is outside the boundary of my quad. This would be acceptable if I could figure out what the coordinates of the quad are after transformation so I could crop the result but I have absolutely no idea how to do that.
Any help would be greatly appreciated in either finding the coordinates of the quad after transformation or ideally finding a way to prevent this situation from occurring in the first place.
Input:
https://i.sstatic.net/33RfN.png
Output: