def CMOStoTwoColor(laser_power, fg, coefs, save_path, threshold=20): """ CMOSカメラ画像→温度配列→二色温度計画像 Parameters ---------- laser_power : int レーザ出力 fg : string フィルタとゲインの組み合わせ coefs : numpy 重回帰式の係数 save_path : string 保存する画像の絶対パス Returns ------- """ height = 300 width = 300 if fg == 'f4-g16': dir_name = "/Users/paix/Desktop/Python_lab/frame_data/20210706/after_projection/f4-g16/" mid_frames = [28, 41, 31, 39, 36] elif fg == 'f4-g24': dir_name = "/Users/paix/Desktop/Python_lab/frame_data/20210706/after_projection/f4-g24/" mid_frames = [32, 38, 32, 26, 57] mid_frame = mid_frames[int((laser_power - 1400) / 100)] img_CMOS = np.array( Image.open(dir_name + str(laser_power) + '/img_' + str(mid_frame) + '.bmp')) x, y = gp.get_gravitypoint_CMOS(img_CMOS, show_gp_img=False, print_gp=False) trimmed_img_r = img_CMOS[y - int(height / 2):y + int(height / 2), x - int(width / 2):x + int(width / 2), 0] trimmed_img_g = img_CMOS[y - int(height / 2):y + int(height / 2), x - int(width / 2):x + int(width / 2), 1] trimmed_img_b = img_CMOS[y - int(height / 2):y + int(height / 2), x - int(width / 2):x + int(width / 2), 2] img_zero = np.zeros(height * width) img_red = trimmed_img_r[:, :].reshape(height * width) ret, img_thresh = cv2.threshold(img_red, threshold, 255, cv2.THRESH_BINARY) trimmed_img_r = trimmed_img_r.reshape(height * width) trimmed_img_g = trimmed_img_g.reshape(height * width) trimmed_img_b = trimmed_img_b.reshape(height * width) for i in img_thresh.nonzero(): img_zero[i] += trimmed_img_r[i] * coefs[0] + trimmed_img_g[i] * coefs[ 1] + trimmed_img_b[i] * coefs[2] + coefs[3] # ic(img_zero) twocolor_CMOS = tc.tocolor(img_zero, height, width).reshape(height, width, 3) # ic(twocolor_CMOS) img_ave = np.array(twocolor_CMOS, dtype=np.int8) pil_img = Image.fromarray(img_ave, mode="RGB") # save_name = str(laser_power) + '_' + str(gain) + '_' + str(average) + '_' + nonzero + "_RGB" + ".bmp" pil_img.save(save_path)
for laser_power in range(1400, 1900, 100): for gain in [16, 24]: for average in range(10, 60, 10): for nonzero in ['nonzero', 'haszero']: if gain == 16: dir_name = "/Users/paix/Desktop/Python_lab/frame_data/20210706/after_projection/f4-g16/" mid_frames = [28, 41, 31, 39, 36] elif gain == 24: dir_name = "/Users/paix/Desktop/Python_lab/frame_data/20210706/after_projection/f4-g24/" mid_frames = [32, 38, 32, 26, 57] mid_frame = mid_frames[int((laser_power - 1400) / 100)] img_CMOS = np.array( Image.open(dir_name + str(laser_power) + '/img_' + str(mid_frame) + '.bmp')) x, y = gp.get_gravitypoint_CMOS(img_CMOS, show_gp_img=False) trimmed_img_r = img_CMOS[y - int(height / 2):y + int(height / 2), x - int(width / 2):x + int(width / 2), 0] trimmed_img_g = img_CMOS[y - int(height / 2):y + int(height / 2), x - int(width / 2):x + int(width / 2), 1] trimmed_img_b = img_CMOS[y - int(height / 2):y + int(height / 2), x - int(width / 2):x + int(width / 2), 2] img_zero = np.zeros(height * width)
laser_power = int(input()) height = 300 width = 300 print('after / before projection?') afbf = input() print('Average range') average = int(input()) dir_name = "/Users/paix/Desktop/Python_lab/frame_data/20210421/CMOS/" + afbf + "_projection/" if laser_power == 1400: img_CMOS = np.array(Image.open(dir_name + '1400/img_72.bmp')) elif laser_power == 1600: img_CMOS = np.array(Image.open(dir_name + '1600/img_86.bmp')) elif laser_power == 1800: img_CMOS = np.array(Image.open(dir_name + '1800/img_95.bmp')) # img_CMOS = np.array(Image.open('/Users/paix/Desktop/Python_lab/dstName.bmp')) x,y = gp.get_gravitypoint_CMOS(img_CMOS) trimmed_img_r = img_CMOS[ y - int(height/2) : y + int(height/2) , x - int(width/2) : x + int(width/2), 0] trimmed_img_g = img_CMOS[ y - int(height/2) : y + int(height/2) , x - int(width/2) : x + int(width/2), 1] trimmed_img_b = img_CMOS[ y - int(height/2) : y + int(height/2) , x - int(width/2) : x + int(width/2), 2] img_zero = np.zeros(height*width) threshold = 20 img_red = trimmed_img_r[:,:].reshape(height*width) ret, img_thresh = cv2.threshold(img_red, threshold, 255, cv2.THRESH_BINARY) trimmed_img_r = trimmed_img_r.reshape(height*width) trimmed_img_g = trimmed_img_g.reshape(height*width) trimmed_img_b = trimmed_img_b.reshape(height*width) for i in img_thresh.nonzero(): if average == 50: img_zero[i] += trimmed_img_r[i]*12.08469623 + trimmed_img_g[i]*0.38873001 + trimmed_img_b[i]*6.39120529 + 649.11480838
def calibration(fg, TEMP, haszero, average, samplenumber, dir_name, mid_frames, laser_power=1600, RGBmin=25, RGBmax=255): """ 二色温度計画像とCMOSカメラ画像の比較をする その結果をcsvファイルに出力する Parameters ---------- laser_power : int レーザ出力 fg : string フィルタとゲインの組み合わせ TEMP : numpyの二次元配列 二色温度計の時間平均値 haszero : bool 温度とRGBの対応データに0を含めるかどうか average : int 平均する温度範囲 samplenumber : int データとして扱うサンプルの数の閾値 dir_name : str 溶融地画像があるディレクトリ mid_frames : numpy 溶融地の真ん中のフレーム Returns ------- csv_path : string 結果を出力したcsvファイルの絶対パス """ height, width = TEMP.shape # キャリブレーション mid_frame = mid_frames[int((laser_power - 1400) / 100)] img_CMOS = np.array( Image.open(dir_name + str(laser_power) + '/img_' + str(mid_frame) + '.bmp')) x, y = gp.get_gravitypoint_CMOS(img_CMOS, show_gp_img=False, print_gp=False) trimmed_img = img_CMOS[y - 45:y + 45, x - 60:x + 60] # import cv2 # cv2.imshow('img', trimmed_img) # cv2.waitKey(0) CMOS_r = trimmed_img[:, :, 0] CMOS_g = trimmed_img[:, :, 1] CMOS_b = trimmed_img[:, :, 2] # CMOS = trimmed_img[:, :, RGBvalue] TEMP = TEMP.reshape(90 * 120) CMOS_r = CMOS_r.reshape(90 * 120)[np.nonzero(TEMP)] CMOS_g = CMOS_g.reshape(90 * 120)[np.nonzero(TEMP)] CMOS_b = CMOS_b.reshape(90 * 120)[np.nonzero(TEMP)] TEMP = TEMP.reshape(90 * 120)[np.nonzero(TEMP)] CMOS_r_new = np.array([]) CMOS_g_new = np.array([]) CMOS_b_new = np.array([]) TEMP_new = np.arange(int(min(TEMP)), int(max(TEMP)) + 1, 5) for temp in range(int(min(TEMP)), int(max(TEMP)) + 1, 5): sum_g = 0 sum_r = 0 sum_b = 0 n_r = 0 n_g = 0 n_b = 0 # for i in range ((height-1)*(width-1)): # for i in range (height+width-1): for i in range(TEMP.size - 1): # if TEMP[i] !=0 and CMOS_r[i] > RGBmin: # if TEMP[i]<temp+average and TEMP[i]>temp-average: # sum_r += CMOS_r[i] # n_r += 1 # if TEMP[i] !=0 and CMOS_g[i] > RGBmin: # if TEMP[i]<temp+average and TEMP[i]>temp-average: # sum_g += CMOS_g[i] # n_g += 1 # if TEMP[i] !=0 and CMOS_b[i] > RGBmin: # if TEMP[i]<temp+average and TEMP[i]>temp-average: # sum_b += CMOS_b[i] # n_b += 1 if TEMP[i] != 0 and CMOS_r[i] > RGBmin: if TEMP[i] < temp + average and TEMP[i] > temp - average: sum_r += CMOS_r[i] n_r += 1 if TEMP[i] != 0 and CMOS_g[i] > RGBmin: if TEMP[i] < temp + average and TEMP[i] > temp - average: sum_g += CMOS_g[i] n_g += 1 if TEMP[i] != 0 and CMOS_b[i] > RGBmin: if TEMP[i] < temp + average and TEMP[i] > temp - average: sum_b += CMOS_b[i] n_b += 1 if n_r > samplenumber: CMOS_r_new = np.append(CMOS_r_new, sum_r / n_r) else: CMOS_r_new = np.append(CMOS_r_new, 0) if n_g > samplenumber: CMOS_g_new = np.append(CMOS_g_new, sum_g / n_g) else: CMOS_g_new = np.append(CMOS_g_new, 0) if n_b > samplenumber: CMOS_b_new = np.append(CMOS_b_new, sum_b / n_b) else: CMOS_b_new = np.append(CMOS_b_new, 0) # xnew = np.append(xnew,sum/n) # nnew = np.append(nnew, n) # xnew = np.append(xnew,0) # nnew = np.append(nnew,0) data = np.vstack([TEMP_new, CMOS_r_new, CMOS_g_new, CMOS_b_new]).T # ic(data) np.savetxt('/Users/paix/Desktop/Python_lab/frame_data/20210706/csv/' + fg + '/' + str(laser_power) + '.csv', data, delimiter=',', fmt='%d') header = ["temperature", "R", "G", "B"] with open( '/Users/paix/Desktop/Python_lab/frame_data/20210706/csv/' + fg + '/' + str(laser_power) + 'to_csv_out.csv', 'w') as f: writer = csv.writer(f) writer.writerow(header) with open('/Users/paix/Desktop/Python_lab/frame_data/20210706/csv/' + fg + '/' + str(laser_power) + '.csv') as fr: reader = csv.reader(fr) writer.writerows(reader) # ヘッダーに "temperature","R","G","B" を追加 # 2つ目 df = pd.read_csv( '/Users/paix/Desktop/Python_lab/frame_data/20210706/csv/' + fg + '/' + str(laser_power) + 'to_csv_out.csv', sep=',') if haszero: df = df[(df.R != 0) | (df.G != 0) | (df.B != 0)] # RGBの全てが0なら削除 else: df = df[(df.R != 0) & (df.G != 0) & (df.B != 0)] # RGBのどれか1つが0なら削除 # df = df[df.temperature > 1900] # df = df[df.temperature < 2400] # print(df) csv_path = '/Users/paix/Desktop/Python_lab/frame_data/20210706/csv/' + fg + '/' + str( laser_power) + 'to_csv_out_out.csv' df.to_csv(csv_path) return csv_path