How to extract different (256,256) images from a (512,512) image and calculate the average of them?
Show older comments
Hi
I have an image (512,512) double. I want to extract different images with size of (256,256) with moving on image with pixel step equal to 16 and finally get an average from all of the extracted images. I think it needs 2 or 3 for loop for moving on the original image(512 and 512) and extract different images with size (256,256). Could you please help me in coding with MATLAB?
9 Comments
Carson Purnell
on 6 Jun 2022
It's a bit unclear what you mean. Are you trying to survey the entire image extent, with a step size of 16 in all directions?
Do you care if it is extensible to other sizes?
MAN
on 6 Jun 2022
DGM
on 6 Jun 2022
Do you actually need the sub images, or do you just need their mean value?
MAN
on 7 Jun 2022
Image Analyst
on 8 Jun 2022
Then you accepted the wrong answer. Look at my answer and see the subimage is there. If you want to save them for after the loop ends, you'll have to put them into a slice of a 3-D array.
Carson Purnell
on 8 Jun 2022
Please explain what you want specifically. Myself and image analyst are assuming different things because you are not providing sufficient information. Do you want the mean of each sample or the mean of the samples?
Carson Purnell
on 9 Jun 2022
If you want the mean of samples my code does exactly that. If you want the samples to each be stored, then just store them in a third dimension within the loop so you can look at them later with
store(:,:,count) = im(i:i+samplesiz-1,j:j+samplesiz-1)
on the inner loop after the count increment.
Accepted Answer
More Answers (1)
Image Analyst
on 6 Jun 2022
Edited: Image Analyst
on 6 Jun 2022
Or you can do it with a nested for loop where you move your window along and compute the mean inside each window.
% Create sample 512 x 512 image.
grayImage = imresize(imread('cameraman.tif'), [512, 512]);
subplot(2, 1, 1);
imshow(grayImage);
title('Original Image')
axis on
% Define parameters.
windowSize = 128; % or 256
stepSize = 16;
[rows, columns] = size(grayImage);
% Define where the windows will be located that we will take the mean inside.
startRows = 1 : stepSize : (rows - windowSize - stepSize)
startCols = 1 : stepSize : (columns - windowSize - stepSize)
outputImage = zeros(length(startRows), length(startCols));
outputRow = 1;
outputCol = 1;
% March the window over the image.
for row = 1 : length(startRows)
thisRow = startRows(row);
for col = 1 : length(startCols)
thisCol = startCols(col);
% Get sub-image.
subImage = grayImage(thisRow:thisRow+windowSize-1, thisCol:thisCol+windowSize - 1);
% Get its mean.
outputImage(outputRow, outputCol) = mean2(subImage);
outputCol = outputCol + 1;
end
% Increment to the next pixel in our output image.
outputCol = 1;
outputRow = outputRow + 1;
end
% Display the output image.
subplot(2, 1, 2);
imshow(outputImage, []);
axis on
title('Output Image')
% Get the overall average of all the subimages
overallAverage = mean2(outputImage)
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
