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)
elif average == 50: if nonzero == 'nonzero': img_zero[i] += trimmed_img_r[i] * ( 41.91545399748533 ) + trimmed_img_g[i] * ( -16.741254808432817 ) + trimmed_img_b[i] * ( -1.5077411437411694 ) + 1105.76905428 # 0を含まないcsvデータ # R2 = 0.9682417600126562 elif nonzero == 'haszero': img_zero[i] += trimmed_img_r[i] * ( 55.487685006950414 ) + trimmed_img_g[i] * ( 3.0853904034376267 ) + trimmed_img_b[i] * ( -24.527991908982145 ) + 922.35833948 # 0を含むcsvデータ # R2 = 0.9624052034513891 ic(img_zero) # twocolor_CMOS = tc.tocolor(img_zero).reshape(height, width, 3) 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(dir_name + save_name) # pil_img.save('/Users/paix/Desktop/Python_lab/calibration/Multiple_regression_analysis/data/temp/CMOS_raw_int8_RGBmin' + str(average) + afbf + str(threshold) + save_name)
# 色データから温度データに変換 ###### temperature_np = tc.totemperature(trimmed_img) temperature_sum += temperature_np total_frame_number = end_frame_number - begin_frame_number + 1 temperature_ave = temperature_sum / total_frame_number TEMP = temperature_ave # 1次元配列 TEMPplt = temperature_ave.reshape(90 * 120) # 2次元配列 # # 温度ヒストグラム表示 # plt.hist(TEMPplt, color="red", bins=128) # plt.ylim(0, 200) # plt.xlim(1300, 3000) # plt.show() ic(temperature_ave.shape) # # 温度データから色データに再変換 img_ave = tc.tocolor(temperature_ave) # 平均化した溶融池温度分布の色画像を表示 img_ave = np.array(img_ave, dtype=np.int8) pil_img = Image.fromarray(img_ave, mode="RGB") save_name = str(laser_power) + "_" + str(total_frame_number) + ".bmp" pil_img.save('calibration/time_average/colorimg/' + save_name) # 温度の時間平均算出終了 # # キャリブレーション # if laser_power == 1400: # img_CMOS = np.array(Image.open('/Users/paix/Desktop/Python_lab/frame_data/20201123/10/afterper2/1400/img_181.bmp')) # elif laser_power == 1600: # img_CMOS = np.array(Image.open('/Users/paix/Desktop/Python_lab/frame_data/20201123/10/afterper2/1600/img_164.bmp')) # elif laser_power == 1800: # img_CMOS = np.array(Image.open('/Users/paix/Desktop/Python_lab/frame_data/20201123/10/afterper2/1800/img_177.bmp'))
def timeAverage(laser_power, middle_frame_number, frames): """ 二色温度計画像を時間平均する Parameters ---------- laser_power : int レーザ出力 begin_frame_number : int 平均するフレームの開始点 end_frame_number : int 平均するフレームの終了点 Returns ------- TEMP : numpyの二次元配列 二色温度計の時間平均値 """ # pathの設定 total_frame_number = frames + 1 directory_name = "/Users/paix/Desktop/Python_lab/frame_data/20210706/pyrometer_short/" base_name = directory_name + str(laser_power) + "/" + str( laser_power) + "0OUT" os.makedirs('frame_data/20210706/colorimg/', exist_ok=True) color_img_path = 'frame_data/20210706/colorimg/' + str( laser_power) + "_" + str(total_frame_number) + ".bmp" if os.path.exists(color_img_path): # if False: img_pil = Image.open(color_img_path) img_np = np.array(img_pil) temperature_ave = tc.totemperature(img_np) TEMP = temperature_ave # 1次元配列 TEMPplt = temperature_ave.reshape(90 * 120) # 2次元配列 # # 温度ヒストグラム表示 # plt.hist(TEMPplt, color="red", bins=128) # plt.ylim(0, 200) # plt.xlim(1300, 3000) # plt.xlabel("temperature") # plt.show() else: # 温度の時間平均算出開始 # 1枚目の溶融池画像を格納 begin_frame_number = middle_frame_number - int(frames / 2) end_frame_number = middle_frame_number + int(frames / 2) img_pil = Image.open(base_name + str(begin_frame_number) + ".BMP") img_np = np.array(img_pil) rough_trimmed_img = tr.img_trimming(img_np, 240, 330, 640, 1279) x, y = gp.get_gravitypoint_img(rough_trimmed_img) trimmed_img = rough_trimmed_img[:, x - 60:x + 60] # 温度データに変換 ###### temperature_sum = tc.totemperature(trimmed_img) for frame_number in range(begin_frame_number + 1, end_frame_number + 1): ic(frame_number) # 1枚ずつ重心周りでトリミングしていく img_pil = Image.open(base_name + str(frame_number) + ".BMP") img_np = np.array(img_pil) rough_trimmed_img = tr.img_trimming(img_np, 240, 330, 640, 1279) x, y = gp.get_gravitypoint_img(rough_trimmed_img, False) # うまくいった # trimmed_img = rough_trimmed_img[ : , x - 60 : x + 60] trimmed_img = img_np[240 + y - 45:240 + y + 45, 640 + x - 60:640 + x + 60] # 色データから温度データに変換 ###### temperature_np = tc.totemperature(trimmed_img) temperature_sum += temperature_np total_frame_number = end_frame_number - begin_frame_number + 1 temperature_ave = temperature_sum / total_frame_number TEMP = temperature_ave # 1次元配列 TEMPplt = temperature_ave.reshape(90 * 120) # 2次元配列 # # 温度ヒストグラム表示 # plt.hist(TEMPplt, color="red", bins=128) # plt.ylim(0, 200) # plt.xlim(1300, 3000) # plt.show() ic(temperature_ave.shape) # # 温度データから色データに再変換 img_ave = tc.tocolor(temperature_ave) # 平均化した溶融池温度分布の色画像を表示 img_ave = np.array(img_ave, dtype=np.int8) pil_img = Image.fromarray(img_ave, mode="RGB") save_name = str(laser_power) + "_" + str(total_frame_number) + ".bmp" pil_img.save('frame_data/20210706/colorimg/' + save_name) # pil_img.save('calibration/time_average/colorimg/20210421/' + save_name) # 温度の時間平均算出終了 return TEMP
trimmed_img = img_CMOS[ y - 45 : y + 45 , x - 60 : x + 60, RGBvalue] img_zero = np.zeros(90*120) threshold = 40 img_red = trimmed_img[:,:].reshape(90*120) ret, img_thresh = cv2.threshold(img_red, threshold, 255, cv2.THRESH_BINARY) trimmed_img = trimmed_img.reshape(90*120) for i in img_thresh.nonzero(): if RGBvalue == 0: img_zero[i] += trimmed_img[i] * 5.08 + 1510.8 elif RGBvalue == 1: img_zero[i] += trimmed_img[i] * 4.06 + 1536.9 elif RGBvalue == 2: img_zero[i] += trimmed_img[i] * 3.33 + 1566.77 ic(img_zero) twocolor_CMOS = tc.tocolor(img_zero).reshape(90, 120, 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(RGB) + ".bmp" pil_img.save('calibration/time_average/colorimg/CMOS_int8_' + save_name) # RGBvalue = 1 # CMOS = trimmed_img[:, :, RGBvalue] # height = 90 # width = 120 # RGBmin = 20 # RGBmax = 255