[Image Registration] How to crop and save 2 images separately after registering them?

6 visualizzazioni (ultimi 30 giorni)
Hello everyone,
I was wondering how I can crop and save some images that I am registering together after I have completed the appropriate registration.
My code has the user click 8 well-defined points on two images, and then these points are passed (along with the 2 images) to estimateGeometricTransform for image registration:
close all
showMatchedFeatures(originalGray,moving,pts_original,pts_moving);
uiwait(warndlg('Press OK to begin image registration.',...
'locFinder Directions'));
After showMatchedFeatures, this is displayed:
The code continues running:
% Find a transformation corresponding to the matching point pairs.
[tform, ~, ~] = estimateGeometricTransform(...
pts_moving, pts_original, 'similarity');
% Align the images
outputView = imref2d(size(originalGray));
moving_warp = imwarp(moving,tform,'OutputView',outputView);
figure, imshowpair(original,moving_warp,'blend')
After running this, I see a somewhat satisfactory registration:
So my question is, how can I take the above images (note that there are 2 distinct images being displayed by imshowpair, which are "original" and "moving_warp") and crop a region which is inside of both regions? I would like to have the crop be as tight to both of the regions as possible so that I don't lose too much information in cropping. The end result would be saved something like these two images I registered and saved manually in photoshop:
&
So how would this cropping step be done? If needed, I could provide more sections of my code and the raw images that I am using in my code.
THANK YOU! :)

Risposte (1)

Shiraz SO Kaderuppan
Shiraz SO Kaderuppan il 21 Giu 2020
Hi,
To do this, you will need to use imcrop on your registered image to bring up the cropping window where you can then drag & select the region of the said image to be cropped. So, in this context, the code will read as follows:
moving_warp_crop = imcrop(moving_warp);
After selecting the region to be cropped, right-click in the cropping rectangle & choose Copy Position (you will need this for defining the coordinates and dimensions of the cropping box to be used for cropping the 2nd image). The position will be copied as a 4-parameter argument in square parentheses, i.e. [xmin ymin width height] (see https://www.mathworks.com/help/images/ref/imcrop.html for details). Then, right-click in the cropping rectangle again, and choose Crop Image. Close the cropping window to return to the main MATLAB window.
Now, you will enter the following code:
original_crop = imcrop(original,[coordinates here]);
Notice that in the square parentheses (which I indicated as coordinates here), you will need to paste the coordinates you copied earlier (when you used Copy Position). This will define the region in the original image which matches the cropping rectangle that you defined earlier in moving_warp. However, in the release version of MATLAB which I am currently using (R2019b), there is an inherent image parsing anomaly, hence you will need to subtract 1 pixel from each of the width and height (i.e. the 3rd & 4th arguments in the square parentheses) to ensure that the cropped original image has the same dimensions as the cropped moving_warp image.
Now, you have the cropped regions of moving_warp & original stored in each of the variables moving_warp_crop & original_crop respectively. If you wish, you can output each of these cropped images as separate files, using imwrite.
  1 Commento
Fryderyk Kögl
Fryderyk Kögl il 4 Lug 2021
Hi, thanks for the answer. But I have two questions: 1. can you do this programmatically? 2. how would you do this for more than 2 images, so you can find the common part of all of them

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by