Problem with imresize function.
7 views (last 30 days)
Show older comments
The problem i am facing is that imresize is not a reversible function.For example,if you convert a binary array of size 5x8 to an array of size 1x192 and then convert it back to size of 5x8,the values are not the same.In case,if u use 'bilinear' attribute then it gives back binary values,however,the values don't match.Please guide me how i can overcome the above problem.(Note:its not essential that the above problem be solved using imresize function.)Any help would be appreciated.
0 Comments
Accepted Answer
Image Analyst
on 23 Apr 2012
You need to use reshape, to get it linear so that you don't lose any elements, and then use the 'nearest option of imresize. Check out this demo:
% Generate sample binary data.
m = randi(2, [5 8])-1
% Now do what Anish wants to do.
mLinear = reshape(m, [1 numel(m)])
% Now make it 192 elements long, for some reason.
mLinear192 = imresize(mLinear, [1 192], 'nearest')
% Undo the process:
mLinear2 = imresize(mLinear192, [1 numel(m)], 'nearest')
m2 = reshape(mLinear2, [5 8]);
% Subtract to see if we did it correctly;
difference1 = mLinear2 - mLinear % Check stage 1.
difference2 = m2 - m % Check final stage
Why do you want to do this anyway? What does this do that you can't do when it's left in its 2D form?
More Answers (2)
Jan
on 23 Apr 2012
Resizing an image until it has a single row only cannot be reversible. Reshaping seems to be more useful, if the problem is not restricted to imresize:
x = rand(5, 8);
y = zeros(1, 192);
y(1:numel(x)) = x(:)';
And backwards:
x2 = reshape(y(1:(5 * 8)), 5, 8);
2 Comments
Geoff
on 23 Apr 2012
The upscale and subsequent downscale of 8 > 192 > 8 should be fine, but I don't know how you expect resizing from 5 > 1 > 5 is going to give you back the same information. When you collapse 5 lines down to 1, you lose all the information in those lines. You cannot scale it back up to 5 and expect your original data to come back out.
Think of this:
R = rand(5,1);
M = mean(R);
How do you recover R using only M?
See Also
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!