【问题标题】:matlab template matching only for 0 (or 1) in matrixmatlab模板仅匹配矩阵中的0(或1)
【发布时间】:2014-06-18 09:36:17
【问题描述】:

我是 matlab 编程的初学者,但在模板匹配方面遇到了一些问题。我有一些带有黑色边框的白框(下面的图片链接)以及一些文本,我想提取所有框,还有一个带有 X 的框(这是一个多选答案)。一开始我使用 normxcorr2 ,但问题是由于模板的许多白色像素,我得到了一些不相关的模板,比如只有空格。我正在寻找一种仅在 0's 上进行模板匹配的算法,因此我可以获得仅具有黑色方块的模板。我希望我说清楚了,谢谢:)

http://i91.photobucket.com/albums/k284/Chris2401/sqrTemplate.png

【问题讨论】:

    标签: image matlab image-processing matching template-matching


    【解决方案1】:

    编辑:2014 年 5 月 2 日

    现在了解您要实现的目标,我现在可以帮助您解决问题。由于您是 MATLAB 的初学者,因此我将使用更简单的方法(尽管有更复杂的方法可以帮助您解决这个问题),因为我想演示该算法的工作原理。

    您基本上想要实现normxcorr2,但您只想在模板中包含标记为黑色的像素。您还想跳过搜索图像中非黑色的位置。那样的话,我会一步一步给你布局算法,然后写一些代码。

    1. 读入模板并提取那些黑色的像素位置
    2. 以滑动窗口方式,对于与搜索图像中的模板大小相同的每个图像块...
      1. 如果整个图像补丁都是白色的,请跳过
      2. 提取相同的黑色位置
      3. 仅使用这些位置计算 NCC。
    3. 输出将是一张地图,其中包含搜索图像中每个位置的 NCC

    我不会处理搜索图像中边框像素的情况,因为我假设您希望能够将搜索图像中的某些内容与模板的完整大小进行匹配。

    这里有一些假设。让我们假设以下变量:

    • imTemplate - 模板图片,比如你给我看的那个
    • imSearch - 我们在做 NCC 的事情时希望搜索的图像

    我还假设每个图像都是二进制的,因为您的帖子标题中包含“0 或 1”。

    因此,我为您准备了以下代码:

    [rowsSearch colsSearch] = size(imSearch); % Get dimensions of search image
    [rowsTemp colsTemp] = size(imTemplate); % Get dimensions of template image
    
    mapBlack = imSearch == 0; % Obtain a map of where the black pixels are in the template
    numPixelsCompare = sum(mapBlack(:)); % Need to know how many pixels are valid for comparison
    
    % Obtain area of searching within the search image
    startSearchRows = 1 + floor(rowsSearch/2);
    endSearchRows =  rowsSearch - floor(rowsSearch/2);
    startSearchCols = 1 + floor(colsSearch/2);
    endSearchCols = colsSearch - floor(colsSearch/2);
    
    % Need half the dimensions of each for the template dimensions... you will
    % see why we need this later
    rowsTempHalf = floor(rowsTemp/2);
    colsTempHalf = floor(colsTemp/2);
    
    % Where we will store our NCC calculations
    NCCMap = zeros(rowsSearch, colsSearch);
    
    % Because you want to include all of the black pixels in your
    % calculations, and these are all the same, the signal you are comparing
    % to basically consists of all white pixels.
    % Create a vector that consists of all 1s that is the same size as how
    % many black pixels exist in the template
    compareSignal = ones(numPixelsCompare, 1);
    
    % For each location in the search image (ensuring that the full template
    % is inside the image)
    for i = startSearchRows : endSearchRows
         for j = startSearchCols : endSearchCols
              % Grab an image patch that is the same size as the template
              % At each location (i,j) this serves as the CENTRE of the image
              % patch, and we are grabbing pixels surrounding this centre that
              % will create a patch that is the same size as the template
              searchBlock = imSearch(i-rowsTempHalf:i+rowsTempHalf, ...
                                     j-colsTempHalf:j+colsTempHalf);
    
              % If all of the pixels are white, skip
              if (all(searchBlock == 1))
                  continue;
              end
    
              % Extract only those pixels that are valid in the template
              searchPixels = searchBlock(mapBlock);
    
              % Must invert so that black pixels become white 
              % You mentioned that white pixels are "background"
              searchPixels = ~searchPixels;
    
              % Compute NCC for this patch
              NCCMap(i,j) = compareSignal'*searchPixels / ...
                            (sqrt(compareSignal'*compareSignal) * sqrt(searchPixels'*searchPixels));
         end
    end
    

    如果您对我计算 NCC 的方式有点困惑,那基本上是您习惯的方式,但我使用向量代数来计算它。这应该有希望给你你想要的。要找到模板匹配的最佳位置,您可以执行以下操作来提取该位置的行和列:

     [r,c] = find(NCCMap == max(NCCMap(:)));
    

    我希望这能解决您的问题。这有点低效,而且它确实会开始受到更高分辨率的图像的影响,但我想给你一个好的开始,这样你就不会无所事事地试图自己解决这个问题。

    注意:我尚未测试此代码,因为我没有您用来解决此问题的示例搜索图像。希望这会奏效。发表评论,让我知道进展如何。

    【讨论】:

    • 感谢您的回答:) 我想做的正是 normxcorr2 所做的,但仅适用于我图片的黑色像素(0)。因此,如果我的模板是链接中的模板,那么如果边框上有黑色像素,它会返回 1,关于白色像素有什么。
    • 我收到一个矩阵尺寸必须一致的错误。在行“out = corr2(template, block);”
    • 哦,真的吗?如果是这样的话,那么我完全误解了你的帖子。所以你只想沿着那些黑色​​的像素进行模板匹配?这个模板是一个带有黑色像素的矩形边框......所以你应该只在匹配时在模板中包含那些黑色的像素吗?在我去编辑我的帖子之前,我需要非常清楚。顺便说一句,不要投票!我误会了。
    • 不要担心否决票,也许我没有说清楚。是的,我只想模板匹配黑色像素,这意味着如果模板越过图像的一部分,该部分具有类似的边框我展示了结果矩阵的相应坐标将为 1 无论内部(意味着我想忽略任何白色像素)。如果 (:,1)=0 和 (1,:)=0 那么它应该是 0.5 等等上
    • "意思是如果模板越过图像的一部分,该部分具有像我展示的那样的边框,那么结果矩阵的相应坐标将为 1,无论内部是什么"......我是抱歉,这对我没有任何意义。你能详细说明一下吗?很抱歉一直问你问题,但在我修改我的帖子之前,我试图了解这个问题的范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多