Image ROI Determination Manually and Apply the Entire Sequence

5 views (last 30 days)
Hello all,
I have a sequence of images. Number of the images does not matter. All I want to do is opening the first image and determine a rectangular region of interest (both position and size). Later on I wan to close current image and record the position and size values of rectangle region of interest. After that I want to crop whole sequence with respect to determined rectangle.
That part of code in my mind is given below
imshow((storedimages(:,:,5));
ROI=[x,y,x1,y1]; % I need to determine this rectangle on current and opened image.
close
for i=1:size(storedimages)
croppedimages(:,:,i)=imcrop(storedimages(:,:,i),ROI);
end
In short, I want to determine x, y, x1, and y1 by manually on a sample image and crop the entire sequence by using ROI.
Thank you in advance.

Answers (1)

Nitin Kapgate
Nitin Kapgate on 9 Oct 2020
Hi Omer,
You can refer the following code to learn more about interactively selecting a rectangular ROI and using the ROI’s position to crop an image.
% Read and display the image into the workspace.
I = imread('wagon.jpg');
figure('Name', 'Wagon.jpg')
imshow(I);
% Create an ROI in the current axis
roi = images.roi.Rectangle(gca);
% Interactively draw a rectangular ROI
rect = drawrectangle('LineWidth',5,'Color','cyan');
% Get position of the rectangle
rectPosition = floor(rect.Position);
xmin = rectPosition(1)
ymin = rectPosition(2)
width = rectPosition(3)
height = rectPosition(4)
% Crop Image By Specifying Crop Rectangle Position
croppedROIImage = imcrop(I,floor(rect.Position));
% Display the cropped rectangular image
figure('Name', 'Cropped Image created from selected ROI');
imshow(croppedROIImage);
% Write the cropped image
imwrite(croppedROIImage, 'croppedROIImage.jpg');
To crop a series of images, use the above method in a loop.

Community Treasure Hunt

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

Start Hunting!