Embed cropped image within original image using 'drawpoint'

1 view (last 30 days)
Luke G.
Luke G. on 9 Mar 2021
Commented: Luke G. on 30 Mar 2021
I want to display a scaled up user-defined region of interest (ROI) within the original image. The issue:
The code:
clc, clear, close all
% select zoomed in ROI
firstFrame = imread('tiger_test.jpg');
title('Select zoom region:','FontSize',16);
zoom = drawrectangle('Color',[1 1 0]);
zoomRegion = zoom.Position;
close all
% decide where to put it
title('Click where (LH corner) to put it:','FontSize',16);
poop = drawpoint;
x_inset = round(poop.Position(1));
y_inset = round(poop.Position(2));
close all
% test it: overlay the two images
cropped = imcrop(firstFrame,zoomRegion);
newFrame = imresize(cropped,2); % scale up by 1.5x
newFrame_dim = size(newFrame);
x_w = newFrame_dim(1);
y_w = newFrame_dim(2);
firstFrame(x_inset:(x_inset+x_w-1),y_inset:(y_inset+y_w-1),:) = newFrame;

Accepted Answer

Tarunbir Gambhir
Tarunbir Gambhir on 23 Mar 2021
From the code that you have shared, I see the issue with the use of variables "x_inset" and "y_inset". When using these indices to slice the image matrix "firstFrame" at the end, the first index should be for the y-coordinate as for a matrix it refers to the rows. Similarly, the second index should be for the x-coordinate refering the columns.
A simple fix to your code can be the following:
x_inset = round(poop.Position(2));
y_inset = round(poop.Position(1));
Update these lines of your code. If possible you can change the variable name accordingly so it is easier to reference it in the future.
  1 Comment
Luke G.
Luke G. on 30 Mar 2021
Tarunbir, thanks for taking the time to answer. Such a simple mistake! Cheers

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by