i've divided the image into non-overlapping blocks and i want to calculate the mean of every block...

after calculating the mean i;ve to store it in array corresponding to their block number
plz tell me how to do?
Mean=[];
for row = 1 : blockSizeR : rows
for col = 1 : blockSizeC : columns
row1 = row;
row2 = row1 + blockSizeR - 1;
row2 = min(rows, row2);
col1 = col;
col2 = col1 + blockSizeC - 1;
col2 = min(columns, col2);
oneBlock = grayImage(row1:row2, col1:col2);
m=mean(mean(oneBlock));
M=m;
Mean=[Mean;M];
disp(Mean);
end
end
the above code shows only the mean value... but i want that it also shows the block number corresponding to the mean value...
plz rectify or modify the above code in order to do so.

 採用された回答

Not sure how you want to define block number but how about:
blockNumber = 1;
for row = 1 : blockSizeR : rows
for col = 1 : blockSizeC : columns
row1 = row;
row2 = row1 + blockSizeR - 1;
row2 = min(rows, row2);
col1 = col;
col2 = col1 + blockSizeC - 1;
col2 = min(columns, col2);
oneBlock = grayImage(row1:row2, col1:col2);
Mean(blockNumber) = mean2(oneBlock);
fprintf('The mean for block #%d = $f', ...
blockNumber, Mean(blockNumber));
blockNumber = blockNumber + 1;
end
end

6 件のコメント

angel
angel 2013 年 2 月 14 日
編集済み: angel 2013 年 2 月 14 日
thank u sir....it works as i wanted...now i m trying to sort the mean values...m applying the sort function....i want output in the form as:
mean of block 1 =12.34
mean of block 2= 6.78
mean of block 3= 34.56
mean of block 4=3.56
after applying sort function i want this to be as:
mean of block 4 =3.56
mean of block 2 = 6.78
mean of block 1 = 12.34
mean of block 3 = 34.56
blockNumber = 1;
for row = 1 : blockSizeR : rows
for col = 1 : blockSizeC : columns
row1 = row;
row2 = row1 + blockSizeR - 1;
row2 = min(rows, row2);
col1 = col;
col2 = col1 + blockSizeC - 1;
col2 = min(columns, col2);
oneBlock = grayImage(row1:row2, col1:col2);
Mean(blockNumber) = mean2(oneBlock);
fprintf('The mean for block #%d = $f', ...
blockNumber, Mean(blockNumber));
blockNumber = blockNumber + 1;
end
end
S=sort(Mean);
fprintf('\nBlock # %d = %f',blockNumber,S);
but output is not as per i want... help me sir...
You have to take both return arguments of sort(), not just one.
can u code it sir for me plz.... u mean
S=sort(Mean(blockNumber);
fprintf('\nblock #%d = %f',blockNumber,S);
if not, correct it plz
[S, blockNumber] = sort(Mean);
fprintf('\nBlock #%d = %f', [blockNumber(:), S(:)].' );
angel
angel 2013 年 2 月 15 日
編集済み: Image Analyst 2013 年 2 月 15 日
thank u so much sir :)
ur help means a lot to me.

サインインしてコメントする。

その他の回答 (2 件)

Hi there,
You could do the following:
%%%%%%%%%%%%%
fun = @(block_struct) mean2(block_struct.data);
I2 = blockproc(A,[8 8],fun);
Block_Mean(:,1)=reshape(I2,[1 size(I2,1)*size(I2,2)]);
Block_Mean(:,2)=1:length(Block_Mean);
%%%%%%%%%%%%%%
Where "A" denotes your image and [8 8] is the size of the block of your choice. This will result in "Block_Mean" which holds the mean intensity and the corresponding block number.

2 件のコメント

angel
angel 2013 年 2 月 14 日
@ Abbas: ya sir but block_proc returns single image after processing all the blocks..i've given the code above i have modified it as the image anaylst told me... i m trying to applying the sort function but not stricking to me how to apply
Block_proc returns a single image with the same size as "A" if you use the documented code as in:
fun = @(block_struct) ...
mean2(block_struct.data) * ones(size(block_struct.data));
But in the code I have given you, each pixel in "I2" corresponds to the mean of a single block.
Anyhow, since what you have in hand works for you then happy days!

サインインしてコメントする。

Jan
Jan 2013 年 2 月 14 日
The fast C-MEX function FEX: BlockMean performs this operation.
R = BlockMean(img, blockSizeR, blockSizeC);

3 件のコメント

angel
angel 2013 年 2 月 14 日
this shows error:
Undefined function 'BlockMean' for input arguments of type 'uint8'. input image is 256 x 256
sir plz help me in sorting the mean values which i've calculated...
plz go through the above code that i've mentioned and tell me..
You need to download and install that code in order to have a BlockMean routine.
angel
angel 2013 年 2 月 15 日
ok sir :)
regards Angel

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by