Calculation of the average of 80X80 blocks of 800X1280 Excel sheet data

1 回表示 (過去 30 日間)
Aramis
Aramis 2021 年 9 月 10 日
コメント済み: Aramis 2021 年 10 月 20 日
Hi community,
I have an excel file including 800X1280 data cells. I want to calculate the average of every 80X80 blocks of this sheet (160 blocks in total). Starting from A1 to A80 for columns and 1 to 80 for rows as one block and have the average of it, and then move to next block, A81 to A160 for columns and 1 to 80 for rows and so on. At the end, I should have 10X16 average numbers.
I appreciate your help and answers for learning and understanding it.

採用された回答

Jan
Jan 2021 年 9 月 10 日
Import the data. Then you have a [800 x 1280] matrix. It does not matter, if the data have been stored in an Excel file. Then:
data = rand(800, 1280);
s = size(data);
R = reshape(data, 80, s(1) / 80, 80, s(2) / 80);
M = sum(sum(R, 1), 3);
result = reshape(M, s / 80) / 6400;
This build blocks of the length 80 in the first and third dimension. Then the elements are summed up and divided by the block size. Reshaping remove the two singleton dimensions at the end.
  17 件のコメント
Jan
Jan 2021 年 9 月 30 日
@Aramis: I do not know a mathematical definition of "gradient of change" for a 80x80 matrix. So what do you want to calculate? Do you mean an elementwise difference?
Aramis
Aramis 2021 年 10 月 13 日
@Image Analyst the problem solved by Jan and it made me to make the original question even more complicated to find an answer.
Hi @Jan, Thanks for your reply. Here is the definition in other way, hope it makes sense to you: I want to find how fast the values change from the outer row of blocks to the inner blocks (in all 4 edges). Refering to the photo uploaded above, if I add one more inner rows of blocks, now moving prependicular from outer block to the inner, I'd like to calculate the gradient (how fast changes for each block).

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 9 月 29 日
You can use blockproc() if you have the IMage Processing Toolbox. It will process a matrix in blocks. You can either have the blocks move over a certain distance (so they overlap), but the usual way is to have the processing window "jump" to the next block location over.
I'm attaching several blockproc demos. I'm using them to process images but it can be just a general purpose matrix instead.
You can have the operation be whatever you want : mean, median, standard deviation, gradient, or some custom function of your own.
In short, here's the relevant snippet for taking the mean in 80x80 blocks:
%===============================================================================================================================
% MEAN of 80 pixel by 80 pixel block
% Block process the image to replace every pixel in the
% 80 pixel by 80 pixel block by the mean 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 80 by 80 output pixels.
% Define a filter function that will give 1 pixel as output for every 2-D block of input.
meanFilterFunction = @(theBlockStructure) mean(theBlockStructure.data(:));
blockSize = [80, 80];
blockyImage = blockproc(grayImage, blockSize, meanFilterFunction);
[rows, columns] = size(blockyImage) % Let's see what size we got out
  1 件のコメント
Aramis
Aramis 2021 年 10 月 20 日
Thanks fo your comment. Very helpful and interesting.

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

カテゴリ

Help Center および File ExchangeComputer Vision with Simulink についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by