Kljuka
Kljuka

Reputation: 63

How do I find image location within document of multiple images and texts using OpenCV

I have a document that contains text and images (it's in png format) and a source image which is included in the document (source image is bigger and in better quality). I would like to find the location (coordinates) of source image in the document.

What makes the process complicated is that the source image in the document might be unevenly scaled (eg.: more vertically than horizontally), a bit cropped (so it's smaller than source image) and slighty modified version of source image (eg.: has rounded edges).

enter image description here

I've tried the OpenCV template matching but with mediocre results - it doesn't handle well uneven scaling.

Is there a way to do it using python and openCV? Or another way?

Upvotes: 1

Views: 302

Answers (1)

cuongptnk
cuongptnk

Reputation: 472

1) Convert the color image to gray, do some threshold, and find contours: link

2) Find the bounding box for contours link

3) Most likely that your image will have some properties for you to pick from the list generated in step 2. For example, width and height are greater than a minimum value.

4) Now create a mask with the chosen bounding box link

5) Now compare histogram distribution between mask region and your image using chi-square distance. A distance less than 1 implies a perfect match. link

6) The bounding box gives the (x,y) coordinates, which you are looking for.

Upvotes: 2

Related Questions