필터 지우기
필터 지우기

Reconstruction of image from the vectorized overlapped blocks of an image

조회 수: 2 (최근 30일)
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??

채택된 답변

DGM
DGM 2021년 8월 11일
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
DGM
DGM 2021년 8월 12일
편집: DGM 2021년 8월 12일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2021년 8월 11일
편집: Matt J 2021년 8월 11일
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!

Translated by