Skip to content

Instantly share code, notes, and snippets.

@bauripalash
Created May 6, 2020 13:03
Show Gist options
  • Save bauripalash/5a08763202eab550506dbd25b0c3b818 to your computer and use it in GitHub Desktop.
Save bauripalash/5a08763202eab550506dbd25b0c3b818 to your computer and use it in GitHub Desktop.
imDiffBug
import cv2
import numpy as np
#from skimage.measure import compare_ssim
from skimage.metrics import structural_similarity
import click
@click.command()
@click.option("-o" , "--output" , default = None , type=str , help = "Output File")
@click.argument("image1" , type = str)
@click.argument("image2" , type = str)
def cli(output , image1 , image2):
x_img = cv2.imread(image1)
y_img = cv2.imread(image2)
x_gray = cv2.cvtColor(x_img , cv2.COLOR_BGR2GRAY)
y_gray = cv2.cvtColor(y_img , cv2.COLOR_BGR2GRAY)
(ssim_score , df) = structural_similarity(x_gray , y_gray , full= True)
df = (df*255).astype("uint8")
threshold = cv2.threshold(df , 0 , 255 , cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(threshold.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
mask = np.zeros(x_img.shape , dtype="uint8")
filled_y = y_img.copy()
for c in contours:
area = cv2.contourArea(c)
if area >= 15:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(x_img, (x, y), (x + w, y + h), (12,255,12), 2)
cv2.rectangle(y_img, (x, y), (x + w, y + h), (12,255,12), 2)
result = np.concatenate((x_img , y_img) , axis = 1)
if not output:
cv2.imshow("RESULT" , result)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
cv2.imwrite(output , result)
cli()
opencv-python
numpy
click
scikit-image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment