Skip to content

Instantly share code, notes, and snippets.

@csbailey5t
Created May 7, 2018 17:23
Show Gist options
  • Save csbailey5t/b869519fb9b0b18a3d03200e2d160841 to your computer and use it in GitHub Desktop.
Save csbailey5t/b869519fb9b0b18a3d03200e2d160841 to your computer and use it in GitHub Desktop.
image-compare for histonets
# coding: utf-8
# In[ ]:
#following code here: https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/
# In[1]:
import cv2
import imutils
from skimage.measure import compare_ssim
import numpy as np
# In[2]:
image1 = cv2.imread('map3-postprocess.png')
image2 = cv2.imread('roads1px-histonets-thinned.png')
image1
# In[6]:
# need to scale map3-postprocess to 2200x2214
# currently, it is 3001x2986
aspectRatio = 3001 / 2986
aspectRatio
# In[7]:
aspect2 = 2200 / 2214
aspect2
# so, the two images have different aspect rations, but let's resize anyway
# In[8]:
image1_resized = cv2.resize(image1, (2200, 2214))
cv2.imwrite('map3-post-resized.png', image1_resized)
# In[9]:
gray1 = cv2.cvtColor(image1_resized, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# In[10]:
(score, diff) = compare_ssim(gray1, gray2, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))
# In[11]:
thresh = cv2.threshold(diff, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts
# In[ ]:
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(image1_resized, (x, y), (x + w, y + h), (0,0,255), 2)
cv2.rectangle(image2, (x, y), (x + w, y + h), (0,0,255), 2)
cv2.imshow("histonets", image1_resized)
cv2.imshow("manual trace", image2)
cv2.imshow("Diff", diff)
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment