问题
Quite hard to explain what I am looking for, I have an image represented as a m by n matrix in Matlab and I am trying to scale it down to 4x4 the same way an image would be scaled (average the nearest values)
So for example
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
0 2 3 4 9 9 7 8
0 2 3 4 9 9 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 7
1 2 3 4 5 6 7 7
Would become
1.5 3.5 5.5 7.5
1.0 3.5 9.0 7.5
1.5 3.5 5.5 7.5
1.5 3.5 5.5 7.0
回答1:
Looks like imresize
gives something slightly different from what you expected. For your input data, the following will work:
A = filter2([1 1; 1 1] / 4, X, 'same')
A = A(1:2:end, 1:2:end);
EDIT: Actually, it's probably faster to do the following:
i = 1:2:size(A,1)-1;
j = 1:2:size(A,2)-1;
B = 0.25 * (A(i,j) + A(i+1,j) + A(i,j+1) + A(i+1,j+1));
回答2:
Any reason why you can't use the imresize() function? It does exactly what you want..
I =
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
0 2 3 4 5 6 7 8
0 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
I2 = imresize(I,[4 4])
I2 =
1.4688 3.4919 5.5117 7.4922
1.0742 3.5289 5.5117 7.4922
1.4688 3.4919 5.5117 7.4922
1.5137 3.4877 5.5117 7.4922
回答3:
If you don't have the imresize.m from the Image Processing Toolbox, you can see how octave does it here (it uses convolution).
来源:https://stackoverflow.com/questions/7741808/matlab-scale-down-a-vector-with-averages