필터 지우기
필터 지우기

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

조회 수: 2 (최근 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
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 CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by