How to reconstruct the images from the blocks of pixel?

3 views (last 30 days)
At the beginning I split the image into separate pixel blocks, I have saved all the pixel blocks as PNG images in one folder in order, but now I want to put these pixel blocks back into the image, how do I write the code?
For example, I want to put 8x8 blocks of pixels into an image with 31 rows and 45 columns, and my idea is:
blocks = 1;
temp = zeros(8*31,8*45);
for i = 1:31
img = imread(['C:\Users\Administrator\Desktop\' num2str((i-1)*45+1) '.png']);
for j = 1:45
img2 = imread(['C:\Users\Administrator\Desktop\' num2str(blocks) '.png']);
img = [img,img2];
blocks = blocks + 1;
temp = img;
But the result of running this way is not right, how to correct it?
  1 Comment
Matt J
Matt J on 23 Nov 2022
But the result of running this way is not right, how to correct it?
We can't really know what's wrong if you don't show us imshow(temp)

Sign in to comment.

Accepted Answer

Matt J
Matt J on 23 Nov 2022
Edited: Matt J on 23 Nov 2022
for i = 1:31*45
imgCell{i} = imread(['C:\Users\Administrator\Desktop\' num2str(i) '.png']);
img=cell2mat( reshape(imgCell,45,31)' );
Matt J
Matt J on 24 Nov 2022
You're welcome, but please Accept-click the answer to indicate that the question is resolved.

Sign in to comment.

More Answers (1)

DGM on 23 Nov 2022
Edited: DGM on 23 Nov 2022
Matt's answer is probably the more general answer, but I'll repeat what he said. We can't know what order you're trying to tile things and why it's not working unless you say.
If your images are tiled row-wise, you might be able to use IPT imtile()
nframes = 6;
A = cell(nframes,1);
for f = 1:nframes
A{f} = imread(sprintf('frame_%04d.png',f)); % six test images attached
Atiled = imtile(A); % this is IPT imtile()
You could also pass imtile() a list of file paths directly instead of reading the images into a cell array. If you want the images tiled columnwise instead, you'll have to reshape and/or transpose the cell array accordingly, as IPT imtile() doesn't have any explicit means of controlling the tiling order. For the conveniences it offers, I feel that things like this make imtile() negligibly more worthwhile than just doing cell array manipulation -- because you usually still literally have to do cell array manipulation to use it.
MIMT (file exchange) has an image tiling tool of the same name, but it's strictly made for tiling images from a 4D array. That said, at least the tiling order can be controlled, and it can be easier to write at times. In this example, I'm going to assume that the frames can be selected with a simple wildcard expression. If that's the case, MIMT mimread() can be used to read all the frames at once.
% all of these are MIMT tools
A = mimread('frame_*.png'); % read all frames in the directory
Atiled = imstacker(A,'padding',0); % stack frames
Atiled = imtile(Atiled,[2 3],'direction','row'); % tile row-wise (MIMT imtile())
Atiled = imtile(Atiled,[2 3],'direction','col'); % tile columnwise instead
  1 Comment
Eiko on 24 Nov 2022
Thank you for your code and the explanations, I have learnd a lot.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by