Matlab will revert back to original image
I am trying to convert a multidimensional array to original image. I split the 512x512 pixel image into 8x8 pixel sub-arrays using a great solution I found in this question :
sub_images = permute(reshape(permute(reshape(i_image, size(i_image, 1), n, []), [2 1 3]), n, m, []), [2 1 3]);
in this case n = m = 8 and sub_images is an array of 8x8x4096. Now the problem is that I want to go back to the original image, avoiding the for loops, but I don't understand how. I know there are functions colfilt
or blockproc
, but I cannot use them. Any help is greatly appreciated!
source to share
Just do the opposite of what you used to modify the original array. The reshape commands remain unchanged (toggling the first and second dimension), while the reshape commands go back to 512
reshaped_i_image = reshape(permute(reshape(permute(sub_images, [2 1 3]), 8, 512, []), [2 1 3]), 512, 512);
source to share
You can solve this with two reshape
and permute
-
out = reshape(permute(reshape(sub_images,n,m,512/n,512/m),[1 3 2 4]),512,[]);
It should be noted that permute
is costly when possible should be avoided.
Below is a runtime test of the stated problem size on the solution approaches listed so far -
i_image = rand(512,512); n = 8; m = 8; sub_images = permute(reshape(permute(reshape(i_image, size(i_image, 1), ... n, []), [2 1 3]), n, m, []), [2 1 3]); func1 = @() reshape(permute(reshape(sub_images,n,m,512/n,512/m),... [1 3 2 4]),512,[]); func2 = @() reshape(permute(reshape(permute(sub_images, [2 1 3]), ... 8, 512, []), [2 1 3]), 512, 512); >> timeit(func1) ans = 0.0022201 >> timeit(func2) ans = 0.0046847
source to share