How to extract different (256,256) images from a (512,512) image and calculate the average of them?

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

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?
yes, I want to move right to the end of image and also move down to the bottom of the image for example. I have to scroll the whole image with constant steps for columns and rows. I need a way to move on the image in rows and columns with a specified amount of step size(16 or 32 for instances). for example:
First image which I need to extract out an image from the original image(512,512) which is:
(0 to 127,0 to 127)=(128,128) pixel size image.
For next images: (0+m to 127+m, 0+n to 127+n) until the end of rows with m and n as step size(16 or 32).
I think m and n must be change in different for loop.
Totally I need to extract out different(128,128) images from my original image(512,512) with a regular steps(16 or 32 pixels move to the right or bottom). Finally, each image has overlapping with previous one clearly.
Do you actually need the sub images, or do you just need their mean value?
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.
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?
Sorry if I did not explain well. I have an image(512,512) double. I need samples in size of 128*128 or 256*256. I just nead to "mean of the samples". I do not need to mean of each sample.
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.

Sign in to comment.

 Accepted Answer

Just loop through the step sizes and accumulate a sum, then divide by the count.
imsiz = 512;
im = rand(imsiz);
step = 16;
samplesiz = 256;
in = zeros(samplesiz); count =0;
for i=1:16:imsiz-samplesiz+1
for j=1:16:imsiz-samplesiz+1
in = in+im(i:i+samplesiz-1,j:j+samplesiz-1);
count = count+1;
end
end
imavg = in/count;

2 Comments

Except it doesn't do what I thought you wanted : get the mean at every window location as the window scans the image in jumps of 16, and then get the overall average of those 625 subimages.
imsiz = 512;
im = imresize(imread('cameraman.tif'), [512, 512]);
subplot(2, 1, 1);
imshow(im)
axis on;
step = 16;
samplesiz = 256;
in = zeros(samplesiz, class(im));
count =0;
for i=1:16:imsiz-samplesiz+1
for j=1:16:imsiz-samplesiz+1
in = in+im(i:i+samplesiz-1,j:j+samplesiz-1);
count = count+1;
end
end
imavg = in/count;
subplot(2, 1, 2);
imshow(imavg, []);
axis on;
See if my answer below is what you want.

Sign in to comment.

More Answers (1)

You can do it with blockproc. Demo attached.
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)
startRows = 1×23
1 17 33 49 65 81 97 113 129 145 161 177 193 209 225 241 257 273 289 305 321 337 353
startCols = 1 : stepSize : (columns - windowSize - stepSize)
startCols = 1×23
1 17 33 49 65 81 97 113 129 145 161 177 193 209 225 241 257 273 289 305 321 337 353
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)
overallAverage = 104.2951

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!