Skip to content

Instantly share code, notes, and snippets.

@Gotoryoo
Created December 2, 2016 12:49
Show Gist options
  • Save Gotoryoo/a469ce4b77079e63c93d025716a43443 to your computer and use it in GitHub Desktop.
Save Gotoryoo/a469ce4b77079e63c93d025716a43443 to your computer and use it in GitHub Desktop.
積層画像同士でのパターンマッチを行うプログラムです。
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 17 15:27:51 2016
@author: ryousuke
"""
import numpy as np
import cv2
#from array import array
#import re
import pandas as pd
from os.path import join, relpath
from glob import glob
import itertools
import ROOT
import math
ROOT.std.__file__ = 'dummy_for_old_pyastro'
ROOT.gROOT.Reset()
path = "C:\\Users\\GTR\\Documents\\lab_log\\log\\H28_11\\20161117\\beam_info\\GTR_test\\img\\"
orizin = 42
#上側の積層画像から輝度値のまとまりを読み取る。
img_u = cv2.imread(path + "beam_8_thre4_{0}-{1}.png".format(orizin,orizin + 7),0);
cv2.imshow('img_u',img_u*30)
img_c_u = img_u.copy()
threshold = 2
max_bri = 255
ret_u,thre_u = cv2.threshold(img_c_u, threshold, max_bri, cv2.THRESH_BINARY)
cv2.imshow('thre_u',thre_u)
contours_u = cv2.findContours(thre_u, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#下側の積層画像から輝度値のまとまりを読み取る。
img_d = cv2.imread(path + "beam_8_thre4_{0}-{1}.png".format(orizin + 7,orizin + 14),0);
cv2.imshow('img_d',img_d*30)
img_c_d = img_d.copy()
ret_d,thre_d = cv2.threshold(img_c_d, threshold, max_bri, cv2.THRESH_BINARY)
cv2.imshow('thre_d',thre_d)
cv2.waitKey(0)
cv2.destroyAllWindows()
contours_d = cv2.findContours(thre_d, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#cv2.imwrite("beam_8_42-49_bin.png",thre_u)
#cv2.imwrite("beam_8_56-63_bin.png",thre_d)
mom_u = []
#bu = ROOT.TNtuple('bu','beam','cx:cy')
mom_d = []
#bd = ROOT.TNtuple('bd','beam','cx:cy')
#輝度値のまとまりごとに重心を取得する。
for cu in contours_u[0]:
mom = {}
M = cv2.moments(cu)
if M['m00'] != 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
mom['cx'] = int(cx)
mom['cy'] = int(cy)
mom_u.append(mom)
else:
cx = 0
cy = 0
# bu.Fill(cx,cy)
for cd in contours_d[0]:
mom = {}
M = cv2.moments(cd)
if M['m00'] != 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
mom['cx'] = int(cx)
mom['cy'] = int(cy)
mom_d.append(mom)
else:
cx = 0
cy = 0
# mom['cx'] = int(cx)
# mom['cy'] = int(cy)
# mom_d.append(mom)
# bd.Fill(cx,cy)
#取得した重心の座標でパターンマッチを行う。
c1 = ROOT.TCanvas('c1','Example with Formula',200,10,700,700)
h2dydx = ROOT.TH2D("h2dydx",";x[pix];y[pix]",100, -50, 50, 100, -50, 50)
for b_u,b_d in itertools.product(mom_u,mom_u):
# print(b_u,b_d)
#もしかしたら、画像の中心を(0,0)になるようにしなければならないかも
#その場合は、絶対値(abs)を使用する必要あり
r = math.sqrt((b_u['cx'] - b_d['cx'])**2 + (b_u['cy'] - b_d['cy'])**2)
if r > 100:
continue
dx = b_u['cx'] - b_d['cx']
dy = b_u['cy'] - b_d['cy']
h2dydx.Fill(dx,dy)
print 'finish'
h2dydx.Draw('colz')
#c1.Print('Beampattern:{0}-{1} to {2}-{3}.png'.format(orizin,orizin + 7,orizin + 7,orizin + 14))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment