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

1 回表示 (過去 30 日間)
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 件のコメント
MAN
MAN 2022 年 6 月 8 日
編集済み: MAN 2022 年 6 月 8 日
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.
Carson Purnell
Carson Purnell 2022 年 6 月 9 日
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.

サインインしてコメントする。

採用された回答

Carson Purnell
Carson Purnell 2022 年 6 月 6 日
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 件のコメント
MAN
MAN 2022 年 6 月 6 日
Thank you, It could be work.
Image Analyst
Image Analyst 2022 年 6 月 6 日
編集済み: Image Analyst 2022 年 6 月 6 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 6 月 6 日
編集済み: Image Analyst 2022 年 6 月 6 日
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

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by