matlab模板仅匹配矩阵中的0(或1) [英] matlab template matching only for 0 (or 1) in matrix

查看:227
本文介绍了matlab模板仅匹配矩阵中的0(或1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是matlab编程的初学者,我在模板匹配方面遇到了一些麻烦。我有几个带有黑色边框的白色框(下面的图片链接)以及一些文字,我想要提取所有框,还有一个有X的(它是一个多选答案)。在开始时我使用了normxcorr2,但问题是由于模板的许多白色像素,我得到了许多不相关的模板,比如只有空格。我一直在寻找一种只在0上进行模板匹配的算法,所以我可以得到只有黑色方块的模板。我希望自己明确表示感谢,谢谢:)

I'm a beginner in matlab programming and i'm having some troubles with template matching. I have a few white boxes with a black border (link for pic below) along with some text and I want to extract all the boxes,and there's also one that has an X in it(it's a multiple choice answer). In the beginning I used normxcorr2 , but the problem is that due to the many white pixels of the template, I get allot of irrelevant templates like only white spaces. I was looking for an algorithm that does template matching only on 0's , so I could get templates that have black squares only. I hope I made myself clear, thanks :)

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

推荐答案

编辑:2014年5月2日

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

Now understanding what you are trying to achieve, I can now help you solve your problem. As you are a beginner to MATLAB, I will use a more simpler approach (even though there are more sophisticated methods to help you figure this out) as I want to demonstrate how the algorithm works.

您基本上想要实现 normxcorr2 ,但您只想在模板中包含标记为黑色的像素。您还希望跳过搜索图像中非黑色的位置。在这种情况下,我将逐步为您布局算法,然后编写一些代码。

You basically want to implement normxcorr2, but you only want to include pixels that are marked as black in the template. You also want to skip locations in the search image that are not black. In that case, I will layout the algorithm for you step by step, then write some code.


  1. 读入模板并提取这些像素黑色的位置

  2. 以滑动窗口的方式,对于与搜索图像中的模板大小相同的每个图像补丁...
  1. Read in the template and extract those pixel locations that are black
  2. In a sliding window fashion, for each image patch of the same size as the template that is in the search image...

  1. 如果整个图像补丁为白色,请跳过

  2. 提取相同的黑色位置

  3. 仅使用这些位置计算NCC 。


  • 输出将是包含搜索图像中每个位置的NCC的地图

  • 我不会处理搜索图像中边框像素的情况,因为我假设您希望能够匹配搜索图像中的全尺寸内容模板。

    I will not handle the case of the border pixels in the search image, as I am assuming that you want to be able to match something in the search image with the full size of the template.

    以下是一些假设。我们假设以下变量:

    Here are some assumptions. Let's assume the following variables:


    • imTemplate - 模板图像,例如你告诉我的一个

    • imSearch - 我们希望在做这个NCC时搜索的图像

    • imTemplate - The template image, such as the one that you have showed me
    • imSearch - The image we wish to search for when doing this NCC stuff

    我还假设每张图片都是二进制的,因为帖子的标题中包含0或1。

    I am also assuming that each of the images are binary, as the title of your post has "0 or 1" in it.

    因此,我有以下代码:

    [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,它基本上是你习惯的,但我用矢量代数来计算它。这应该有希望给你你想要的。要找到模板匹配位置的最佳位置,您可以执行以下操作以提取此位置的行和列:

    If you're a bit confused with the way I calculated the NCC, it is basically what you're used to, but I used vector algebra to compute it instead. This should hopefully give you what you want. To find the best location of where the template matched, you can do the following to extract the row and column of this location:

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

    我希望这能解决你的问题。这是一个有点低效的,它会真正开始受到更高分辨率图像的影响,但我想给你一个良好的开端,所以你不要闲着试图自己想出来。

    I hope this solves your question. It is a tad inefficient, and it will really start to suffer with higher resolution images, but I wanted to give you a good start so you're not sitting around idle trying to figure it out on your own.

    NB:我还没有测试过此代码,因为我没有用于解决此问题的示例搜索图像。希望这会奏效。发表评论让我知道它是怎么回事。

    NB: I have not tested this code yet as I don't have an example search image that you are using to solve this problem. Hopefully this will work. Leave a comment and let me know how it goes.

    这篇关于matlab模板仅匹配矩阵中的0(或1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆