在matlab中通过一组dicom图像的中线

Mid line through a set of dicom images in matlab

我在 matlab 上有一组 Dicom 图像,我想添加一条穿过所有图像的中线 我正在通过 imshow3d 函数输出图像

谢谢

编辑:这是我所拥有的,随机点不在中间,它们只是 运行 通过图像

>> 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);

此代码与我为您之前的一个问题提供的答案非常相似;即我不使用 imshow3D 但框架相似且更易于修改以满足您的需要。在这种情况下,按下按钮后,堆栈的中间会出现一条线,您可以使用滑块滚动它。希望对您有所帮助。

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

示例输出:

向上移动滑块后:

这是你的意思吗?如果不是,我会删除那个答案哈哈,抱歉。

看起来 ans.Img 包含由图像堆栈组成的 3D 矩阵。看起来你有一些进展,但请允许我以不同的方式来做这件事。基本上,您需要生成一组坐标,我们可以在其中访问图像堆栈,并在图像堆栈中的每个图像的中间绘制一条垂直线。做这样的事情。首先获取堆栈的尺寸,然后确定列的中间点。接下来,生成一组坐标,为一幅图像在中间画一条线。执行此操作后,对其余切片重复此操作并获取这些的列主要索引:

%// 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;