主要内容

本页采用了机器翻译。点击此处可查看英文原文。

细胞计数

本示例演示了如何结合使用基本形态学运算符和连块分析,从视频流中提取信息。在此示例中,该程序统计了每个视频帧中大肠杆菌的数量。请注意,这些细胞的亮度各不相同,这使得分割任务更加具有挑战性。

初始化

使用以下代码段来初始化所需的变量和对象。

VideoSize = [432 528];

创建一个 System object,用于读取 avi 文件中的视频。

filename = 'ecolicells.avi';
hvfr = VideoReader(filename);

创建一个 BlobAnalysis System object,用于查找视频中分割出的细胞的质心。

hblob = vision.BlobAnalysis( ...
                'AreaOutputPort', false, ...
                'BoundingBoxOutputPort', false, ...
                'OutputDataType', 'single', ...
                'MinimumBlobArea', 7, ...
                'MaximumBlobArea', 300, ...
                'MaximumCount', 1500);

% Acknowledgement
ackText = ['Data set courtesy of Jonathan Young and Michael Elowitz, ' ...
             'California Institute of Technology'];

创建用于显示视频的 System object。

hVideo = vision.VideoPlayer;
hVideo.Name  = 'Results';
hVideo.Position(1) = round(hVideo.Position(1));
hVideo.Position(2) = round(hVideo.Position(2));
hVideo.Position([4 3]) = 30+VideoSize;

流处理循环

创建一个处理循环,用于统计输入视频中的细胞数量。该循环使用了您在上文中实例化的 System object。

frameCount = int16(1);
while hasFrame(hvfr)
    % Read input video frame
    image = im2gray(im2single(readFrame(hvfr)));

    % Apply a combination of morphological dilation and image arithmetic
    % operations to remove uneven illumination and to emphasize the
    % boundaries between the cells.
    y1 = 2*image - imdilate(image, strel('square',7));
    y1(y1<0) = 0;
    y1(y1>1) = 1;
    y2 = imdilate(y1, strel('square',7)) - y1;

    th = multithresh(y2);      % Determine threshold using Otsu's method    
    y3 = (y2 <= th*0.7);       % Binarize the image.
    
    Centroid = step(hblob, y3);   % Calculate the centroid
    numBlobs = size(Centroid,1);  % and number of cells.
    % Display the number of frames and cells.
    frameBlobTxt = sprintf('Frame %d, Count %d', frameCount, numBlobs);
    image = insertText(image, [1 1], frameBlobTxt, ...
            'FontSize', 16, 'BoxOpacity', 0, 'FontColor', 'white');
    image = insertText(image, [1 size(image,1)], ackText, ...
            'FontSize', 10, 'AnchorPoint', 'LeftBottom', ...
            'BoxOpacity', 0, 'FontColor', 'white');

    % Display video
    image_out = insertMarker(image, Centroid, '*', 'MarkerColor', 'green');   
    step(hVideo, image_out);

    frameCount = frameCount + 1;
    pause(1);
end

Figure Movie Player contains an axes object and other objects of type uiflowcontainer, uimenu, uitoolbar. The hidden axes object contains an object of type image.

总结

在“结果”窗口中显示了原始视频,绿色标记表示细胞的质心位置。帧号和细胞数量显示在左上角。

数据集来源说明

例如,本示例中的数据集由加州理工学院的乔纳森·杨 (Jonathan Young) 和迈克尔·埃洛维茨 (Michael Elowitz) 提供。经许可使用。有关此数据的更多信息,请参阅

N. Rosenfeld, J. Young, U. Alon, P. Swain, and M.B. Elowitz, "Gene Regulation at the Single-Cell Level, " Science 2005, Vol. 307, pp. 1962-1965.