模板在多个图像上的交叉关联

时间:2013-04-15 00:29:09

标签: matlab

我编写了一个代码,用于在matlab中对多个图像执行模板的互相关。此代码用于跟踪单元格跨多个帧的移动,代码有点交互,首先用户选择图像模板从他/她运行程序以执行互相关的第一帧开始,我已经定义了一个感兴趣的区域,代码必须找到该模板。我的问题是如何在交叉相关检测到并将其定位在所有帧之后获取单元格位置的x和y坐标

 format long
fontSize = 10;
file_name = 'stack0001.tif';  %TIFF Stack
image_info = imfinfo(file_name);
numImg = length(image_info) %Number of images in stack
rgbImage = imread(file_name,'Index', 1);
[sub_rgbImage,rect_rgbImage] = imcrop(rgbImage);
figure,
imshow(sub_rgbImage)
title({'Template Image ' ;'to Search For'});
h=figure;
 for i1=1:numImg %Read Each Frame
    fprintf('Now correlating frame #%d with frame #%d\n',1,i1);

    rect_A= [247.5 134.5 35 81]; % region to look for object
    A=imread(file_name,'Index', i1);%read the following image from image loop (in tiff stack)
    sub_A = imcrop(A,rect_A); % Region of Interest
    figure,
    imshow(sub_A); % Show region of Interest
    axis on;

    % Search the red channel for a match.
    correlationOutput = normxcorr2(sub_rgbImage(:,:,1), sub_A(:,:,1));
    x=size(correlationOutput, 2);
    y=size(correlationOutput, 1);
    h=figure;
    set(h,'visible','off');
    figure, surf(correlationOutput),shading flat;


    h=figure;
    set(h,'visible','off');
    figure('Position', [300 300 300 300]);
    imshow(correlationOutput, []);
    sprintf('Normalized Cross Correlation Output of frame #%d and #%d\n',i1,i1+1);
    title('Cross Correlation');
    %Offset between the images found by correlation

    [maxCorrValue, maxIndex] = max(abs(correlationOutput(:)))
    [ypeak,xpeak] = ind2sub(size(correlationOutput),maxIndex(1))
    corr_offset = [(ypeak-size(sub_rgbImage,1)) 
                       (xpeak-size(sub_rgbImage,2))] ;


    %relative offset between position of subimages
    rect_offset = [(rect_A(1)- rect_rgbImage(1))
                   (rect_A(2)- rect_rgbImage(2))]

    %total offset
    offset = rect_offset+corr_offset;
    xoffset= offset(1)
    yoffset= offset(2)


end

1 个答案:

答案 0 :(得分:0)

如果可以将边界转换为二进制图像,则可以找到边界。也许通过阈值处理背景并创建单元格的二进制图像。然后,您可以使用regionprops方法访问图像中连接组件的属性。或者你可以使用bwboundaries并知道轮廓像素的身份而不是质心。