Search code examples
pythonopencvimage-processingcomputer-visionimage-thresholding

How to find out whether a line present in one image is subset of another line present in different image using opencv in python?


I have two images say imageA and imageB as shown below. Now I need to check whether the line present in imageB already exists in image A or not (i.e) line present in imageB should be the subset of line present in imageA. I used image subtraction in open cv but not able to get desired output. I am a newbie to opencv. So someone please provide your inputs.

Note : Can have some threshold for line matching as the line in both images will not match 100% exactly.

ImageA :

ImageB :


Solution

  • Here's an approach:

    1. Take image A, threshold it and morphologically dilate it a little to allow for slight differences in position/alignment. A larger dilation will allow greater misalignment.

    enter image description here

    1. Take image B, threshold it. Count the non-zero pixels.

    2. Do a bitwise AND of the two images and count how many pixels are white in the result - i.e. common to both images.

    enter image description here

    1. Calculate the ratio of the results from steps 2 and 3 to get a measure of the percentage of pixels in image B that are in, or very nearly in image A.