From c81508738f93816ed43adc8422c0ff05ab81147b Mon Sep 17 00:00:00 2001 From: Laila Saadatifard Date: Fri, 9 Oct 2015 14:59:21 -0500 Subject: [PATCH] fix the matlab ivote3 code to works on the synthetic image. --- Matlab_3D/main.m | 71 ++++++++++++++++++++++++++++++++++++++--------------------------------- Matlab_3D/old-matlab.m | 232 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 files changed, 38 insertions(+), 265 deletions(-) delete mode 100644 Matlab_3D/old-matlab.m diff --git a/Matlab_3D/main.m b/Matlab_3D/main.m index 783af62..bc431fd 100644 --- a/Matlab_3D/main.m +++ b/Matlab_3D/main.m @@ -6,11 +6,11 @@ total = tic; % ******* Initialize voting parameters ************************************** rmax = 7; %maximum radius of the cell -ang_deg = 15.1; %half the angular range of the voting area +ang_deg = 25.1; %half the angular range of the voting area ang = ang_deg * pi / 180; -iter = 3; %number of voting iterations +iter = 5; %number of voting iterations t0 = 1.2; %threshold color -sigma = [4, 4, 2]; +sigma = [1, 1, 1]; % t = 0.1; d_ang= ang / (iter); @@ -18,8 +18,7 @@ d_ang= ang / (iter); % p = [100, 50, 100]; % ps = [200, 200, 100]; ps = [100, 50, 40]; -r = 7; -I = syn_Img(r , ps); +I = syn_Img(rmax , ps); % volfile = 'img\nissl-rat.vol'; % fid = fopen(volfile); % open the file that include the image % S = fread(fid, 3, 'int32'); @@ -205,8 +204,6 @@ for itr = 1 : iter t_v1 = toc; disp(['voting done. time =',num2str(t_v1)]); - figure, imagesc(squeeze(Ivote(:, :, ceil(size(Ivote, 3)/2),:))); colormap(gray); - % update the voting direction tic; for v = 1: nV @@ -222,15 +219,12 @@ for itr = 1 : iter [~, local_max_idx] = max(local_Ivote); %convert this into a global subscript - [gx, gy, gz] = ind2sub(size(Ivote), g_v_prime(v,local_max_idx)); + [g_px, g_py, g_pz] = ind2sub(size(Ivote), g_v_prime(v,local_max_idx)); %compute the vector from the voter position to this position - dx = gx - vx; - dy = gy - vy; - dz = gz - vz; - Igrad_x(vx, vy, vz) = dx; - Igrad_y(vx, vy, vz) = dy; - Igrad_z(vx, vy, vz) = dz; + Igrad_x(vx, vy, vz) = g_px - vx; + Igrad_y(vx, vy, vz) = g_py - vy; + Igrad_z(vx, vy, vz) = g_pz - vz; end @@ -241,7 +235,7 @@ for itr = 1 : iter %% -t = 10; +t = 2500; out = Ivote; out(out=t) = 255; @@ -250,10 +244,10 @@ out = imregionalmax(out); out1(:,:,:,1) = mat2gray(Iblur); out1(:,:,:,2) = mat2gray(out); out1(:,:,:,3) = mat2gray(Iblur); +% out = out1; - -figure(2); +figure(4); subplot(2, 3, 1), imagesc(squeeze(Iblur(:, :, ceil(size(Iblur, 3)/2)))); @@ -282,19 +276,30 @@ colormap(gray); % % figure, imagesc(squeeze(Iblur( ceil(size(Iblur, 1)/2), :, :))); colormap(gray); % figure, imagesc(squeeze(Ivote( ceil(size(Ivote, 1)/2), :, :))); colormap(gray); % -fid0 = fopen('iblur.vol', 'w'); -fwrite(fid0, Iblur); -fclose(fid0); - -fid1 = fopen('ivote1.vol', 'w'); -fwrite(fid1, Ivote1); -fclose(fid1); - -fid2 = fopen('ivote2.vol', 'w'); -fwrite(fid2, Ivote2); -fclose(fid2); - -fid3 = fopen('ivote3.vol', 'w'); -fwrite(fid3, Ivote3); -fclose(fid3); - +% fid0 = fopen('D:\ivote3_files\iblur.vol', 'w'); +% fwrite(fid0, Iblur); +% fclose(fid0); +% +% fid1 = fopen('D:\ivote3_files\ivote1.vol', 'w'); +% fwrite(fid1, Ivote1); +% fclose(fid1); +% +% fid2 = fopen('D:\ivote3_files\ivote2.vol', 'w'); +% fwrite(fid2, Ivote2); +% fclose(fid2); +% +% fid3 = fopen('D:\ivote3_files\ivote3.vol', 'w'); +% fwrite(fid3, Ivote3); +% fclose(fid3); +% +% fid4 = fopen('D:\ivote3_files\ivote4.vol', 'w'); +% fwrite(fid4, Ivote4); +% fclose(fid4); +% +% fid5 = fopen('D:\ivote3_files\ivote5.vol', 'w'); +% fwrite(fid5, Ivote5); +% fclose(fid5); +% +% fid10 = fopen('D:\ivote3_files\ivote10.vol', 'w'); +% fwrite(fid10, Ivote); +% fclose(fid10); diff --git a/Matlab_3D/old-matlab.m b/Matlab_3D/old-matlab.m deleted file mode 100644 index 135356b..0000000 --- a/Matlab_3D/old-matlab.m +++ /dev/null @@ -1,232 +0,0 @@ -clc; -clear; -disp('***************** NEW RUN *********************'); -total = tic; - - -% ******* Initialize voting parameters ************************************** -rmax = 9; %maximum radius of the cell -ang_deg = 15.1; %half the angular range of the voting area -ang = ang_deg * pi / 180; -iter = 1; %number of voting iterations -t0 = 1.2; %threshold color -sigma = [4, 4, 2]; -% t = 10; -d_ang= ang / (iter); - -% ******** Testing parameters ****************************************** -p = [100, 50, 100] ; -ps = [50, 50, 25] - 1; -% p = [100, 100, 100] ; -% ps = [300, 300, 150] - 1; - -volfile = 'img\nissl-rat.vol'; -fid = fopen(volfile); % open the file that include the image -S = fread(fid, 3, 'int32'); -X = S(1); -Y = S(2); -Z = S(3); - -% load the VOL data into a 2D matrix -I = fread(fid,[X Y*Z], 'uint8'); -fclose(fid); - -%change this to a 3D matrix -I = single(reshape(I, [X, Y, Z])); - -% invert the intensity -I = 255 - I; - -%perform a gaussian blur -I_blur = gauss_blur3d(I, sigma); - -% compute the gradient -[Igrad_x, Igrad_y, Igrad_z] = gradient(I_blur); - -%%crop out a small subregion of I -Isub_x = Igrad_x(p(1):p(1)+ps(1), p(2):p(2)+ps(2), p(3):p(3)+ps(3)); -Isub_y = Igrad_y(p(1):p(1)+ps(1), p(2):p(2)+ps(2), p(3):p(3)+ps(3)); -Isub_z = Igrad_z(p(1):p(1)+ps(1), p(2):p(2)+ps(2), p(3):p(3)+ps(3)); - -%calculate the gradient magnitude -Imag = sqrt(Isub_x .^ 2 + Isub_y .^ 2 + Isub_z .^2); -I_theta = acos(Isub_z./Imag); -I_phi = atan(Isub_y./Isub_x); - - -%set a threshold for the gradient magnitude -It = Imag > t0; - -% -% thresholding -It_x = size(It,1); -It_y = size(It,2); -It_z = size(It,3); -% -It(1:rmax, :, :) = 0; -It(It_x - rmax:It_x, :,:) = 0; -It(:, 1:rmax, :) = 0; -It(:, It_y - rmax:It_y,:) = 0; -It(:, :, 1:rmax) = 0; -It(:,:, It_z - rmax:It_z) = 0; - -% -[Itx,Ity,Itz] = (ind2sub(size(It),find(It))); -Itx = single(Itx); -Ity = single(Ity); -Itz = single(Itz); -nV = nnz(It); - - -% create a meshgrid describing coordinates relative to the voter position -range = single(-rmax):single(rmax); -[mx, my, mz] = (meshgrid(range, range, range)); -m_mag = sqrt(mx.^2 + my.^2 + mz.^2); - -% create a mask for the voting area -M_dist = mx.^2 + my.^2 + mz.^2 < rmax^2; -M_theta = acos(mz./m_mag); -M_phi = atan(my./mx); -M_phi (my ==0 & mx ==0) = 0; -pxPerRow = single(size(It,1)); -pxPerCol = single(size(It,2)); -validPoints = single(zeros(nV,1)); - -% g_v_prime = zeros(nV, ceil(rmax^2*ang/3)); -g_v_prime = single(zeros(nV, (rmax^3))); - - - -disp('first part done.'); -%% vote -tic; - for itr = 1 : iter - - Ivote = single(zeros(size(It))); - for v = 1: 1 - -% - % coordinates of the current voter - vx = Itx(v); - vy = Ity(v); - vz = Itz(v); -% - vtheta= I_theta(vx,vy,vz); - vphi = I_phi(vx,vy,vz); - vmag = Imag(vx,vy,vz); -% ang_diff = single(zeros([2*rmax+1 2*rmax+1 2*rmax+1])); -% for i=1: 2*rmax +1 -% for j=1: 2*rmax+1 -% for k = 1:2*rmax+1 -% ang_diff(i,j,k) = acos(dot([Isub_x(vx,vy,vz) Isub_y(vx,vy,vz) Isub_z(vx,vy,vz)], [mx(i,j,k) my(i,j,k) mz(i,j,k)])/(vmag*norm([mx(i,j,k) my(i,j,k) mz(i,j,k)]))); -% end -% end -% end - ang_diff = acos(((sin(M_theta).*sin(vtheta)).*cos(vphi - M_phi)) + cos(M_theta).*cos(vtheta)); - M_diff = min(2*pi - ang_diff,ang_diff)