Image / Matrix binning with sum of each bin
조회 수: 99 (최근 30일)
이전 댓글 표시
Hi,
I'm trying to downsample an image/matrix by binning.
What I want to achieve is that each output pixel would be the sum of it's parts.
From what I understood, using the imresize would only be possible with some kind of weighted average and not summing.
Is there a bulit in function that would allow for binning with summing?
If not, what would be the best approach to do that?
Thanks!
O.
댓글 수: 0
채택된 답변
DGM
2022년 1월 5일
Avoiding any sort of weighting suggests to me that this is strictly integer-factor downsampling. If that's the case, then one way might be something like this.
blocksize = [2 2]; % downsampling ratio [h w]
A = ones(10) % test array (geometry must be integer-divisible by blocksize)
B = mat2cell(A,repmat(blocksize(1),[1 size(A,1)/blocksize(1)]),...
repmat(blocksize(2),[1 size(A,2)/blocksize(2)]));
B = cellfun(@(x) sum(x,'all'),B)
댓글 수: 6
DGM
2022년 1월 6일
편집: DGM
2022년 1월 6일
It always takes me forever to make reshape do what I want, but this might work.
A = kron([1 3 5; 2 4 8; 3 6 9],ones(2)) % makes it easy to follow the numbers
s = size(A);
bs = [2 2]; % blocksize
B = reshape(A.',bs(1),s(1)/bs(1),[]);
B = reshape(permute(B,[1 3 2]),bs(1),bs(2),[]);
B = sum(sum(B,1),2);
B = reshape(B,s./bs)
That should be quite a bit faster than using cell operations, even if it's pretty opaque to read.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!