Why does imwarp returns abrupt uint8 arrays on applying a Projective Transform?

After performing some math on a few images, I obtained Homography matrix as below:
H =
1.0e+05 *
0.0023 0.0018 -4.4192
0.0018 0.0013 -3.3300
0.0000 0.0000 -0.0105
The image that I use is of dimension 612 * 816 * 3. After using the below code, I get the warped image dimension as 2*2*3.
tform = projective2d(H')
J = imwarp(img,tform);
Can someone please help me with this.

8 Comments

Please demonstrate this with the actual input data, img.
Here is a demonstration that does not support what you say:
H= [0.0023 0.0018 -4.4192
0.0018 0.0013 -3.3300
0 0 -0.0105] * 1e5;
out=imwarp(rand(612 , 816 , 3), projective2d(H'));
whos out
Name Size Bytes Class Attributes out 216x284x3 1472256 double
The below shared screenshots seconds my problem description.
Although, the snippet shared by you works for me. But, I wanted to know what goes wrong here. Also, the below commands gives a different answer. Could you please clarify the differences?
K = warp(H, double(refImage)).CData;
size(K)
ans =
612 816 3
A screenshot will not help us. We need you to run your code here in the forum, as I have done above.
It makes no sense to compare this use of warp() to imwarp(). Warp() is for graphical texturemapping. The output K is a copy of refImage. It would be a lot more obvious if the image weren't improperly scaled for its class.
H= [0.0023 0.0018 -4.4192 % this is Z-data
0.0018 0.0013 -3.3300
0 0 -0.0105] * 1e5;
refImage = imread('peppers.png'); % this is a uint8 image
% refImage is mapped onto the surface defined by H
% image is correctly scaled for class 'double', so it actually shows up
K = warp(H, im2double(refImage)).CData;
So we can ignore warp() here. It's not relevant.
EDIT: I went ahead and ran OCR on the screenshot. I kind of had a hunch that full precision was necessary for the issue to arise.
% this is H to full precision, class double
H = [234.40055498025358815539220813662, 176.16693542091721269571280572563, -441919.76452097168657928705215454;
175.87313731614469247688248287886, 133.74617803109279634554695803672, -332998.0214978544972836971282959;
0.55541916052038531326218162575969, 0.41922221571270390505503655731445, -1050.6303467652635390550130978227];
% this is a uint8 RGB image (the class doesn't matter here)
inpict = im2uint8(rand(612,816,3));
% apply the transformation
outpict = imwarp(inpict, projective2d(H'));
size(outpict)
ans = 1×3
2 2 3
... and I was right. As to why that's the case, I'll defer to someone who can intuit their way around spatial transformations.
Hi @Matt J, I am unable to add/ access the image file here in the comments section like @DGM has done. Will sure upload once I figure out that.
@DGM: so when I have to warp an image, I must use the imwarp?
For spatial transformations of images, use imwarp(). For texturemapping of surfaces in a plot, use warp().
Since I've demonstrated that the issue occurs with a randomly generated image, I don't think it's necessary to use your particular image. That said, for future questions, you can attach relevant files using the paperclip button in the editor toolbar.
For this question, the issue lies in H and whether it's appropriate for the task or being handled correctly (I'm going to assume it is). Someone who is more familiar with these transformations should be able to look at H' and tell whether it's expected that the output is downscaled like this. It may not be an issue of scale, but of the outputview being inappropriate.
@DGM, thanks a lot for the response.
The image is of 40MB and the forum isn't accepting that. Anyways, I got answers for what I was seeking. Guess downscaling isn't an issue here with the H I use.
You were right when you said, ". I kind of had a hunch that full precision was necessary for the issue to arise. "
I need that precision for my algo to work properly. When I use that precision I get the answer as 2x2x3.
outImage = imwarp(refImage, projective2d(H'));

Sign in to comment.

Answers (0)

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 25 Dec 2022

Commented:

on 26 Dec 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!