Average of certain pixels in an image

조회 수: 18 (최근 30일)
tarun
tarun 2013년 4월 17일
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?

채택된 답변

Matt J
Matt J 2013년 4월 17일
K=kron(ones(4,1)/4, speye(64) );
meanBlock=K.'*A*K;
A(1:64,1:64) = meanBlock;
  댓글 수: 2
tarun
tarun 2013년 4월 17일
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?
Matt J
Matt J 2013년 4월 18일
편집: Matt J 2013년 4월 19일
Cast A to double type.
A=double(A);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 4월 17일
편집: Image Analyst 2013년 4월 17일
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
tarun
tarun 2013년 4월 18일
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'
Image Analyst
Image Analyst 2013년 4월 19일
No, don't do that. That will blur the image. Did Matt's solution work?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by