0

I am trying to prepare program for comparison 2D drawings or just views on those drawings that can be positioned within the drawing in slightly different positions.

For now I have program that compares the drawings using SSIM and OpenCV but if i.e. one of the views on the drawing will be slightly shifted it will be marked as a completely different view.

Moreover I want to also have possibility to compare the screenshot taken with Windows snipping tool in the rectangular mode. Due to that the input screenshots would be in different sizes and ratios.

Below I show you example what is the expected result with given input as: IMAGE_1: IMAGE_2:

And the expected result should be like this: IMAGE_1 - expected result: IMAGE_2 - expected result:

Help me with the idea how to solve this problem. I am newbie in image processing and in programming in general so please show me the right direction with this ;) Thanks in advance.

Code is copied from some other post and works only for drawing where the views are in the same position.

from skimage.metrics import structural_similarity
import cv2
import numpy as np


# Load images
before = cv2.imread('im1.jpg')
after = cv2.imread('im2.jpg')

# Convert images to grayscale
before_gray = cv2.cvtColor(before, cv2.COLOR_BGR2GRAY)
after_gray = cv2.cvtColor(after, cv2.COLOR_BGR2GRAY)

# Compute SSIM between the two images
(score, diff) = structural_similarity(before_gray, after_gray, 
full=True)
print("Image Similarity: {:.4f}%".format(score * 100))

# The diff image contains the actual image differences between the 
two images
# and is represented as a floating point data type in the range [0,1]
# so we must convert the array to 8-bit unsigned integers in the 
range
# [0,255] before we can use it with OpenCV
diff = (diff * 255).astype("uint8")
diff_box = cv2.merge([diff, diff, diff])

# Threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | 
cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

mask = np.zeros(before.shape, dtype='uint8')
filled_after = after.copy()

for c in contours:
    area = cv2.contourArea(c)
    if area > 40:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(before, (x, y), (x + w, y + h), (36,255,12), 2)
        cv2.rectangle(after, (x, y), (x + w, y + h), (36,255,12), 2)

cv2.imwrite('before', before)
cv2.imwrite('after', after)
cv2.waitKey()
garbul
  • 1
  • 1
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Blue Robin Feb 27 '23 at 15:34
  • If it's the windows snipping tool then the image is probably only translated. Do you need your solution to work with rotated or scaled images as well? Do you care how long this takes to run? – Ian Chu Mar 01 '23 at 15:13
  • We can assume that the view will be only translated. I don't care how long it takes to run – garbul Mar 02 '23 at 16:40

0 Answers0