MATLAB Answers

how to crop image to 512x512 pixel

18 views (last 30 days)
ctwawa
ctwawa on 11 Sep 2021
Commented: Image Analyst on 29 Sep 2021 at 1:20
i have some image that i want to crop all the image into 512x512 pixel and make it in different spot or location in an image.
how to make each image 20 ROI, each image collect roi not at same spot.
1. Size roi 512x512 pixel
2. Different spot/ location in an image

Accepted Answer

Image Analyst
Image Analyst on 11 Sep 2021
OK, seems you must have encountered some difficulty with my other answer. So here is what I've done for you. Is it what you want?
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename)
rgbImage = imread('peppers.png');
rgbImage = imresize(rgbImage, 2.5); % Make it at least 512 rows
[rows, columns, numberOfColorChannels] = size(rgbImage)
for k = 1 : 20
topRow = ceil(rand * (rows - 512));
leftColumn = ceil(rand * (columns - 512));
croppedImage = rgbImage(topRow : topRow + 511, leftColumn : leftColumn + 511, :);
subplot(5, 4, k);
imshow(croppedImage);
axis('on', 'image');
caption = sprintf('Row=%d, Col=%d', topRow, leftColumn);
title(caption);
drawnow;
end
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename)
  6 Comments
Image Analyst
Image Analyst on 29 Sep 2021 at 1:20
If the image is on disk, you can use imread().

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 11 Sep 2021
How do you want to specify the starting and ending rows and columns? Or do you want to do it interactively (like with ginput)? Basically, once you know, it's
croppedImage = rgbImage(topRow : topRow + 511, leftColumn : leftColumn + 511, :);
so you just need to define topRow and leftColumn.

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!