bmp 이미지는 b, g, r 순서로 저장되어 있어 나눌때
b, g, r = cv2.split(bgr_image)로 나눠야 한다.
import cv2
import os
import matplotlib.pyplot as plt
output_folder = "./output_split/"
from os import listdir
from os.path import isfile, join
# cwd = os.getcwd()
cwd = "D:\Projects\testimages"
onlyfiles = [os.path.join(cwd, f) for f in os.listdir(cwd) if
os.path.isfile(os.path.join(cwd, f))]
for filename in onlyfiles:
print(filename)
head, tail = os.path.split(filename)
filename = tail
filename, file_extension = os.path.splitext(filename)
print(filename)
# filename = "testfilename"
file_path = cwd + "/" + filename + ".bmp"
# get bmp image and show
bgr_image = cv2.imread(file_path)
# bmp image split to Red Image, Green Image, Blue Image
b, g, r = cv2.split(bgr_image)
#os.mkdir(output_folder)
# save Red Image, Green Image, Blue Image jpg
cv2.imwrite(os.path.join(output_folder, filename + "_R.bmp"), r)
cv2.imwrite(os.path.join(output_folder, filename + "_G.bmp"), g)
cv2.imwrite(os.path.join(output_folder, filename + "_B.bmp"), b)
파일 경로명은 각자 알맞게 수정해서 사용하기
plt.imshow(r)
plt.title("Red Image")
plt.show()