making coarse matrix from fine resolution matrix
Show older comments
Hi, I am trying to make a coarse resolution matrix of 3600x1800 from a 8640x4320 matrix by summing up the elements of the matrix.
Hi am trying the following: However this doesnot work with fractions (here, 2.4)
%%%%Z1 is the original 8640x4320 matrix
abc = blockproc(Z1,[2.4,2.4],@(x)sum(x.data));
abc1 = blockproc(abc,[1,2.4],@(x)sum(x.data));
Accepted Answer
More Answers (2)
If you have the Image Processing Toolbox,
abc1=imresize(Z1,[3600,1800])
10 Comments
Image Analyst
on 23 Jan 2020
Or if you want it to look coarse/chunky rather than smooth, use the 'nearest' option.
abc = imresize(Z1, [3600,1800], 'nearest');
Though I'm not sure it would be that noticeable - depends on your image.
SChow
on 23 Jan 2020
Image Analyst
on 23 Jan 2020
To sum up an image, you can use conv2()
kernel = ones(3,3); % Sum up in a local 3x3 neighborhood.
sumImage = conv2(double(grayImage), kernel);
SChow
on 23 Jan 2020
However I need a 3600x1800 matrix, that means I need to sum every 2.4x2.4 cells.
I suspect what you are pursuing might not be different enough from imresize to be worth the trouble. The only difference between what you are doing, and what imresize does is that you are using a rectangular anti-aliasing filter window, while imresize uses some other low pass filter (Gaussian?). Does the difference really matter to you? imresize was written to do resolution reduction in a pretty smart way.
The code that I wrote (embeded in the question) effciently sums up the elements of Z1 to abc1 if the factors in the square bracket were not in fractions.
Your blockproc method is quite inefficient, I'm afraid. Compare to sepblockfun (Download)
Z1=rand(8640,4320);
tic;
abc = blockproc(Z1,[4,1],@(x)sum(x.data));
abc1 = blockproc(abc,[1,4],@(x)sum(x.data));
toc; %Elapsed time is 218.637263 seconds.
tic
abc2=sepblockfun(Z1,[4,4],'sum');
toc; %Elapsed time is 0.086412 seconds.
SChow
on 23 Jan 2020
SChow
on 23 Jan 2020
This should give proper agreement in the sums
abc3=imresize(Z1,1/2.4)*2.4^2;
or
abc3=imresize(Z1,1/2.4);
abc3=abc3/sum(abc3(:))*sum(Z1(:));
SChow
on 23 Jan 2020
4 Comments
I would recommend doing this in horizontal and vertical passes, so that the intermediate matrix S won't be so large,
S = sepblockfun( repelem (Z1 , scaling, 1) , [12,1], 'sum');
abc2 = sepblockfun( repelem (S, 1, scaling) , [1,12], 'sum')/scaling^2;
SChow
on 24 Jan 2020
You can do,
S = sepblockfun( repelem (Z1 , scaling, 1) , [12,1], @nansum);
abc2 = sepblockfun( repelem (S, 1, scaling) , [1,12], @nansum)/scaling^2;
You cannot apply sepblockfun to nanmean directly, because nanmean is not separable, e.g.
>> A=rand(5); A(1:3)=nan
A =
NaN 0.9730 0.8253 0.8314 0.4168
NaN 0.6490 0.0835 0.8034 0.6569
NaN 0.8003 0.1332 0.0605 0.6280
0.6596 0.4538 0.1734 0.3993 0.2920
0.5186 0.4324 0.3909 0.5269 0.4317
>> nanmean(nanmean(A,1),2)
ans =
0.5163
>> nanmean(nanmean(A,2),1)
ans =
0.5142
However, you can implement block-wise nanmean indirectly by using the separability of nansum,
>> sepblockfun(A,[5,5],@nansum)./sepblockfun(~isnan(A),[5,5],@nansum)
ans =
0.5063
>> nanmean(A(:))
ans =
0.5063
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!