Skip to content

Instantly share code, notes, and snippets.

@Gotoryoo
Created November 29, 2016 05:30
Show Gist options
  • Save Gotoryoo/114334071ff40b0f8a6699ff456c4160 to your computer and use it in GitHub Desktop.
Save Gotoryoo/114334071ff40b0f8a6699ff456c4160 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Sat May 02 15:29:29 2015
@author: jyoshida-sci
"""
import cv2
import numpy as np
import itertools
#cv2.namedWindow('trim')
markrad = 80
template = np.zeros((markrad*2, markrad*2), np.uint8)
cv2.circle(template, (markrad,markrad), markrad, 1, -1)
grids = np.ones((markrad*2*4, markrad*2*5), np.uint8)*255
rowlist = ([0,1],
[53,54],
[106,107],
[160,161])
collist = (0,9,18,27,36)
img = np.ones((700,2048), np.uint8)*255
f = open('gridmarks.txt', 'w') # 書き込みモードで開く
for c,r in itertools.product(range(len(collist)), range(len(rowlist))):
file0 = "{0:03}-{1:03}-000.bmp".format(collist[c],rowlist[r][0])
aaa = cv2.imread(file0,cv2.CV_LOAD_IMAGE_GRAYSCALE)
img[0:358,0:2048] = aaa
file1 = "{0:03}-{1:03}-000.bmp".format(collist[c],rowlist[r][1])
bbb = cv2.imread(file1,cv2.CV_LOAD_IMAGE_GRAYSCALE)
img[336:336+358,0:2048] = bbb
rev = np.zeros_like(img)
cv2.bitwise_not(img,rev)
res = cv2.matchTemplate(rev,template,eval("cv2.TM_CCORR_NORMED"))
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
#print file
#print max_val
#cv2.circle(img, max_loc, 3, 255, -1)
#bottom_right = (top_left[0] + w, top_left[1] + h)
#cv2.rectangle(img,top_left, bottom_right, (0,0,255), 2)
x = top_left[0]
y = top_left[1]
w, h = template.shape[::-1]
cx = x + w/2
cy = y + h/2
strings = "{0} {1} {2} {3}\n".format(cx, cy, collist[c], rowlist[r][0])
print strings
f.write(strings)
grids[r*h:(r+1)*h, c*w:(c+1)*w] = img[y:y+h,x:x+w].copy()
#trim = img[y:y+h,x:x+w].copy()
#cv2.imshow('trim', trim)
#cv2.waitKey(0)
f.close()
cv2.imwrite('grids.bmp', grids)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment