Borrar filtros
Borrar filtros

Converting 3D patches to 3D image

1 visualización (últimos 30 días)
Julian Büchel
Julian Büchel el 8 de Dic. de 2017
Comentada: madhan ravi el 13 de Oct. de 2018
Hey guys,
I have a bunch of patches generated from a 3D image of size 64x64x25. I now want to retore the image with these pixels. I was able to do this with the following code:
function [img] = patch2im_2d_time(patch, size_img, size_patch, size_skip, border)
Nx = size_img(1);
Ny = size_img(2);
Nt = size_img(5);
psz1 = size_patch(1);
psz2 = size_patch(2);
psz3 = size_patch(3);
%Extract blocks. This is not necessary
patches = reshape(patch, [psz1 psz2 psz3 size(patch,2)]);
c = 1;
img2 = zeros(squeeze(size_img));
%Count for each pixel how many times we added smth to it.
add_count = zeros(size_img);
%The first three loops, loop through all the pixels in the image
for d=1:Nt-psz3+1
for j=1:Nx-psz2+1
for i=1:Ny-psz1+1
%Here we get the next patch. The next patch is always
%the patch that has the pixel at i,j,d at its top front corner.
current_patch = patches(:,:,:,c);
%counter for the next patch
c = c + 1;
%In this loop we add the patch values of each pixel in the
%patch to the image. i,j,d is the base. We add the offset
%ii jj and dd to it. This iteration takes psz^3 many
%iterations.
for dd=1:psz3
for ii=1:psz2
for jj=1:psz1
img2(i+ii-1,j+jj-1,d+dd-1) = img2(i+ii-1,j+jj-1,d+dd-1) + current_patch(ii,jj,dd);
add_count(i+ii-1,j+jj-1,d+dd-1) = add_count(i+ii-1,j+jj-1,d+dd-1) + 1;
end
end
end
end
end
end
img = flipud(rot90(img2 ./ add_count,1));
end
The problem is that it runs for 15 (!) seconds on my machine. I have an implementation that does it for the 2D case in under 1 second. I know, the for loops are just horrible, but I can't figure out a better way. If you quickly want to try some things, I suggest that you use
reshape(linspace(1,1000,1000),[10 10 10]);
To generate a 10x10x10 matrix that has the indices as entries. That is how I came up with the bad solution. It would be great if somebody could provide an alternative, as I am using this in an iterative algorithm and the overhead is horrible (even for testing).
  1 comentario
Duncan Lilley
Duncan Lilley el 12 de Dic. de 2017
If it is possible in your case, using vectorization instead of loops may improve the runtime of your function.
Here is a documentation page for vectorization in MATLAB.

Iniciar sesión para comentar.

Respuestas (1)

mouna barhoumi
mouna barhoumi el 13 de Oct. de 2018
hello, I need this fuction but in python, someone help me and render this code in python :)
  1 comentario
madhan ravi
madhan ravi el 13 de Oct. de 2018
this forum is for Matlab not python

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by