Mid line through a set of dicom images in matlab

221 views Asked by At

I have a set of Dicom images on matlab and i would like to add a midline going through all the images I am outputting the images via imshow3d function

thanks

Edit: here's what i have, the random points are not in the middle they just run through the image

>> clc;

>>clear;

>>%imports dicom images

>>run DicomImport.m;

>>%random points for shortest distance test

>>a = [1 10 200];

>>b = [500 512 300];

>>ab = b - a;

>>n = max(abs(ab)) + 1;

>>s = repmat(linspace(0, 1, n)', 1, 3);

>>for d = 1:3

  >>  s(:, d) = s(:, d) * ab(d) + a(d);

>>end



>>s = round(s);

>>Z = 593; 

>>N = 512;

>>X = zeros(N, N, Z);

>>X(sub2ind(size(X), s(:, 1), s(:, 2), s(:, 3))) = 1;

>>C = find(X);

>>ans.Img(C) = 5000;


>> %shows image

>>imshow3D(ans.Img);
2

There are 2 answers

5
rayryeng On BEST ANSWER

So it looks like ans.Img contains the 3D matrix consisting of your image stack. It looks like you've got something going, but allow me to do this a bit differently. Basically, you need to generate a set of coordinates where we can access the image stack and draw a vertical line in the middle of the each image in the image stack. Do something like this. First get the dimensions of the stack, then determine the halfway point for the columns. Next, generate a set of coordinates that will draw a line down the middle for one image. After you do this, repeat this for the rest of the slices and get the column major indices for these:

%// Get dimensions
[rows,cols,slices] = size(ans.Img);

%// Get halfway point for columns
col_half = floor(cols/2);

%// Generate coordinates for vertical line for one slice
coords_middle_row = (1:rows).';
coords_middle_col = repmat(col_half, rows, 1);

%// Generate column major indices for the rest of the slices:
ind = sub2ind(size(ans.Img), repmat(coords_middle_row, slices, 1), ...
             repmat(coords_middle_col, slices, 1), ...
             reshape(kron(1:slices, ones(rows, 1)), [], 1));

%// Set the pixels accordingly
ans.Img(ind) = 5000;
0
Benoit_11 On

This code is quite similar to the answer I provided to one of your earlier question; i.e. I don't use imshow3D but the framework is similar and simpler to modify in order to suit your need. In this case, upon pressing a pushbutton a line appears at the middle of the stack and you can scroll through it with the slider. I hope this can be of help.

function LineDicom(~)
clc
clear
close all

%// Load demo data
S = load('mri');

%// Get dimensions and number of slices.
ImageHeight = S.siz(1); %// Not used here
ImageWidth = S.siz(2); %// Not used here
NumSlices = S.siz(3);

S.D = squeeze(S.D);

%// Create GUI
hFig = figure('Position',[100 100 400 400],'Units','normalized');

%// create axes with handle
handles.axes1 = axes('Position', [0.2 0.2 0.6 0.6]);

%// create y slider with handle
handles.y_slider = uicontrol('style', 'Slider', 'Min', 1, 'Max', NumSlices, 'Value',1, 'Units','normalized','position', [0.08 0.2 0.08 0.6], 'callback', @(s,e) UpdateY);
handles.SlideryListener = addlistener(handles.y_slider,'Value','PostSet',@(s,e) YListenerCallBack);

%// Create pusbutton to draw line
handles.DrawLineButton= uicontrol('style', 'push','position', [40 40 100 30],'String','Draw line', 'callback', {@DrawLine,handles});

%// Flag to know whether pushbutton has been pushed
handles.LineDrawn = false;

%// Show 1st slice
imshow(S.D(:,:,1))

guidata(hFig,handles);

%// Listeners callbacks followed by sliders callbacks. Used to display each
%// slice smoothly.
    function YListenerCallBack

        handles = guidata(hFig);

        %// Get current slice
        CurrentSlice = round(get(handles.y_slider,'value'));

        hold on
        imshow(S.D(:,:,CurrentSlice));

        %// If button was button, draw line
        if handles.LineDrawn
            line([round(ImageWidth/2) round(ImageWidth/2)],[1 ImageHeight],'Color','r','LineWidth',2);
        end
        drawnow

        guidata(hFig,handles);

    end

    function UpdateY(~)

        handles = guidata(hFig); %// Get handles.
        CurrentSlice = round(get(handles.y_slider,'value'));

        hold on
        imshow(S.D(:,:,CurrentSlice));

        if handles.LineDrawn
            line([round(ImageWidth/2) round(ImageWidth/2)],[1 ImageHeight],'Color','r','LineWidth',2);
        end
        drawnow

        guidata(hFig,handles);

    end

%// Pushbutton callback to draw line.
    function DrawLine(~,~,handles)

        line([round(ImageWidth/2) round(ImageWidth/2)],[1 ImageHeight],'Color','r','LineWidth',2);

        handles.LineDrawn = true;
        guidata(hFig,handles);
    end

end

Sample output:

enter image description here

and after moving the slider up:

enter image description here

Is this what you meant? If not I'll remove that answer haha and sorry.