Created
February 27, 2017 08:11
-
-
Save Gotoryoo/9aacda5470c5197a0bb1e641ace6cfbd to your computer and use it in GitHub Desktop.
gridのネガの写真から○を抽出し、一枚の画像にするpythonコードである。
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
| import cv2 | |
| import numpy as np | |
| import itertools | |
| import re | |
| import glob | |
| #cv2.namedWindow('trim') | |
| markrad = 30 | |
| template = np.zeros((markrad*2, markrad*2), np.uint8) | |
| cv2.circle(template, (markrad,markrad), markrad, 255, -1) | |
| grids = np.zeros((markrad*2*5, markrad*2*1), np.uint8) | |
| canvas = np.zeros((600, 300), np.uint8) | |
| #cv2.imshow('grid',template) | |
| #cv2.waitKey(0) | |
| #cv2.destroyAllWindows() | |
| gridlist = [] | |
| path = 'C:\\Users\\GTR\\Documents\\lab_log\\log\\H29_2\\20170209\\nega_pic\\' | |
| #gridname = open(path + 'ver2_gridpoint_stage_type2.txt','r') | |
| files = glob.glob("C:\\Users\\GTR\\Documents\\lab_log\\log\\H29_2\\20170209\\nega_pic\\ver2_20170208_namechange\\*.png") | |
| rowlist = (5,15,25,35,45) | |
| collist = (0,10,20,30,40) | |
| for c in range(len(collist)): | |
| grids = np.zeros((markrad*2*1, markrad*2*5), np.uint8) | |
| for r in range(len(rowlist)): | |
| #for c,r in itertools.product(range(len(collist)), range(len(rowlist))): | |
| print c,r | |
| for file in files: | |
| path = file | |
| #print path | |
| itemlist = path.split('_') | |
| num = len(itemlist) | |
| #print num | |
| x_p = float(itemlist[6]) | |
| rx = round(x_p) | |
| y_p = float(itemlist[7]) | |
| ry = round(y_p) | |
| #print rx,ry | |
| damy_x = int(rx + 15) | |
| damy_y = int(abs(ry)) | |
| #print damy_x,damy_y | |
| if rowlist[r-1] == damy_x and collist[c- 1] == damy_y: | |
| #print path | |
| #print rx,ry | |
| #print damy_x,damy_y | |
| aaa = cv2.imread(file,cv2.CV_LOAD_IMAGE_GRAYSCALE) | |
| aaa_c = aaa.copy() | |
| res = cv2.matchTemplate(aaa,template,eval("cv2.TM_CCORR_NORMED")) | |
| min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) | |
| top_left = max_loc | |
| 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, x_p, y_p) | |
| #print strings | |
| #cv2.imwrite('grids_ver1_{0}_{1}.png'.format(x_p,y_p), aaa_c[y:y+h,x:x+w].copy()) | |
| #grids[0*h:1*h, r*w:(r+1)*w] = aaa_c[y:y+h,x:x+w].copy() | |
| w_g, h_g = grids.shape[::-1] | |
| c_can = 2*c + 1 | |
| #canvas[c_can*h_g:(c_can+1)*h_g,20: 20 + w_g]= grids.copy() | |
| rowlist = (0,10,20,30,40) | |
| collist = (5,15,25,35,45) | |
| for c in range(len(collist)): | |
| grids = np.zeros((markrad*2*1, markrad*2*5), np.uint8) | |
| for r in range(len(rowlist)): | |
| #for c,r in itertools.product(range(len(collist)), range(len(rowlist))): | |
| print c,r | |
| for file in files: | |
| path = file | |
| #print path | |
| itemlist = path.split('_') | |
| num = len(itemlist) | |
| #print num | |
| x_p = float(itemlist[6]) | |
| rx = round(x_p) | |
| y_p = float(itemlist[7]) | |
| ry = round(y_p) | |
| #print rx,ry | |
| damy_x = int(rx + 15) | |
| damy_y = int(abs(ry)) | |
| #print damy_x,damy_y | |
| if rowlist[r-1] == damy_x and collist[c- 1] == damy_y: | |
| #print path | |
| #print rx,ry | |
| #print damy_x,damy_y | |
| aaa = cv2.imread(file,cv2.CV_LOAD_IMAGE_GRAYSCALE) | |
| aaa_c = aaa.copy() | |
| res = cv2.matchTemplate(aaa,template,eval("cv2.TM_CCORR_NORMED")) | |
| min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) | |
| top_left = max_loc | |
| 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, x_p, y_p) | |
| #print strings | |
| #cv2.imwrite('grids_ver1_{0}_{1}.png'.format(x_p,y_p), aaa_c[y:y+h,x:x+w].copy()) | |
| #grids[0*h:1*h, r*w:(r+1)*w] = aaa_c[y:y+h,x:x+w].copy() | |
| w_g, h_g = grids.shape[::-1] | |
| c_can = 2*c | |
| #canvas[c_can*h_g:(c_can+1)*h_g,0:w_g]= grids.copy() | |
| #cv2.imwrite('grids_ver1.bmp', canvas) | |
| cv2.destroyAllWindows() | |
| ''' | |
| rowlist = (0,10,20,30,40) | |
| collist = (5,15,25,35,45) | |
| for c,r in itertools.product(range(len(collist)), range(len(rowlist))): | |
| #print c,r | |
| a = 0 | |
| ''' | |
| for file in files: | |
| path = file | |
| #print path | |
| itemlist = path.split('_') | |
| num = len(itemlist) | |
| #print num | |
| x_p = float(itemlist[6]) | |
| rx = round(x_p) | |
| y_p = float(itemlist[7]) | |
| ry = round(y_p) | |
| #print rx,ry | |
| damy_x = int(rx + 15) | |
| damy_y = int(abs(ry)) | |
| #print damy_x,damy_y | |
| damy_x = 0 | |
| damy_y = 0 | |
| aaa = cv2.imread(file,cv2.CV_LOAD_IMAGE_GRAYSCALE) | |
| aaa_c = aaa.copy() | |
| ret,thre = cv2.threshold(aaa, 50 , 255, cv2.THRESH_BINARY) | |
| #cv2.imshow('aaa',aaa) | |
| #cv2.waitKey(0) | |
| #cv2.destroyAllWindows() | |
| # rev = np.zeros_like(aaa) | |
| # cv2.bitwise_not(aaa,rev) | |
| # cv2.imshow('rev',rev) | |
| # cv2.waitKey(0) | |
| # cv2.destroyAllWindows() | |
| res = cv2.matchTemplate(thre,template,eval("cv2.TM_CCORR_NORMED")) | |
| min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) | |
| top_left = max_loc | |
| 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, x_p, y_p) | |
| #print strings | |
| cv2.imwrite('grids_ver2_{0}_{1}.png'.format(x_p,y_p), aaa_c[y:y+h,x:x+w].copy()) | |
| #grids[damy_y*h:(damy_y+1)*h, damy_x*w:(damy_x+1)*w] = aaa_c[y:y+h,x:x+w].copy() | |
| #cv2.imwrite('grids.bmp', canvas) | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment