필터 지우기
필터 지우기

Converting 3D patches to 3D image

조회 수: 1 (최근 30일)
Julian Büchel
Julian Büchel 2017년 12월 8일
댓글: madhan ravi 2018년 10월 13일
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
Duncan Lilley
Duncan Lilley 2017년 12월 12일
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.

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

답변 (1개)

mouna barhoumi
mouna barhoumi 2018년 10월 13일
hello, I need this fuction but in python, someone help me and render this code in python :)
  댓글 수: 1
madhan ravi
madhan ravi 2018년 10월 13일
this forum is for Matlab not python

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

카테고리

Help CenterFile Exchange에서 Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by