How to check if contours are the same?
<p>I've got two images, the second one is the first one after modification. I find contours of two images and then check if there any same contours. The problem is, that despite contours being the same (drawing contours of one image and another one gives totally same results) after checking that it never becomes true that one contour is equal to any another.</p>

<p><strong>The code:</strong>
Finding the contours:</p>

<pre><code>List&lt;MatOfPoint&gt; contours1 = new ArrayList&lt;&gt;();
List&lt;MatOfPoint&gt; contours2 = new ArrayList&lt;&gt;();
Imgproc.findContours(contours1mat, contours1, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.findContours(contours2mat, contours2, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
</code></pre>

<p>Checking the contours (it never makes match variable true):</p>

<pre><code>for(int i = 0; i &lt; contours1.size(); i++)
{
    boolean match = false;

    for (int j = 0; j &lt; contours2.size(); j++)
    {
        if (contours1.get(i) == contours2.get(j))
        {
            //never hits that place, despite passing through all contours
            match = true;
            break;
        }
    }

    if (!match)
    {
        contours1.remove(i);
        i--;
    }
}
</code></pre>

<p><strong>What am I doing wrong?</strong> Is this way of checking the similarity of the contours is incorrect? If so, how can I check if two contours are the same? How would You check the contour match if you would need to?</p>

<p><strong>First image:</strong></p>

<p><a href="https://i.stack.imgur.com/4LkLK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4LkLK.jpg" alt="enter image description here"></a></p>

<p><strong>Second image:</strong></p>

<p><a href="https://i.stack.imgur.com/DLF1W.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DLF1W.jpg" alt="enter image description here"></a></p>

<p>The logic behind the second image is that it is the first image but with holes punched in it. I am checking if those holes cross the side of the white zone, if it does not, there should be two same contours in first and second image. <strong>I am only finding the outer contours</strong>, so the contour size is the same in both images.</p>

<p><strong>...</strong></p>

<p><strong>UPDATE!!!</strong>
This code gives me the desired result, but the problem is that it is really <strong>time consuming</strong>, and, obviously, there is a huge room for improvement in the algorythm itself (probably some approximation of contours could be done before the check) or the method I use. I really hope that there is an easy direct result to solve my issue. Code:</p>

<pre><code>for(int i = 0; i &lt; contours1.size(); i++)
{
    boolean match = true;

    for (int j = 0; j &lt; contours2.size(); j++)
    {
        if (contours1.get(i).toArray().length == contours2.get(j).toArray().length)
        {
            boolean match2 = true;
            for (int k = 0; k &lt; contours1.get(i).toArray().length; k++)
            {
                if ((contours1.get(i).toArray()[k].x != contours2.get(j).toArray()[k].x) ||
                    (contours1.get(i).toArray()[k].y != contours2.get(j).toArray()[k].y))
                {
                    match2 = false;
                }
            }
            if (match2)
            {
                match = true;
                break;
            }
        }
        else
            match = false;
    }

    if (!match)
    {
        contours1.remove(i);
        i--;
    }
}
</code></pre>