检测两个图像的重叠行的算法

时间:2013-04-25 15:54:17

标签: image algorithm image-processing

我们说我有2张图像A和B,如下所示。

enter image description here

请注意,A的底部与n像素行的B顶部重叠,由两个红色矩形表示。 A和B具有相同的列数,但可能具有不同的行数。

两个问题:

  • 鉴于A和B,如何有效地确定n
  • 如果B以某种方式改变,其30%-50%的像素被完全替换(例如,假设显示#票数/答案/视图的左上区域被广告横幅替换)。如何确定n

如果有人能够指出算法或更好的算法,使用任何语言(首选C / C ++,C#,Java和JavaScript)的实现,我们非常感激。

5 个答案:

答案 0 :(得分:6)

如果我理解正确,您可能想要查看这两个图像的灰度版本的normalized cross correlation。如果您有大图像或大的重叠区域,则使用图像的FFT(或重叠区域)在频域中最有效地完成此操作,并称为phase correlation

我将采取的基本步骤如下:

  1. 提取第一张图像的下半部分和第二张图像的上半部分。
  2. 将两个图像色块转换为灰度。
  3. 对每个图像补丁执行FFT(此处有一些与窗口和填充有关的详细信息)。
  4. 计算两个FFT的复共轭(与空间域中的相关性相同)。
  5. 对结果进行逆FFT。
  6. 找到上面的峰值,以获得最佳对齐两幅图像的XY移位。
  7. 找到顶部和底部图像补丁之间的相对偏移,您可以根据需要轻松计算 n

    如果您想在不必从头开始编写代码的情况下进行实验,OpenCV有许多模板匹配功能,您可以轻松尝试。 See here for details.

    如果任一图像的一部分已被更改 - 例如通过横幅广告 - 上述过程仍然提供最佳匹配,并且您在步骤6中找到的峰值幅度表示匹配“置信度” - 因此您可以大致了解这两个区域的相似程度。

答案 1 :(得分:3)

我在ImageMagick上做了一些游戏。这是我所做的动画,解释和代码如下。

enter image description here

首先,我使用webkit2png抓取了几个StackOverflow页面,称他们为a.pngb.png

然后我从b.png的左上角和一个宽度相同的列中裁剪出一个矩形,但是a.png的整个高度

这给了我这个:

enter image description here

和这个

enter image description here

我现在将第二页中较小的矩形从第一页覆盖到条带的底部。然后,我通过从另一个中减去一个来计算两个图像之间的差异,并注意当差异为零时,图像必须相同,输出图像将为黑色,因此我找到了它们重叠的点。

以下是代码:

#!/bin/bash
# Grab page 2 as "A" and page 3 as "B"
# webkit2png -F -o A http://stackoverflow.com/questions?page=2&sort=newest
# webkit2png -F -o B http://stackoverflow.com/questions?page=3&sort=newest

BLOBH=256  # blob height
BLOBW=256  # blob width

# Get height of x.png
XHEIGHT=$(identify -format "%h" x.png)

# Crop a column 256 pixels out of a.png that doesn't contain adverts or junk, into x.png
convert a.png -crop ${BLOBW}x+0+0 x.png

# Crop a rectangle 256x256 pixels out of top left corner of b.png, into y.png
convert b.png -crop ${BLOBW}x${BLOBH}+0+0 y.png

# Now slide y.png up across x.png, starting at the bottom of x.png
# ... differencing the two images as we go
# ... stop when the difference is nothing, i.e. they are the same and difference is black image
lines=0
while :; do
   OFFSET=$((XHEIGHT-BLOBH-1-lines))
   if [ $OFFSET -lt 0 ]; then exit; fi
   FN=$(printf "out-%04d.png" $lines)
   diff=$(convert x.png -crop ${BLOBW}x${BLOBH}+0+${OFFSET} +repage \
           y.png \
           -fuzz 5% -compose difference -composite +write $FN \
           \( +clone -evaluate set 0 \) -metric AE -compare -format "%[distortion]" info:)
   echo $diff:$lines
   ((lines++))
done
n=$((BLOBH+lines))

答案 2 :(得分:2)

FFT解决方案可能比您希望的更复杂。 对于一般问题,这可能是唯一可行的方法。

对于一个简单的解决方案,您需要开始做出假设。 例如,您能保证图像的列排列(除非发生了变化)吗?这允许您沿着@n.m。

建议的路径走下去

是否可以将图像切割成垂直条带,如果足够比例的条带匹配,可以考虑行匹配?

[如果我们需要对此进行稳健的话,可以重做一次使用不同列偏移量的通道。]

这就是:

class Image
{
public:
    virtual ~Image() {}
    typedef int Pixel;
    virtual Pixel* getRow(int rowId) const = 0;
    virtual int getWidth() const = 0;
    virtual int getHeight() const = 0;
};

class Analyser
{
    Analyser(const Image& a, const Image& b)
        : a_(a), b_(b) {}
    typedef Image::Pixel* Section;
    static const int numStrips = 16;
    struct StripId
    {
        StripId(int r = 0, int c = 0)
            : row_(r), strip_(c)
        {}
        int row_;
        int strip_;
    };
    typedef std::unordered_map<unsigned, StripId> StripTable;
    int numberOfOverlappingRows()
    {
        int commonWidth = std::min(a_.getWidth(), b_.getWidth());
        int stripWidth = commonWidth/numStrips;
        StripTable aHash;
        createStripTable(aHash, a_, stripWidth);
        StripTable bHash;
        createStripTable(bHash, b_, stripWidth);
        // This is the position that the bottom row of A appears in B.
        int bottomOfA = 0;
        bool canFindBottomOfAInB = canFindLine(a_.getRow(a_.getHeight() - 1), bHash, stripWidth,  bottomOfA);
        int topOfB= 0;
        bool canFindTopOfBInA =  canFindLine(b_.getRow(0), aHash, stripWidth, topOfB);
        int topOFBfromBottomOfA = a_.getHeight() - topOfB;
        // Expect topOFBfromBottomOfA == bottomOfA
        return bottomOfA;
    }
    bool canFindLine(Image::Pixel* source, StripTable& target, int stripWidth, int& matchingRow)
    {
        Image::Pixel* strip = source;
        std::map<int, int> matchedRows;
        for(int index = 0; index < stripWidth; ++index)
        {
            Image::Pixel hashValue = getHashOfStrip(strip,stripWidth);      
            bool match =  target.count(hashValue) > 0;
            if (match)
            {
                ++matchedRows[target[hashValue].row_];
            }
            strip += stripWidth;
        }
        // Can set a threshold requiring more matches than 0
        if (matchedRows.size() == 0)
            return false;
        // FIXME return the most matched row.
        matchingRow = matchedRows.begin()->first;
        return true; 
    }
    Image::Pixel* getStrip(const Image& im, int row, int stripId, int stripWidth)
    {
        return im.getRow(row) + stripId * stripWidth;
    }
    static Image::Pixel getHashOfStrip(Image::Pixel* strip, unsigned width)
    {
        Image::Pixel hashValue = 0;
        for(unsigned col = 0; col < width; ++col)
        {
            hashValue |= *(strip + col);
        }
    }
    void createStripTable(StripTable& hash, const Image& image, int stripWidth)
    {
        for(int row = 0; row < image.getHeight(); ++row)
        {
            for(int index = 0; index < stripWidth; ++index)
            {
                // Warning: Not this simple!
                // If images are sourced from lossy intermediate and hence pixels not _exactly_ the same, need some kind of fuzzy equality here.
                // Details are going to depend on the image format etc, but this is the gist.
                Image::Pixel* strip = getStrip(image, row, index, stripWidth);
                Image::Pixel hashValue = getHashOfStrip(strip,stripWidth);      
                hash[hashValue] = StripId(row, index);
            }
        }
    }

    const Image& a_;
    const Image& b_;

};

答案 3 :(得分:0)

这样的事情可能会有所帮助:

首先,从底部向上遍历图像A,搜索其中包含重要信息的行。例如,可以通过计算整行的总色移来计算“信息”。比如说,两个相邻像素的颜色为#ffffff和#ff0000 - 总计数加2.0。准备好一系列阈值,并锁定达到该阈值的第一行。该系列可以是“10.0,0.1 *行长,0.15 *行长,......”到合理的限制。然后,从最顶层发现向下遍历此数组,取相应的行并从上下搜索其在B中的匹配。如果找到,并且阈值足够大,请取出阵列中的下一个并计算其匹配的位置,然后进行比较。如果成功,则锁定了B与A的正确偏移量,它等于height_of_A - first_row_index + first_row_match_index。如果失败则继续搜索下一行。如果所有匹配都失败,则从B的第一行搜索A的最后一行,直到从A的底部找到的第一行的偏移量。如果再次失败,则答案为0.当然,如果使用JPEG图像使用阈值匹配,因为A和B中的像素可能不精确,也可能对不匹配的像素具有容差。

答案 4 :(得分:-1)

如果行完全匹配,则对两个图像中的行进行排序并合并。你的副本就在那里。然后转到原始图像并找到A中最长的重复连续条纹,这样B中的相应行也是连续的。或者只是看一下相应图像的顶部和底部。

如果有横幅广告,首先想到的是将图像分成几个垂直条带,并分别对每对条带进行操作。