center crop image based on other image dimension

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 Comments

It would help to know how you're registering your images. An example of the code and images would make it clear. I could concoct some simple demo, but if you're using imref2d() or something, it wouldn't help you.
Thank you so much for the response.
I'm not using imref2d(), here is my code:
%----------------tir image
TIR=imread("path.tif");
tfw_file=strcat("pathR.tfw");
tfw=worldfileread(tfw_file,'planar',size(TIR));
TIR= double(TIR);
TIR(find(TIR<0.1))=0;
TIR(find(TIR>70))=0;
%----------------rgb image
RGB=imread("Path.tif");
tfw_file_RGB=strcat("path.tfw");
tfw_RGB=worldfileread(tfw_file_RGB,'planar',size(RGB));
RGB= double(RGB(:,:,2));
% --------- draw polygon and make mask
[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(TIR,tfw);
BW=createMask(hp);
% ------------ trim rgb and tir and tfw approximatly
trim_tfw =worldfileread([tif_PathName '.tfw']);
trim_tfw(3,1)=X_upper_map;
trim_tfw(3,2)=Y_upper_map;
tif_mask=TIR.*BW;
tif_mask=tif_mask(Y_upper_pix:Y_lower_pix,X_upper_pix:X_lower_pix,:);
tif_mask_RGB=RGB.*BW;
tif_mask_RGB=tif_mask_RGB(Y_upper_pix:Y_lower_pix,X_upper_pix:X_lower_pix,:);
if there is anything more I can provide, let me know and thanks again!!
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?
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 :)

Sign in to comment.

Answers (0)

Products

Release

R2021a

Asked:

on 2 May 2022

Commented:

on 4 May 2022

Community Treasure Hunt

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

Start Hunting!