How to reconstruct the images from the blocks of pixel?

조회 수: 4 (최근 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 CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by