Average of certain pixels in an image

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

K=kron(ones(4,1)/4, speye(64) );
meanBlock=K.'*A*K;
A(1:64,1:64) = meanBlock;

2 Comments

Using this it is giving me some error
Error using .* Sparse integer array arithmetic operations are not supported.
Error in Untitled2 (line 20) meanBlock=K.'.*A*K;
I have uploaded the image in the comment above. can you please observe it?
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

Matt J
Matt J on 17 Apr 2013
Edited: Matt J on 17 Apr 2013
No, I don't think blockproc applies here. It's an inter-block mean calculation.
Oh you're right. I read it too fast. Plus I've never heard of such a need before. I'd like to see an image to understand what the need for this is. If it's an array, like a microarray plate (96 well plate) then I can understand. If it's some kind of noise reduction thing, then I'm wondering if "non-local means" (a well known noise reduction method) would be the better approach.
tarun
tarun on 17 Apr 2013
Edited: tarun on 17 Apr 2013
I m sorry I think I was not able to explain it right. Yes its a noise reduction thing.
this is the image
as you can see each block is similar and I want to take the average of each and replace it with the top left block.
Is each one of those nominally the same image so that only the noise is different, or does the lady have a slightly different pose in each tile? Were they all taken at the same time, or at slightly different times? How was this image constructed in the first place? You have to admit, it's not your garden variety image you get out of a digital camera.
each and every image is same only noise is different. Yes they are taken at the same time. image was constructed using the 'make tiles' option in some photo editor software and then 'zero mean random noise' was added to it.
meanFilter = fspecial('average', [4 4]);
toShow = imfilter(A, meanFilter);
subplot(1,2,1), imshow(A,[])
subplot(1,2,2), imshow(toShow,[])
This is my code so far. I know I m close because it is giving me a clear image but instead of removing noise from all the blocks I want replace the average of all 16 blocks with only the 'top left block'
No, don't do that. That will blur the image. Did Matt's solution work?

Sign in to comment.

Asked:

on 17 Apr 2013

Community Treasure Hunt

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

Start Hunting!