Skip to content

Instantly share code, notes, and snippets.

@MasazI
Created September 14, 2017 08:28
Show Gist options
  • Select an option

  • Save MasazI/00c7315ef4c7848ef81e1209bafa90ac to your computer and use it in GitHub Desktop.

Select an option

Save MasazI/00c7315ef4c7848ef81e1209bafa90ac to your computer and use it in GitHub Desktop.
crop image sample for preprocessing
import cv2
import os
num_movie = 10
for i in range(num_movie):
# movie file dir %03d
dir = "%03d" % (i+1)
print(dir)
files = os.listdir(dir)
for file in files:
img = os.path.join(dir, file)
name, ext = os.path.splitext(img)
if ext != ".png":
continue
print(img)
mat = cv2.imread(img)
# カラーとグレースケールで場合分け
if len(mat.shape) == 3:
height, width, channels = mat.shape[:3]
else:
height, width = mat.shape[:2]
channels = 1
# 取得結果(幅,高さ,チャンネル数,depth)を表示
print("width: " + str(width))
print("height: " + str(height))
print("channels: " + str(channels))
print("dtype: " + str(mat.dtype))
crop_mat = mat[55:300, 5:630]
odir = 'ID%s_crop_ob' % (dir)
if not os.path.exists(odir):
os.mkdir(odir)
opath = os.path.join(odir, file)
cv2.imwrite(opath, crop_mat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment