MATLAB Answers

Average of certain pixels in an image

144 views (last 30 days)
tarun
tarun on 17 Apr 2013
I have an image which is of size 256 X 256. Random noise is distributed all over the image. This image has 16 '64 X 64' similar images(or tiles). What I want to do is
1. Divide the image into 16 '64 X 64' pixel size blocks.
2. Take average of all the blocks. for example adding 1st pixel of the first block with 1st pixel of the second block and so on .....till 16(as number of blocks is 16). and doing this for each and every pixel until we get an average image of 64 X 64 pixels. I used the loop
sum=0;
for i=1:256
for j=1:256
sum=sum+A(i,j)+ A(i+64,j) + A(i+128,j)+ A(i+196,j)+ A(i,j+64)+ A(i+64,j+64)+ A(i+128,j+64)+ A(i+196,j+64) + A(i,j+128)+ A(i+64,j+128) + A(i+128,j+128) + A(i+196,j+128) + A(i,j+196) + A(i+64,j+196) + A(i+128,j+196) +A(i+196,j+196)/16; % as you can see it can go on....(I know I m missing something here)
imshow (sum,[])
3. Next, Place the resulting image in the position of the image in the top-left corner. Leave the remaining 15 images undisturbed for comparison purposes.
Like 'Average filtering'. Can anyone please help?

Accepted Answer

Matt J
Matt J on 17 Apr 2013
K=kron(ones(4,1)/4, speye(64) );
meanBlock=K.'*A*K;
A(1:64,1:64) = meanBlock;
  2 Comments
Matt J
Matt J on 18 Apr 2013
Cast A to double type.
A=double(A);

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 Apr 2013
Edited: Image Analyst on 17 Apr 2013
Use blockproc(). See my well-commented demo:
% Demo code to divide the image up into 16 pixel by 16 pixel blocks
% and replace each pixel in the block by the mean,
% of all the gray levels of the pixels in the block.
%
clc;
clearvars;
close all;
workspace;
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
if ~exist(folder, 'dir')
% If that folder does not exist, don't use a folder
% and hope it can find the image on the search path.
folder = [];
end
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
% Define the function that we will apply to each block.
% First in this demo we will take the median gray value in the block
% and create an equal size block where all pixels have the median value.
% Image will be the same size since we are using ones() and so for each block
% there will be a block of 8 by 8 output pixels.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:));
% Block process the image to replace every pixel in the
% 16 pixel by 16 pixel block by the median of the pixels in the block.
blockSize = [16, 16];
blockyImage = blockproc(single(grayImage), blockSize, meanFilterFunction);
[rows columns] = size(blockyImage);
% Display the block median image.
subplot(1, 2, 2);
imshow(blockyImage, []);
caption = sprintf('Block Mean Image\nInput block size = 16\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
  7 Comments
Image Analyst
Image Analyst on 19 Apr 2013
No, don't do that. That will blur the image. Did Matt's solution work?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!