How to reconstruct the images from the blocks of pixel?

6 ビュー (過去 30 日間)
Eiko
Eiko 2022 年 11 月 23 日
コメント済み: Matt J 2022 年 11 月 24 日
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;
end
if(i==1)
temp = img;
end
if(i~=1)
temp=[temp;img];
end
end
imshow(temp);
But the result of running this way is not right, how to correct it?
  1 件のコメント
Matt J
Matt J 2022 年 11 月 23 日
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)

サインインしてコメントする。

採用された回答

Matt J
Matt J 2022 年 11 月 23 日
編集済み: Matt J 2022 年 11 月 23 日
imgCell=cell(1,31*45);
for i = 1:31*45
imgCell{i} = imread(['C:\Users\Administrator\Desktop\' num2str(i) '.png']);
end
img=cell2mat( reshape(imgCell,45,31)' );
imshow(img)
  2 件のコメント
Eiko
Eiko 2022 年 11 月 24 日
Thank you for your code! I think the error is that I reversed the length and width, and when I use your code, swapping the positions of 45 and 31 in the reshape function and the image is correct. Thank you so much!
Matt J
Matt J 2022 年 11 月 24 日
You're welcome, but please Accept-click the answer to indicate that the question is resolved.

サインインしてコメントする。

その他の回答 (1 件)

DGM
DGM 2022 年 11 月 23 日
編集済み: DGM 2022 年 11 月 23 日
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
end
Atiled = imtile(A); % this is IPT imtile()
imshow(Atiled)
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())
imshow(Atiled)
Atiled = imtile(Atiled,[2 3],'direction','col'); % tile columnwise instead
  1 件のコメント
Eiko
Eiko 2022 年 11 月 24 日
Thank you for your code and the explanations, I have learnd a lot.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by