Warp an image through a homography H that maps source pixel coordinates to output pixel coordinates, using inverse mapping.
Coordinates: pixel (x,y) is column x, row y, with integer coordinates at pixel centres. The source image img has shape (h, w) (grayscale) or (h, w, C).
Implement warp_perspective(img, H, out_shape) returning (out, mask), where out_shape = (oh, ow):
- For every output pixel (x,y), find its source location (xs,ys) by applying H−1 to (x,y,1) and dividing by the third coordinate.
- The pixel is valid when −10−6≤xs≤w−1+10−6 and −10−6≤ys≤h−1+10−6. Invalid pixels get value 0 (in every channel) and
mask false.
- For valid pixels, clip xs to [0,w−1] and ys to [0,h−1], then sample bilinearly: x0=⌊xs⌋, x1=min(x0+1,w−1), a=xs−x0 (same for y with b), and
out=(1−a)(1−b)I[y0,x0]+a(1−b)I[y0,x1]+(1−a)bI[y1,x0]+abI[y1,x1]
per channel.
Return out as a float array of shape (oh, ow) or (oh, ow, C) and mask as a boolean (oh, ow) array. Test homographies never send an output pixel to infinity.