diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1fa8bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.tif +*.pdf diff --git a/README.md b/README.md new file mode 100644 index 0000000..aeee88a --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# TCCPrinting + +舌カラーチャートの生成 + +### 使い方 + +指定 L*a*b\* から sRGB へ変換してカラーチャート画像を生成 + +### 出力画像 + +- tcc#.tif カラーチャート +- tcc#full.tif TIAS 用カラーチャート + +### 印刷色最適化手順 + +1. 目標 Lab を TCC_Lab1.csv に設定 +2. tcc1.tif を生成し,印刷 +3. 印刷チャートを 分光測色計 CM-700d で測色 +4. 目標 Lab と測色 Lab の差分を計算 +5. 設定 Lab を目標 Lab 方向へ 差分/2 だけ変化させて TCC_Lab2.csv に設定 + +- 2.-5.を,測色 Lab が目標 Lab に近づくまで繰り返し +- 24 色の平均色差 0.5 ぐらいまで近づけることができる diff --git a/TCCPrinting.py b/TCCPrinting.py new file mode 100644 index 0000000..919c767 --- /dev/null +++ b/TCCPrinting.py @@ -0,0 +1,183 @@ +import csv +import datetime + +import cv2 +import numpy as np + +checker_size = 150 +margin = 30 + + +# 舌カラーチャートの生成 +def make_tcc(no_str=""): + # Load Lab values from the CSV file + with open(f"TCC_Lab{no_str}.csv", "r") as csvfile: + reader = csv.reader(csvfile) + data = [] + for row in reader: + data.append([float(x) for x in row]) + # print("Lab values") + # print(np.array(data)) + + cols = 4 + rows = 6 + image_w = checker_size * cols + margin * (cols + 1) + image_h = checker_size * rows + margin * (rows + 1) + print("Image size: %dx%d" % (image_w, image_h)) + + lab_image = np.zeros((image_h, image_w, 3), dtype=np.float32) + for c in range(cols): + for r in range(rows): + x = image_w - (c + 1) * (checker_size + margin) + y = margin + r * (checker_size + margin) + idx = c * rows + r + lab_image[y : y + checker_size, x : x + checker_size, 0] = data[idx][0] + lab_image[y : y + checker_size, x : x + checker_size, 1] = data[idx][1] + lab_image[y : y + checker_size, x : x + checker_size, 2] = data[idx][2] + + rgb_image = (cv2.cvtColor(lab_image, cv2.COLOR_LAB2BGR) * 255).astype(np.uint8) + # print(rgb_image.shape) + # print(rgb_image.dtype) + # print(np.max(rgb_image[:,:,0])) + # print(np.max(rgb_image[:,:,1])) + # print(np.max(rgb_image[:,:,2])) + + output_image(rgb_image.copy(), 4, 6, len(data), f"tcc{no_str}.tif", True) + + # ここからTIASカラーチャート用画像の生成 + + full_image = np.full((1545, 810, 3), dtype=np.uint8, fill_value=[255, 255, 255]) + full_image[210 : 210 + image_h, 30 : 30 + image_w, :] = rgb_image + + aruco = cv2.aruco + dictionary = aruco.getPredefinedDictionary(aruco.DICT_4X4_50) + for i in range(2): + marker_id = 40 + i + marker = aruco.generateImageMarker(dictionary, marker_id, 150) + x = 330 + (i // 2) * 200 + y = 30 + (i % 2) * 1335 + full_image[y : y + 150, x : x + 150, 0] = marker + full_image[y : y + 150, x : x + 150, 1] = marker + full_image[y : y + 150, x : x + 150, 2] = marker + x = 50 + (i // 2) * 700 + y = 60 + (i % 2) * 1420 + cv2.putText( + full_image, + "id:%d" % marker_id, + (x, y), + cv2.FONT_HERSHEY_TRIPLEX, + 1, + (0, 0, 0), + 1, + cv2.LINE_AA, + ) + + cut_color = (200, 200, 200) + points = [ + (300, 0), + (300, 180), + (0, 180), + (0, 1350), + (330, 1544), + (480, 1544), + (809, 1350), + (809, 180), + (510, 180), + (510, 0), + (330, 0), + ] + for i in range(len(points)): + cv2.line(full_image, points[i], points[(i + 1) % len(points)], cut_color, 2) + + date_str = datetime.datetime.now().strftime("%Y.%m.%d") + cv2.putText( + full_image, + f"Tongue Color Checker, Chiba University {date_str}", + (45, 1345), + cv2.FONT_HERSHEY_TRIPLEX, + 0.8, + (0, 0, 0), + 1, + cv2.LINE_AA, + ) + + cv2.putText( + full_image, + "54x103 mm", + (600, 1520), + cv2.FONT_HERSHEY_TRIPLEX, + 0.9, + (0, 0, 0), + 1, + cv2.LINE_AA, + ) + + cv2.imwrite(f"tcc{no_str}full.tif", full_image) + + +# 指定RGB値とその近似色の色票を生成 +def make_test(bc, gc, rc, step): + rgb = [] + for bb in range(bc - step, bc + step + 1, step): + for gg in range(gc - step, gc + step + 1, step): + for rr in range(rc - step, rc + step + 1, step): + rgb.append([bb, gg, rr]) + # print(np.array(rgb)) + + cols = 4 + rows = 7 + image_w = checker_size * cols + margin * (cols + 1) + image_h = checker_size * rows + margin * (rows + 1) + print("Image size: %dx%d" % (image_w, image_h)) + + image = np.zeros((image_h, image_w, 3), dtype=np.uint8) + for c in range(cols): + for r in range(rows): + x = image_w - (c + 1) * (checker_size + margin) + y = margin + r * (checker_size + margin) + idx = c * rows + r + if idx < len(rgb): + image[y : y + checker_size, x : x + checker_size, 0] = rgb[idx][0] + image[y : y + checker_size, x : x + checker_size, 1] = rgb[idx][1] + image[y : y + checker_size, x : x + checker_size, 2] = rgb[idx][2] + + output_image( + image, cols, rows, len(rgb), "test%03d_%03d_%03d_%d.png" % (bc, gc, rc, step) + ) + + +# 色票画像を出力 +def output_image(rgb_image, cols, rows, num, filename, output_values=False): + rgb = np.zeros((num, 3), dtype=np.uint8) + for c in range(cols): + for r in range(rows): + idx = c * rows + r + if idx < num: + x = rgb_image.shape[1] - (c + 1) * (checker_size + margin) + y = margin + r * (checker_size + margin) + val = rgb_image[y + 1, x + 1, :] + rgb[idx, :] = val + if output_values: + cv2.putText( + rgb_image, + "b%3d, g%3d, r%3d" % (val[0], val[1], val[2]), + (x, y - 5), + cv2.FONT_HERSHEY_SIMPLEX, + 0.5, + (255, 255, 255), + 1, + cv2.LINE_AA, + ) + + print("BGR values") + print(rgb) + + # cv2.imshow("Image", rgb_image) + # cv2.waitKey(0) + cv2.imwrite(filename, rgb_image) + + +# メイン +if __name__ == "__main__": + make_tcc("6") # チャートを作るLab値ファイル TCC_Lab#.csv #の数値を指定 + # make_test(92, 110, 181, 5) diff --git a/TCC_Lab6.csv b/TCC_Lab6.csv new file mode 100644 index 0000000..dc8c6bd --- /dev/null +++ b/TCC_Lab6.csv @@ -0,0 +1,24 @@ +61.59245,24.84225,23.37915 +52.801775,33.2208,19.48965 +47.1452,33.476975,17.806175 +44.02715,26.712,13.1965 +58.303525,30.986325,15.135275 +55.31015,22.438025,6.46885 +42.098375,15.313425,4.853175 +64.8964,8.159075,6.154925 +44.84305,7.598625,1.391725 +59.539025,19.997125,12.92195 +71.6761,5.356575,17.757075 +62.543,14.27845,25.846025 +95.4251,-0.9608,6.905825 +66.811925,0.2629,3.10045 +57.80755,-0.019025,1.742625 +48.8722,-0.51145,0.2776 +40.7334,-0.632875,0.983675 +33.753825,-1.37905,1.6988 +51.062725,-1.7849,-23.1725 +65.1432,-34.36815,33.045275 +55.61505,40.486625,26.831225 +85.1572,-10.682,52.355875 +63.289875,35.6266,-2.4749 +68.86625,-17.9355,-19.800875 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0dd006b --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +opencv-python