Can't dewarp image using fitgeotform2d and imwarp

8 vues (au cours des 30 derniers jours)
jcd
jcd le 8 Juil 2024
Commenté : Umar le 8 Juil 2024
I'm trying to reverse a transformation in an image (the image is upside down and os are the points in intrinsic coordinates). I have point coordinates in the intrinsic coordinate (moving points) and their equivalent in the real world coordinates (fixed points). When I use the fitgeotform2d and imwarp functions to obtained a dewarped image, I only get an matrix of zeros and size of the original image as output. Here's the data (calImg.mat) and the code:
load("calImg.mat")
J = double(calImg);
fixedPoints = W;
movingPoints = I;
figure(1)
imagesc(J)
tform = fitgeotform2d(movingPoints,fixedPoints,"projective");
Jregistered = imwarp(flipud(J),tform,OutputView=imref2d(size(J)));
figure(2)
imshowpair(I,Jregistered)
The dots in real world are flipped upside down because the image is upside down. For example, the top-right corner point in the left subplot figure (121) is the bottom-left corner point in the right subplot figure (122).

Réponses (1)

Umar
Umar le 8 Juil 2024
Hi JCD,
When dealing with image transformations and reversals in Matlab, it is crucial to ensure the correct usage of functions and data. In your case, the issue might lie in the transformation process or the way the transformation is applied to the image.
To address the problem of obtaining an output image of zeros, let's delve into potential solutions and optimizations:
Check Data Consistency:
Verify that the calImg.mat file contains the correct image data and is loaded properly into the variable calImg.Ensure that the fixedPoints and movingPoints arrays (W and I respectively) are correctly defined and correspond to the intrinsic and real-world coordinates.
Transformation Function:
The fitgeotform2d function fits a geometric transformation between two sets of control points. Ensure that the transformation type ("projective" in this case) is appropriate for your transformation needs. Double-check the correctness of the transformation by inspecting the tform object after fitting the transformation.
Image Warping:
The imwarp function applies the geometric transformation to the image. Ensure that the transformation is correctly applied to the image J. Verify that the OutputView parameter is correctly specified. In your code snippet, there seems to be a syntax error in specifying OutputView=imref2d(size(J)). The correct syntax should be OutputView, not OutputView=.
Here is a revised version of your code snippet with potential corrections:
load("calImg.mat")
J = double(calImg);
fixedPoints = W;
movingPoints = I;
figure(1)
imagesc(J)
tform = fitgeotrans(movingPoints, fixedPoints, "projective"); % Use fitgeotrans instead of fitgeotform2d
Jregistered = imwarp(J, tform, 'OutputView', imref2d(size(J))); % Correct the syntax for OutputView
figure(2)
imshowpair(I, Jregistered)
By making these adjustments and ensuring the correctness of data and function usage, you should be able to reverse the transformation successfully and obtain a meaningful output image instead of a matrix of zeros. Remember to validate each step and inspect intermediate results for better debugging and understanding.
  2 commentaires
jcd
jcd le 8 Juil 2024
Thanks for your quick response. I tried the modified code but I still get the same black image. I aslo check I have the right image. I attached some more explanation and an image for clarification. I think the issue might also be the teh code has to do two transformations: flip the image upside down and then correct the projective perspective?
Umar
Umar le 8 Juil 2024

Hi JCD,

Let me break down your query and address the potential issues step by step.

1. Loading and Displaying Images:

   load("calImg.mat")
   J = double(calImg);
   fixedPoints = W;
   movingPoints = I;
   figure(1)
   imagesc(J)
   - Here, `calImg.mat` is loaded and stored as `J`. The `fixedPoints` and `movingPoints` define corresponding points in the images `W` and `I` respectively for the transformation.

2. Geometric Transformation Setup:

   tform = fitgeotrans(movingPoints, fixedPoints, "projective");
   Jregistered = imwarp(J, tform, 'OutputView', imref2d(size(J)));
   - `fitgeotrans` is used to compute a projective transformation (`"projective"`) based on the corresponding points.
   - `imwarp` applies the transformation `tform` to image `J` using an `imref2d` object that specifies the output size (`size(J)`).

3. Visualization of Results:

   figure(2)
   imshowpair(I, Jregistered)
   - `imshowpair` is used to display both `I` (presumably the original image) and `Jregistered` (the transformed image) side by side for comparison.

Potential Issues Identified:

- *Image Data Type:* Ensure that `calImg.mat` contains the image data in a format compatible with `imwarp`. Converting to `double` (`J = double(calImg)`) may not always be necessary depending on the original data type.

- Transformation Accuracy:Verify that `fixedPoints` and `movingPoints` accurately correspond to points in `W` and `I`. Incorrect correspondence can lead to failed registration.

- OutputView Specification:Ensure that the `OutputView` parameter in `imwarp` is correctly defined (`imref2d(size(J))`). This specifies the spatial referencing information for the output image.

Revised Approach:

Based on the code snippet provided and the issues identified, make sure to:

- Check the correctness of point correspondences (`fixedPoints` and `movingPoints`).

- Verify the image data type and ensure it matches the requirements of `imwarp`.

- Double-check the `OutputView` parameter to ensure it accurately describes the output image dimensions.

-Also, double check the code you mentioned about two transformations: flip the image upside down and then correct the projective perspective

Hopefully, following these steps will help resolve your problems.

Connectez-vous pour commenter.

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by