1

Currently, I try to develop AI system by Python to matching Sacred object from 2 photo. The first photo is a high-resolution Sacred object (A) photo capture by DSLR camera. enter image description here The second photo is a Sacred object (A) photo capture by Phone Camera. enter image description here

I need the AI system to predict the percentage of similar from 0 - 100%. I try some methodlogy but I don't get a good result.

Please suggest which methodlogy will be fit to prediction model? THANK

arttioz
  • 265
  • 3
  • 6
  • https://stackoverflow.com/questions/52736154/how-to-check-similarity-of-two-images-that-have-different-pixelization have you checked this? – Chayan Bansal Sep 04 '20 at 12:08

1 Answers1

2

One approach is using template-matching

If you use the first image as a template:

  • Template image:

  • enter image description here

  • Source image:


  • enter image description here

  • Result will be:


  • enter image description here

Steps:


    1. Convert both template and source images to the gray-scale and apply Canny edge detection.
    • template = cv2.imread("template_resized.jpg")
      template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
      template = cv2.Canny(template, 50, 200)
      
    • enter image description here

    • source = cv2.imread("source_resized.jpg")
      source = cv2.cvtColor(source, cv2.COLOR_BGR2GRAY)
      source = cv2.Canny(source, 50, 200)
      
    • enter image description here

    1. Check whether the template matches the source image
    • result = cv2.matchTemplate(source, template, cv2.TM_CCOEFF)

    • We need maximum-value and maximum-value location from the result

    • (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

    1. Get coordinates and draw the rectangle
    • (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
      (endX, endY) = (int((maxLoc[0] + w) * r), int((maxLoc[1] + h) * r))
      
    • Draw Rectangle

    • cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
      

Possible Question: Why didn't you use the original image size?


Answer: Well, template-matching works better for small images. Otherwise the result is not satisfactory. If I use the original image size the result will be: link

Possible Question: Why did you use cv2.TM_CCOEFF?


Answer: It was just an example, you can experiment with the other parameters

Possible Question: How do I calculate the similarity-percentage using template-matching?


Answer: Please look at this answer. As stated you can use the minMaxLoc's output for similarity percentage.

For full code, please look at the opencv-python tutorial.

Ahmet
  • 7,527
  • 3
  • 23
  • 47