Last active
May 7, 2017 23:04
-
-
Save crackwitz/be6054b62e2ca85cf3cec7d98aaf7c55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import os | |
import sys | |
import numpy as np | |
import cv2 | |
np.set_printoptions(precision=4, suppress=True) | |
def getseam(image, x0=0, y0=0, blur=False): | |
height, width, nch = image.shape | |
# de-gamma, normalize | |
image = (np.float32(image) * (1/255)) ** (2.2) | |
# vertical seams | |
xdiff = cv2.filter2D(cv2.blur(image, ksize=(1,blur or 1)), ddepth=cv2.CV_32F, kernel=np.array([[-1, +1]]), borderType=cv2.BORDER_REPLICATE) | |
xdiff = np.abs(xdiff).sum(axis=2) | |
# horizontal seams | |
ydiff = cv2.filter2D(cv2.blur(image, ksize=(blur or 1,1)), ddepth=cv2.CV_32F, kernel=np.array([[-1], [+1]]), borderType=cv2.BORDER_REPLICATE) | |
ydiff = np.abs(ydiff).sum(axis=2) | |
xseams = xdiff.sum(axis=0) / (nch*height) # vertical seams | |
yseams = ydiff.sum(axis=1) / (nch*width) # horizontal seams | |
xbest = np.argmax(xseams) | |
xscore = xseams[xbest] | |
ybest = np.argmax(yseams) | |
yscore = yseams[ybest] | |
bestaxis = 0 if (xscore >= yscore) else 1 | |
bestscore = [xscore, yscore][bestaxis] | |
bestcoord = [xbest, ybest][bestaxis] | |
return bestscore, "xy"[bestaxis], bestcoord+[x0,y0][bestaxis] | |
def splitroi(rect, axis, coord): | |
(x0,y0,x1,y1) = rect | |
if axis == 'x': # vertical split | |
return ( | |
(x0, y0, coord, y1), | |
(coord, y0, x1, y1) | |
) | |
elif axis == 'y': # horizontal split | |
return ( | |
(x0, y0, x1, coord), | |
(x0, coord, x1, y1) | |
) | |
else: | |
assert False | |
infile = sys.argv[1] | |
image = cv2.imread(infile) # de-gamma? normalize to 1.0? | |
h,w = image.shape[:2] | |
rois = [ (0, 0, w, h) ] | |
minsize = 200 | |
i = 0 | |
while i < len(rois): | |
x0,y0,x1,y1 = roi = rois[i] | |
print roi, ":" | |
#import pdb; pdb.set_trace() | |
score, axis, coord = getseam(image[y0:y1, x0:x1], x0=x0, y0=y0, blur=5) | |
print "seam at {1} = {2}, score {0:.3f}".format(score, axis, coord) | |
subrois = splitroi(roi, axis, coord) | |
#if (score >= 0.15) and all(min(x1-x0, y1-y0) >= minsize for x0,y0,x1,y1 in subrois): | |
if (score >= 0.15) and (min(x1-x0, y1-y0) >= minsize): | |
rois[i:i+1] = subrois | |
else: | |
#print roi | |
rois[i] = rois[i] + (score,) | |
i += 1 | |
print np.array(rois) | |
result = image/255 * 0.5 | |
for roi in rois: | |
x0,y0,x1,y1,score = roi | |
cv2.line(result, (x0,y0), (x1,y1), color=(0,0,1), thickness=3, lineType=cv2.LINE_AA) | |
cv2.line(result, (x0,y1), (x1,y0), color=(0,0,1), thickness=3, lineType=cv2.LINE_AA) | |
cv2.putText(result, text="{:.3f}".format(score), org=(x0, y1), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale=2, color=(1,1,1), thickness=2, lineType=cv2.LINE_AA) | |
cv2.imwrite("{0}-split{1}".format(*os.path.splitext(infile)), (result*255).clip(0, 255)) | |
cv2.namedWindow("result", cv2.WINDOW_NORMAL) | |
cv2.imshow("result", result) | |
cv2.waitKey(0) | |
cv2.destroyWindow("result") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment