finding mean for values

I have values named in variabla Block in the my values are as
val(:,:,1)
8x8 matrix values
val(:,:,2)
;
;
;
val(:,:,3)
8x8 matrix values
for these all i have to find mean,for val(:,:,1) to vali(:,:,3)
please assist me

1 comentario

Jan
Jan el 29 de Nov. de 2012
Please, FIR, explain the input data in valid Matlab syntax. I cannot guess what "val(:,:,1) 8x8 matrix values" and the three semicolons exactly mean. We try to help you, but it would be much easier if you care about clear questions also.

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 28 de Nov. de 2012
Editada: Jan el 28 de Nov. de 2012

0 votos

Please read the documentation of mean():
help mean
doc mean
It can be solved in one single line. Either a reshape is helful, or remember that the mean value of a set of variables is the mean value of the mean values of subsets, when they all have the same size.

5 comentarios

FIR
FIR el 28 de Nov. de 2012
JAN i know to apply mean ,but how to for each subimage
my code
x=imread('peppers.png');
y=double(x);
[r c]=size(y);
bs=8; %size of each block
nob=(r/bs)*(c/bs);%number of blocks
kk=0;
for i=1:(r/bs)
for j=1:(c/bs)
Block(:,:,kk+j)=y((bs*(i-1)+1:bs*(i-1)+bs),(bs*(j-1)+1:bs*(j-1)+bs));
end
end
for the variable Block i have to apply mean ,please help
Jan
Jan el 28 de Nov. de 2012
@FIR: What should I do with the posted code? What does it explain?
If you want to apply the mean() to the variable Block, what about:
mean(Block)
As explained in the documentation, this calculates the mean over the first non-singelton dimension. Of course you can defined the dimension to operate on manually also.
What will happen for mean(mean(B)) ?
I assume, that your creation of "Block" can be performed by a reshape also in one line.
FIR
FIR el 28 de Nov. de 2012
Jan it gives answer but if i try to do 8x8 matrix to 8x1 if i do Block(:) it gives matrix for whole block,i need for each block
Jan
Jan el 28 de Nov. de 2012
Editada: Jan el 28 de Nov. de 2012
Did you read the documentation of mean already? If so, read it again. Look for the method to define the dimension to operate on.
In the original question it seems like you have a [8 x 8 x N] array and want to get the means as an [1 x N] vector. Then the command I've posted already does this: mean(mean(B)).
Image Analyst
Image Analyst el 28 de Nov. de 2012
He talks about "each block" so I'm guessing that he's processing a much larger array in "chunks" of 8 by 8. So he processes one block, then "jumps" over to the adjacent block and processes that, and so on for each block. Thus I thought blockproc() would be perfect for that. But he doesn't seem to know how to extract a plane from the N dimensional array, like
plane1 = val(:, :, 1);
plane2 = val(:, :, 2);
and so on.

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 28 de Nov. de 2012

0 votos

Use blockproc(). Here's three grayscale examples that you can adapt to color very easily.
% Demo code to divide the image up into 16 pixel by 16 pixel blocks
% and replace each pixel in the block by the median, mean, or standard
% deviation 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(2, 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.
medianFilterFunction = @(theBlockStructure) median(theBlockStructure.data(:)) * ones(size(theBlockStructure.data), class(theBlockStructure.data));
% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by the median of the pixels in the block.
blockSize = [8 8];
% Quirk: must cast grayImage to single or double for it to work with median().
% blockyImage8 = blockproc(grayImage, blockSize, medianFilterFunction); % Doesn't work.
blockyImage8 = blockproc(single(grayImage), blockSize, medianFilterFunction); % Works.
[rows columns] = size(blockyImage8);
% Display the block median image.
subplot(2, 2, 2);
imshow(blockyImage8, []);
caption = sprintf('Block Median Image\n32 blocks. Input block size = 8, output block size = 8\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
% Block process the image to replace every pixel in the
% 4 pixel by 4 pixel block by the mean of the pixels in the block.
% The image is 256 pixels across which will give 256/4 = 64 blocks.
% Note that the size of the output block (2 by 2) does not need to be the size of the input block!
% Image will be the 128 x 128 since we are using ones(2, 2) and so for each of the 64 blocks across
% there will be a block of 2 by 2 output pixels, giving an output size of 64*2 = 128.
% We will still have 64 blocks across but each block will only be 2 output pixels across,
% even though we moved in steps of 4 pixels across the input image.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)) * ones(2,2, class(theBlockStructure.data));
blockSize = [4 4];
blockyImage64 = blockproc(grayImage, blockSize, meanFilterFunction);
[rows columns] = size(blockyImage64);
% Display the block mean image.
subplot(2, 2, 3);
imshow(blockyImage64, []);
caption = sprintf('Block Mean Image\n64 blocks. Input block size = 4, output block size = 2\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by the standard deviation
% of the pixels in the block.
% Image will be smaller since we are not using ones() and so for each block
% there will be just one output pixel, not a block of 8 by 8 output pixels.
blockSize = [8 8];
StDevFilterFunction = @(theBlockStructure) std(double(theBlockStructure.data(:)));
blockyImageSD = blockproc(grayImage, blockSize, StDevFilterFunction);
[rows columns] = size(blockyImageSD);
% Display the block standard deviation filtered image.
subplot(2, 2, 4);
imshow(blockyImageSD, []);
title('Standard Deviation Filtered Image', 'FontSize', fontSize);
caption = sprintf('Block Standard Deviation Filtered Image\n32 blocks. Input block size = 8, output block size = 1\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);

4 comentarios

FIR
FIR el 28 de Nov. de 2012
I need mean of each Block ,not pixel
Image Analyst
Image Analyst el 28 de Nov. de 2012
Yes. I know. That's why I gave the code. It processes blocks with examples for a 8 by 8 block and a 2 by 2 block of pixels. It does not give the mean of each pixel. If you think about it, the mean of each pixel is simply the image itself again - pretty worthless thing to do, so why would I do that?
Ade Aulya
Ade Aulya el 7 de Feb. de 2020
hi,
what if we want to get the output still in rgbImage ? could it be ?
Image Analyst
Image Analyst el 7 de Feb. de 2020
Take each output from processing each color channel individually and concatenate them together:
blockyImageSDR = blockproc(rgbImage(:, 1), blockSize, StDevFilterFunction); % Process red channel.
blockyImageSDG = blockproc(rgbImage(:, 2), blockSize, StDevFilterFunction); % Process green channel.
blockyImageSDB = blockproc(rgbImage(:, 3), blockSize, StDevFilterFunction); % Process blue channel.
rgbSD = cat(3, blockyImageSDR, blockyImageSDG, blockyImageSDB);

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

FIR
el 28 de Nov. de 2012

Comentada:

el 7 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by