Clear Filters
Clear Filters

Reconstruction of image from the vectorized overlapped blocks of an image

2 views (last 30 days)
I have a matrix whose columns are vectorized overlapped blocks of an image. How to obtain the image back from this matrix using matlab code? For ex: I have taken a 256X256 image, divided into blocks of size 8X8 with 50% overlapping. I have vectorized each block into 64X1 vector and stored as columns of a matrix. I need the matlab code to reassemble this matrix back to image. Can someone help??

Accepted Answer

DGM
DGM on 11 Aug 2021
I don't know how your output is arranged. You have 63^2 vectorized blocks. If they are all columns in a flat 64x3969 array, how are they ordered? Or is it a 3D array (64x63x63)?
I'm going to assume it's 3D
A = imread('cameraman.tif');
% build the vectorized block array
B = zeros(64,63,63,'uint8');
getidx = @(x) (1:8) + 4*(x-1);
for m = 1:63
for n = 1:63
B(:,m,n) = reshape(A(getidx(m),getidx(n)),64,1);
end
end
% rebuild the image
% half of the operations are redundant, but it's simple
% this uses the same indexing function that was used to build B
C = zeros(256,256,'uint8');
for m = 1:63
for n = 1:63
C(getidx(m),getidx(n)) = reshape(B(:,m,n),8,8,1);
end
end
immse(A,C) % image is identical to source
Are there more succinct ways this could be done? Probably.
  3 Comments
DGM
DGM on 12 Aug 2021
Edited: DGM on 12 Aug 2021
Okay, so they're columns in a flat array, but how are they ordered?
Let's assume the array was built by extracting blocks from the image columnwise. Depending on how the array was built, this may be the likely case. This is the assumption that Matt J's answer uses
A = imread('cameraman.tif');
% ASSUMING ARRAY IS BUILT COLUMNWISE
B = zeros(64,3969,'uint8');
getidx = @(x) (1:8) + 4*(x-1);
for n = 1:63
for m = 1:63
B(:,63*(n-1)+m) = reshape(A(getidx(m),getidx(n)),64,1);
end
end
% rebuild the image
C = zeros(256,256,'uint8');
for n = 1:63
for m = 1:63
C(getidx(m),getidx(n)) = reshape(B(:,63*(n-1)+m),8,8);
end
end
immse(A,C) % image is identical to source
ans = 0
The array could have also been built by extracting blocks row-wise.
% ASSUMING ARRAY IS BUILT ROWWISE
B = zeros(64,3969,'uint8'); % size is the same! order changes!
getidx = @(x) (1:8) + 4*(x-1);
for m = 1:63
for n = 1:63
B(:,63*(m-1)+n) = reshape(A(getidx(m),getidx(n)),64,1);
end
end
% rebuild the image
C = zeros(256,256,'uint8');
for m = 1:63
for n = 1:63
C(getidx(m),getidx(n)) = reshape(B(:,63*(m-1)+n),8,8);
end
end
immse(A,C) % image is identical to source
ans = 0
In order to adapt Matt J's answer to the row-wise form, just transpose the flattened cell array of blocks before converting to numeric:
% matt j's answer
temp=reshape(B,8,8,63,63);
temp=num2cell(temp(:,:,1:2:end,1:2:end),[1,2]);
D = cell2mat(reshape(temp,32,32).'); % transpose
immse(A,D)
ans = 0

Sign in to comment.

More Answers (1)

Matt J
Matt J on 11 Aug 2021
Edited: Matt J on 11 Aug 2021
matrix=rand(64,3969); %initial data
temp=reshape(matrix,8,8,63,63);
temp=num2cell(temp(:,:,1:2:end,1:2:end),[1,2]);
reassembled = cell2mat(reshape(temp,32,32)); %result

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!