Image 在matlab中通过一组dicom图像进行中线扫描
我在matlab上有一组Dicom图像,我想添加一条穿过所有图像的中线 我通过imshow3d功能输出图像 谢谢 编辑:这就是我所拥有的,随机点不在中间,它们只是贯穿图像Image 在matlab中通过一组dicom图像进行中线扫描,image,matlab,image-processing,line,dicom,Image,Matlab,Image Processing,Line,Dicom,我在matlab上有一组Dicom图像,我想添加一条穿过所有图像的中线 我通过imshow3d功能输出图像 谢谢 编辑:这就是我所拥有的,随机点不在中间,它们只是贯穿图像 >> clc; >>clear; >>%imports dicom images >>run DicomImport.m; >>%random points for shortest distance test >>a = [1 10 200];
>> 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);
这段代码与我对您前面一个问题的回答非常相似;i、 e.我不使用
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;
太好了。你试过什么?所以我可以选择两个点,在3d中创建一条最短的距离线,这很好,但我想让它穿过每个切片。当图像输出给我时,我也可以使用“插入”选项卡添加一行代码,但我不想看得太过火。哈哈,你为什么不给我们展示一些代码来说明你的尝试?看一看,我添加了一些代码。你能描述一下中线在这里的含义吗?你的意思是从第一个切片的中间画一条3D线,从切片到最后一个切片的中间?啊,做得好。我想我误解了OP想要什么lol。不,这实际上是一些非常不错的代码。你在做我做的事情,但它被包装在一个漂亮的GUI中。@TaimoorKhan-别担心。当你准备好的时候,考虑接受我们的答案。这会告诉StackOverflow社区您不再需要帮助。这是通过单击任意一篇文章顶部的复选标记图标来完成的,该图标位于向上和向下箭头下方的左侧。祝你好运再次感谢!只是一个关于代码的简单问题。是否可以沿x轴移动直线以使其从中间偏移?所以它不是从中间往下走,而是从我选择的任何一点往下走?@TaimoorKhan-所以你想要一条水平线,垂直坐标由你选择?。。。还是要偏移正在绘制垂直线的位置的水平坐标?第一个选择是做更多的工作。第二个选项是简单地将
col_half
变量修改为您选择的任何列,前提是它包含在图像中。