计算 MRI 图像中发现的两个孔的面积

Calculate the areas of two holes found in a MRI image

我正在尝试计算 MRI 图像中发现的两个孔的面积?我尝试使用 imellipse 和 imfreehand。但是我找不到两个洞的区域。一次只有一个。我怎么知道两个孔的面积?

图片:

我认为 Image Segmenter 应用正是您所需要的。它为您提供了多种初始化和优化细分的方法。该应用程序输出代表您感兴趣区域的二进制掩码。之后,您可以使用 regionprops 函数以像素为单位测量它们的面积。

这是一个可以满足您需要的 GUI。最长的部分是设置实际的 GUI,但其余部分非常简单。我评论了代码。正如您将看到的,在您完成对每个区域的追踪后,会自动计算面积。我放了一个编辑框来输入将像素转换为 um 的系数以获得实际距离...这很容易应用于计算。

因此在 GUI 中有 2 个按钮:

1) Trace hole用于在图像上手动跟踪一个孔。一旦你有 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 个洞后:

最后在计算出它们之间的距离后: