2014-07-23 5 views
0

Matlab 함수 gradient을 사용하여 볼륨의 그래디언트를 계산하려고합니다. 슬라이스 그라디언트를 표시하려면 quiver을 사용합니다.Matlab에서 3d 구조의 그래디언트를 계산하십시오.

x, y 및 z 축에 대해 대칭 인 입방체 볼륨을 사용합니다. 놀랍게도 결과는 모든 조각에서 동일하지 않습니다. 실제로 xy 평면 (Z 슬라이스, 마지막 이미지)의 결과 만 예상되는 결과입니다.

이미지의 경계에서 그래디언트를 계산할 때 문제가 있다는 것을 알고 있습니다. 하지만 나에게있어 국경에서의 결과는 중요하지 않으므로 국경 옆의 결과가 정확한지 상관하지 않는다. 나를 위해 세 가지 이미지가 모두 마지막 이미지처럼 보이는 것이 중요합니다.

누군가 내 코드에 어떤 문제가 있다고 말할 수 있습니까? 감사! gradients of slice in xz-plane gradients of slice in xy-plane

f=zeros(20,20,20); 
space = 5; 
f(:,:,space) = 1; f(:,:,end-space) = 1; 
f(:,space,:) = 1; f(:,end-space,:) = 1; 
f(space,:,:) = 1; f(end-space,:,:) = 1; 
space = 4; 
f(:,:,space) = 1; f(:,:,end-space) = 1; 
f(:,space,:) = 1; f(:,end-space,:) = 1; 
f(space,:,:) = 1; f(end-space,:,:) = 1; 

size_iso = size(f); 
x_slice = round(size_iso(1)/2); 
y_slice = round(size_iso(2)/2); 
z_slice = round(size_iso(3)/2); 

% display the gradient of the edge map 
[fx,fy,fz] = gradient(f,0.1); 
figure; 
image(squeeze(f(x_slice,:,:))*50); colormap(gray(64)); hold on; 
quiver(squeeze(fy(x_slice,:,:)),squeeze(fz(x_slice,:,:))); 
axis equal; 
title(['edge map gradient of X-slice ', num2str(x_slice)]); 

figure; 
image(squeeze(f(:,y_slice,:))*50); colormap(gray(64)); hold on; 
quiver(squeeze(fx(:,y_slice,:)),squeeze(fz(:,y_slice,:))); 
axis equal; 
title(['edge map gradient of Y-slice ', num2str(y_slice)]); 

figure; 
image(squeeze(f(:,:,z_slice))*50); colormap(gray(64)); hold on; 
quiver(squeeze(fx(:,:,z_slice)),squeeze(fy(:,:,z_slice))); 
axis equal; 
title(['edge map gradient of Z-slice ', num2str(z_slice)]); 

답변

1

gradients of slice in yz-plane

는 상황이 조금 3D 행렬과 좌표를 더 복잡합니다. 이미지 와이드 30 픽셀 높이 및 10 개 픽셀을 표시 예

img = rand(10,30); 
imagesc(img); 
axis equal; 

들어

. MatLab에서 이미지를 표시 할 때 첫 번째 차원 (행)은 실제로 플롯에서 Y 축입니다. 두 번째 차원 (열)은 플롯에서 X 축입니다. 코드가 제대로 작동하려면이 슬라이스 이미지뿐만 아니라 차원의 순서에주의를 지불해야하지만 들어

% we need a 3D matrix with 
% 10 points along the X-axis 
% 20 points along the Y-axis 
% 30 points along the Z-axis 
f = rand(20,10,30); % note the order of numbers 

size_iso = size(f),    % gives [20 10 30] 
x_slice = round(size_iso(2)/2) % gives 5 
y_slice = round(size_iso(1)/2) % gives 10 
z_slice = round(size_iso(3)/2) % gives 15 

figure; 
image(squeeze(f(:,x_slice,:))*50); colormap(gray(64)); hold on; 
axis equal; 
title(['X-slice ', num2str(x_slice)]); 
% this code produces image 30 pixels wide and 20 pixels high 
% Thus 1st dimension (vertical axis) is actually the Y-axis 
% Thus 2nd dimension (horizontal axis) is actually the Z-axis 

figure; 
image(squeeze(f(y_slice,:,:))*50); colormap(gray(64)); hold on; 
axis equal; 
title(['Y-slice ', num2str(y_slice)]); 
% this code produces image 30 pixels wide and 10 pixels high 
% Thus 1st dimension (vertical axis) is actually the X-axis 
% Thus 2nd dimension (horizontal axis) is actually the Z-axis 

figure; 
image(squeeze(f(:,:,z_slice))*50); colormap(gray(64)); hold on; 
axis equal; 
title(['Z-slice ', num2str(z_slice)]); 
% this code produces 10 pixels wide and 20 pixels high 
% Thus 1st dimension (vertical axis) is actually the Y-axis 
% Thus 2nd dimension (horizontal axis) is actually the X-axis 

: 고려 http://www.mathworks.com/help/matlab/math/multidimensional-arrays.html

코드에서 실수를 설명하기 위해, 예를 들어, 간단한 예를 참조하십시오 또한 그들이 이동하는 방식으로 squeeze 기능. 따라서 다음 quiver 함수 호출에 적절한 좌표 조합을 제공해야합니다.

주어진 축에 수직 인 슬래브를 고유 한 값으로 채우도록 코드를 수정하여 쉽게 구별 할 수 있어야합니다. 또한 같은 목적으로 각 축을 따라 다른 치수를 사용하고 있습니다.

xvalue=0.33; 
yvalue=0.66; 
zvalue=1.00; 

% we need a 3D matrix with 
% 10 points along the X-axis 
% 20 points along the Y-axis 
% 30 points along the Z-axis 
f = zeros(20,10,30); % note the order of numbers 

space = 3; 
f(:,space,:) = xvalue; f(:,end-space,:) = xvalue; 
f(space,:,:) = yvalue; f(end-space,:,:) = yvalue; 
f(:,:,space) = zvalue; f(:,:,end-space) = zvalue; 

size_iso = size(f); 
x_slice = round(size_iso(2)/2); % note dimension number here for x_slice 
y_slice = round(size_iso(1)/2); % note dimension number here for y_slice 
z_slice = round(size_iso(3)/2); 

% display the gradient of the edge map 
[fx,fy,fz] = gradient(f,0.1); 

figure; 
image(squeeze(f(:,x_slice,:))*50); colormap(gray(64)); hold on; 
quiver(squeeze(fz(:,x_slice,:)),squeeze(fy(:,x_slice,:))); 
axis equal; 
title(['edge map gradient of X-slice ', num2str(x_slice)]); 
xlabel('Z') 
ylabel('Y') 

figure; 
image(squeeze(f(y_slice,:,:))*50); colormap(gray(64)); hold on; 
quiver(squeeze(fz(y_slice,:,:)),squeeze(fx(y_slice,:,:))); 
axis equal; 
title(['edge map gradient of Y-slice ', num2str(y_slice)]); 
xlabel('Z') 
ylabel('X') 

figure; 
image(squeeze(f(:,:,z_slice))*50); colormap(gray(64)); hold on; 
quiver(squeeze(fx(:,:,z_slice)),squeeze(fy(:,:,z_slice))); 
axis equal; 
title(['edge map gradient of Z-slice ', num2str(z_slice)]); 
xlabel('X') 
ylabel('Y') 

예, 이것은 까다롭고 처음에는 이해하기 힘들지만 연습에 익숙해집니다.

+0

실제로 매우 까다 롭지 만 fx는 x 방향의 그라데이션입니다. 따라서 x 방향 종속 데이터로 떨림을 부여해야합니다. y 방향의 그래디언트로 떨림을 전달하면 y 기울기가 0입니다 (fx에 y 그래디언트가 없기 때문에). – nightlyop

+0

예, 그런 것입니다. 직각 투영 ('slice'와'quiver3' 함수 사용) 하에서 슬라이스와 벡터 필드 평면의 3D 렌더링을 수행하고 3D 플롯을 회전시켜 2D 플롯으로 나타낼 수 있습니다. 이 경우 어떤 좌표를 사용해야하는지 쉽게 이해할 수 있습니다. – anandr

관련 문제