Created
August 10, 2018 14:10
-
-
Save Yu-AnChen/ea3cf8c1af452dc975920b5cf741f2f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division, print_function | |
import numpy as np | |
import matplotlib | |
import matplotlib.pyplot as plt | |
from skimage.feature import match_template | |
import skimage.io | |
def getMatchedSubImg(partialImg, fullImg, debug=False): | |
result = match_template(fullImg, partialImg) | |
ij = np.unravel_index(np.argmax(result), result.shape) | |
x, y = ij[::-1] | |
imgShape = partialImg.shape | |
if debug: | |
plotMatcchTemplate(partialImg, fullImg, result) | |
return fullImg[y:y+imgShape[0], x:x+imgShape[1]] | |
def plotMatcchTemplate(partialImg, fullImg, result): | |
ij = np.unravel_index(np.argmax(result), result.shape) | |
x, y = ij[::-1] | |
fig = plt.figure(figsize=(8, 3)) | |
ax1 = plt.subplot(1, 3, 1) | |
ax2 = plt.subplot(1, 3, 2) | |
ax3 = plt.subplot(1, 3, 3, sharey=ax2, sharex=ax2) | |
ax1.imshow(partialImg, cmap=plt.cm.gray) | |
ax1.set_axis_off() | |
ax1.set_title('Partial image') | |
ax2.imshow(fullImg, cmap=plt.cm.gray) | |
ax2.set_axis_off() | |
ax2.set_title('Full image') | |
# highlight matched region | |
hcoin, wcoin = partialImg.shape | |
rect = plt.Rectangle((x, y), wcoin, hcoin, edgecolor='r', facecolor='none') | |
ax2.add_patch(rect) | |
r2 = np.zeros(fullImg.shape) | |
r2[:result.shape[0], :result.shape[1]] += result | |
ax3.imshow(r2) | |
ax3.set_axis_off() | |
ax3.set_title('NCC result') | |
# # highlight matched region | |
ax3.autoscale(False) | |
ax3.plot(x, y, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10) | |
plt.show() | |
return | |
def pixelDiffBinarizedWithGate (img1, img2, gate=5000): | |
return (img1 > gate) != (img2 > gate) | |
def plotBinnedPixelDiff (img, binNumPerRow=50, maxFactor=0.3): | |
rowBase = int(img.shape[0] / binNumPerRow) | |
colBase = int(img.shape[1] / binNumPerRow) | |
gridheat = np.array([[np.sum(img[rowBase*j:rowBase*(j+1), colBase*i:colBase*(i+1)]) for i in range(binNumPerRow)] for j in range(binNumPerRow)]) | |
plt.imshow(gridheat, norm=matplotlib.colors.Normalize(vmin=0, vmax=rowBase*colBase*maxFactor, clip=False)) | |
import os | |
filesPath = '20180810_batch/' | |
stitchTool = 'mist' | |
stitchedImgsPath = '20180810_batch/si_even_mosaic_' + stitchTool + '.tif' | |
files = os.listdir(filesPath) | |
refImgs = [f for f in files if ('mosaic' not in f and 'tif' in f)] | |
stitchedImg = skimage.io.imread(stitchedImgsPath) | |
for fName in refImgs: | |
refImg = skimage.io.imread(filesPath + fName)[:,:,0] | |
matched = getMatchedSubImg(refImg, stitchedImg) | |
skimage.io.imsave(filesPath + stitchTool + '/' + fName.split('.')[0] + '-' + stitchTool + '.tif', matched) | |
diff = pixelDiffBinarizedWithGate(refImg, matched) | |
plotBinnedPixelDiff(diff, 20, 0.3) | |
def plotBinnedPixelDiffAll(refImgs, stitchTool, binNum, factor): | |
result = [] | |
for fName in refImgs: | |
refImg = skimage.io.imread(filesPath + fName)[:,:,0] | |
paredImg = skimage.io.imread(filesPath + stitchTool + '/' + fName.split('.')[0] + '-' + stitchTool + '.tif') | |
print(filesPath + stitchTool + '/' + fName.split('.')[0] + '-' + stitchTool + '.tif') | |
result.append(pixelDiffBinarizedWithGate(refImg, paredImg)) | |
plotBinnedPixelDiff(np.array(result).sum(axis=0)/12, binNum, factor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment