Average H of hsv

2 ビュー (過去 30 日間)
Xiaochao
Xiaochao 2012 年 9 月 11 日
I want to divide a picture of 640*480 pixels to 16*16 blocks and get average value H of every block. The result shoud be a 40*30 array. But I tried mean function, the result always is a 1*16 array.
  1 件のコメント
Jan
Jan 2012 年 9 月 11 日
Please post the code. It is impossible to debug "I tried mean function".

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

回答 (3 件)

Rene
Rene 2012 年 9 月 11 日
A = rand(480,640);
B=[];
for i=1:16:480
for j=1:16:640
B = [B,mean(mean(A(i:i+15,j:j+15)))];
end
end
B = reshape(B,30,40);
should do the trick
note: inefficient for large matrices

Jan
Jan 2012 年 9 月 11 日
Exactly as I have posted in your other thread, which contains almost the same question:
H = rand(640, 480);
H = reshape(H, 16, 40, 16, 30);
blockMean = reshape(sum(sum(H, 1), 3), 40, 30) / 256;
  1 件のコメント
Xiaochao
Xiaochao 2012 年 9 月 11 日
Thank you very much. The the code does work.I'm a new user and still learnt the functions

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


Sean de Wolski
Sean de Wolski 2012 年 9 月 11 日
編集済み: Sean de Wolski 2012 年 9 月 11 日
Why not use blockproc() like I suggested in the other thread? It will do this for you completely automagically - there is no reason to reinvent the wheel.
bm = blockproc(H,[16 16],@(blk)mean(blk.data(:)));
  4 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 9 月 11 日
0.06 seconds with a little more intelligence in anonymous function creation:
blksz = [16 16];
n = 16^2;
bm = blockproc(H,blksz,@(blk)sum(sum(blk.data))./n);
Jan
Jan 2012 年 9 月 11 日
Ok, Sean. I admit one 640x480 picture is tiny. Then I claim that the Resahpe/Sum approach is simpler that Blockproc with an anonymous function. But this is not a convincing argument also: Both solutions can be used by copy&paste now, such that the "programming" will need a few seconds only.
I avoid to suggest the faster C-Mex function http://www.mathworks.com/matlabcentral/fileexchange/24812-blockmean, which I have published after the "mean over blocks"-questions have been written repeatedly. Here the speed advantage will be eaten up by the time reuquired to install the C-compiler...

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by