我正在尝试计算MRI图像中发现的两个孔的面积?我尝试使用imellipse和imfreehand。但我找不到两个洞的区域。它只是一次一个。我怎么知道两个洞的区域?
图像:
答案 0 :(得分:0)
我认为Image Segmenter app正是您所需要的。它为您提供了多种初始化和细化分段的方法。该应用程序输出表示您感兴趣的区域的二进制掩码。之后,您可以使用regionprops
功能以像素为单位测量其面积。
答案 1 :(得分:0)
这是一个可以满足您需求的GUI。最长的部分是设置实际的GUI,但其余部分非常简单。我对代码进行了评论。如您所见,在完成每个区域的跟踪后,将自动计算该区域。我放了一个编辑框来输入将像素转换为um的因子以获得实际距离...这很容易应用于计算。
因此在GUI中有2个按钮:
1)跟踪孔用于手动跟踪图像上的孔。一旦有2个孔,就可以按下第二个按钮
2)计算点的质心并将其显示在新图中。还计算点之间的距离(以像素为单位)。请注意,它可以推广超过2分,但我会留给您。
以下是截图代码:
function HoleBrain(~)
clc
clear
close all
%// Set up GUI and components
hFigure = figure('Position',[100 100 800 800],'Units','Pixels');
handles.axes1 = axes('Units','Pixels','Position',[80,100,650,650]);
handles.Button = uicontrol('Style','Push','Position',[30 700 90 40],'String','Trace hole','Callback',@(s,e) DrawHoleCallback);
handles.NumHolesTitle = uicontrol('Style','text','Position',[30 670 90 20],'String', '# holes','BackgroundColor',[.6 .6 .6]);
handles.NumHolesDisp = uicontrol('Style','text','Position',[30 640 90 20],'String', '0');
handles.HoleIDTitle = uicontrol('Style','text','Position',[20 610 40 20],'String', 'Hole','BackgroundColor',[.6 .6 .6]);
handles.HoleID = uicontrol('Style','text','Position',[20 540 40 80],'String', '');
handles.AreaTitle = uicontrol('Style','text','Position',[70 610 60 20],'String', 'Area','BackgroundColor',[.6 .6 .6]);
handles.Area = uicontrol('Style','text','Position',[70 540 60 80],'String', '');
handles.PixToMicron = uicontrol('Style','text','Position',[40 500 60 20],'String', 'um/pixel','BackgroundColor',[.6 .6 .6]);
handles.PixToMicronEdit = uicontrol('Style','edit','Position',[40 470 60 20],'String', '');
handles.CentroidButton = uicontrol('Style','push','Position',[10 430 120 20],'String', 'Measure centroids','Callback',@(s,e) MeasureCentroidsCallback,'BackgroundColor',[.1 .4 .9],'FontSize',12);
handles.DistanceTitle = uicontrol('Style','text','Position',[40 400 60 20],'String','Distance','BackgroundColor',[.6 .6 .6]);
handles.DistanceText = uicontrol('Style','text','Position',[40 370 60 20],'String','');
Im = imread('BrainMRI.jpg');
%// Get the size of the image
[handles.h,handles.w,~] = size(Im);
%// Crop the image to get larger field of view of the region to select.
handles.NewIm = Im(round(handles.h/4):round(3*handles.h/4),round(1):round(handles.w/2),:);
handles.NewSize = [size(handles.NewIm,1) size(handles.NewIm,2)];
imshow(handles.NewIm,'InitialMagnification',1000)
%// Set up counter to count # of holes traced.
handles.HoleCounter = 0;
guidata(hFigure,handles);
%// Callback of the pushbutton. Press it to start tracing a hole.
function DrawHoleCallback(~)
handles = guidata(hFigure);
%// Counter
handles.HoleCounter = handles.HoleCounter + 1;
hHand = imfreehand(gca);
%// Store the mask created by the trace. Important!
handles.maskHand{handles.HoleCounter} = createMask(hHand);
%// Calculate/display the area of the region/hole. In pixel units!
outI = uint8(handles.maskHand{handles.HoleCounter}).*handles.NewIm(:,:,1);
handles.AreaPixels(handles.HoleCounter) = sum(outI(:));
A = handles.AreaPixels;
set(handles.NumHolesDisp,'String',num2str(handles.HoleCounter));
set(handles.HoleID,'String',num2str(transpose(1:handles.HoleCounter)));
set(handles.Area,'String',num2str(A(:)));
%// Update guidata.
guidata(hFigure,handles);
end
%// Used to detect centroids and measure the distance between the holes.
function MeasureCentroidsCallback(~)
handles = guidata(hFigure);
%// Create new logical image to actually find centroid and display them.
ImBW = false(handles.NewSize(1),handles.NewSize(2));
for k = 1:handles.HoleCounter
ImBW((handles.maskHand{k})) = 1;
end
%// Get coordinates
S = regionprops(ImBW,'Centroid');
CentroidCoord = vertcat(S.Centroid);
%// Create new figure to display new image
figure('Position',[300 300 800 800],'Units','Pixels');
axes('Units','Pixels','Position',[80,100,650,650]);
imshow(handles.NewIm,'InitialMagnification',1000)
hold on
for k = 1:numel(S)
scatter(S(k).Centroid(:,1), S(k).Centroid(:,2),30,'filled');
end
%//Trace line to connect points
line([CentroidCoord(1,1) CentroidCoord(2,1)],[CentroidCoord(1,2) CentroidCoord(2,2)],'Color','y','LineStyle','-','LineWidth',2)
hold off
%// Measure distance
Dist = pdist(CentroidCoord,'euclidean');
%// Display distance
set(handles.DistanceText,'String',num2str(Dist));
guidata(hFigure,handles);
end
end
GUI如下所示:
追踪2个洞后:
最后计算出它们之间的距离后: