前提・実現したいこと
- 画像を分割してサブ画像を作る。(各サブ画像は同じ大きさ)
- サブ画像を処理する。
- サブ画像を再度1枚の画像に戻す。
このとき、ステップ3で生成された1枚の画像には、サブ画像の境界部に境界線ができてしまう。
ステップ1にてに境界部に余裕を持ってサブ画像を生成し、ステップ3では元画像で同じ箇所にあった部分をのりしろのようにして、境界線が生まれないように画像を生成したい。
ステップ1にて分割することは達成できているため、
ステップ3にて結合する際にのりしろを考慮して(重なっている部分は平均画素値をとる、等)結合するコードがあれば教えていただきたいです。
試したこと
のりしろを考えずにサブ画像を結合したコードは以下となります。
import os import glob # from PIL import Image # import pathlib # from pathlib import Path import cv2 import numpy as np seq = "SEQUENCE_NAME" res_before_concat = "SUB_IMAGE_DIRECTORY/" res_after_concat = "CONCAT_IMAGE_DIRECTORY/" frame_num = 300 # 処理する画像枚数 split_num = 64 # ここでは縦横8分割したサブ画像の結合を考える def concat_tile(im_list_2d): return cv2.vconcat([cv2.hconcat(im_list_h) for im_list_h in im_list_2d]) for i in range(1,frame_num+1): num_f ="{:03d}".format(i) frame_name = "Frame "+num_f+".png" for k in range(0,split_num): num_fk ="{:02d}".format(k) path_png = res_before_concat + seq + num_fk + '/'+ frame_name if k == 0: im0 = cv2.imread(path_png) elif k == 1: im1 = cv2.imread(path_png) elif k == 2: im2 = cv2.imread(path_png) elif k == 3: im3 = cv2.imread(path_png) elif k == 4: im4 = cv2.imread(path_png) elif k == 5: im5 = cv2.imread(path_png) elif k == 6: im6 = cv2.imread(path_png) elif k == 7: im7 = cv2.imread(path_png) elif k == 8: im8 = cv2.imread(path_png) elif k == 9: im9 = cv2.imread(path_png) elif k == 10: im10 = cv2.imread(path_png) elif k == 11: im11 = cv2.imread(path_png) elif k == 12: im12 = cv2.imread(path_png) elif k == 13: im13 = cv2.imread(path_png) elif k == 14: im14 = cv2.imread(path_png) elif k == 15: im15 = cv2.imread(path_png) elif k == 16: im16 = cv2.imread(path_png) elif k == 17: im17 = cv2.imread(path_png) elif k == 18: im18 = cv2.imread(path_png) elif k == 19: im19 = cv2.imread(path_png) elif k == 20: im20 = cv2.imread(path_png) elif k == 21: im21 = cv2.imread(path_png) elif k == 22: im22 = cv2.imread(path_png) elif k == 23: im23 = cv2.imread(path_png) elif k == 24: im24 = cv2.imread(path_png) elif k == 25: im25 = cv2.imread(path_png) elif k == 26: im26 = cv2.imread(path_png) elif k == 27: im27 = cv2.imread(path_png) elif k == 28: im28 = cv2.imread(path_png) elif k == 29: im29 = cv2.imread(path_png) elif k == 30: im30 = cv2.imread(path_png) elif k == 31: im31 = cv2.imread(path_png) elif k == 32: im32 = cv2.imread(path_png) elif k == 33: im33 = cv2.imread(path_png) elif k == 34: im34 = cv2.imread(path_png) elif k == 35: im35 = cv2.imread(path_png) elif k == 36: im36 = cv2.imread(path_png) elif k == 37: im37 = cv2.imread(path_png) elif k == 38: im38 = cv2.imread(path_png) elif k == 39: im39 = cv2.imread(path_png) elif k == 40: im40 = cv2.imread(path_png) elif k == 41: im41 = cv2.imread(path_png) elif k == 42: im42 = cv2.imread(path_png) elif k == 43: im43 = cv2.imread(path_png) elif k == 44: im44 = cv2.imread(path_png) elif k == 45: im45 = cv2.imread(path_png) elif k == 46: im46 = cv2.imread(path_png) elif k == 47: im47 = cv2.imread(path_png) elif k == 48: im48 = cv2.imread(path_png) elif k == 49: im49 = cv2.imread(path_png) elif k == 50: im50 = cv2.imread(path_png) elif k == 51: im51 = cv2.imread(path_png) elif k == 52: im52 = cv2.imread(path_png) elif k == 53: im53 = cv2.imread(path_png) elif k == 54: im54 = cv2.imread(path_png) elif k == 55: im55 = cv2.imread(path_png) elif k == 56: im56 = cv2.imread(path_png) elif k == 57: im57 = cv2.imread(path_png) elif k == 58: im58 = cv2.imread(path_png) elif k == 59: im59 = cv2.imread(path_png) elif k == 60: im60 = cv2.imread(path_png) elif k == 61: im61 = cv2.imread(path_png) elif k == 62: im62 = cv2.imread(path_png) elif k == 63: im63 = cv2.imread(path_png) im_tile = concat_tile([[im0, im1, im2, im3, im4, im5, im6, im7], [im8, im9, im10, im11, im12, im13, im14, im15], [im16, im17, im18, im19, im20, im21, im22, im23], [im24, im25, im26, im27, im28, im29, im30, im31], [im32, im33, im34, im35, im36, im37, im38, im39], [im40, im41, im42, im43, im44, im45, im46, im47], [im48, im49, im50, im51, im52, im53, im54, im55], [im56, im57, im58, im59, im60, im61, im62, im63] ]) cv2.imwrite(res_after_concat + seq + "/" +frame_name, im_tile) print(frame_name)
結果画像
上記コードの結果画像です。
境界線部分を拡大した結果画像となります。ピンクで囲ってあるところが境界線が見えてしまっている例としてわかりやすいかと思います。
回答2件
あなたの回答
tips
プレビュー