center crop image based on other image dimension

1 次查看(过去 30 天)
Hi,
I have two images of the same area but with different size: an RGB and TIR format.
I'm drawing a polygon on one of them and convert it to a mask in order to get the same cropped area in the second image.
in QGIS it works: (this is the TIR on top of the RGB)
When I try to do it in MATLAB it fails.
The polygon-
To use the mask in the RGB image I resized the image in the center:
targetSize = size(TIR);
r = centerCropWindow2d(size(RGB),targetSize);
RGB = imcrop(RGB,r);
Now when I try to use the same mask on the RGB image, I get a different area (much closer):
I've also tried to do it the other way, to padd the small image with zeros around it accordingly.
Is it even possible? I want to get the same output as in the QGIS.
Any help is highly appreciated!
  4 个评论
DGM
DGM 2022-5-4
I am really not familiar with mapping toolbox stuff at all, but maybe someone else is. I might be able to bang my head against it for a bit, but I'd really have to have the actual files to be able to have any confidence that I'm actually replicating the issue correctly.
Is trim_tif() a user-defined function?
stav marzuk
stav marzuk 2022-5-4
The mapping toolbox stuff is not a problem, I only need help with cropping the same part from both images (RGB AND TIR).
trim_tif() :
function [hp,Y_upper_pix,Y_lower_pix,X_upper_pix,X_lower_pix,X_upper_map,Y_upper_map,X_lower_map,Y_lower_map]=trim_tif(RGB,tfw)
% trim tif image
h=figure;
mapshow(uint8(RGB),tfw);
xlim(tfw.XLimWorld);ylim(tfw.YLimWorld);
hold on
figure(h);
hp = drawpolygon;
pos_map=hp.Position;
[row_pos_pix,col_pos_pix]=map2pix(tfw,pos_map);
X_upper_pix=round(min(col_pos_pix));
Y_upper_pix=round(min(row_pos_pix));
X_lower_pix=round(max(col_pos_pix));
Y_lower_pix=round(max(row_pos_pix));
[X_upper_map,Y_upper_map]=pix2map(tfw,Y_upper_pix,X_upper_pix);
[X_lower_map,Y_lower_map]=pix2map(tfw,Y_lower_pix,X_lower_pix);
end
The problem is when i upload the images to matlab, their size is really diffrent and the location is not the same. I'm looking for a way to cut an area from one picture and get the same area in the same size from the second one as well.
Here is a link to the images I try to use:
Thanks again :)

请先登录,再进行评论。

回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by