This article cuts out only the Code part handled in the article serialized in Note. If you are interested in the technical background, please refer to the Note article.
Since I / O of images is used a lot, I will implement them together as a helper. Doing so will make the Code easier to read and easier to maintain. The library to be used is as follows. Please install it with pip etc. in advance.
Let's describe the actual implementation.
Use glob to create an absolute path list of files with the specified extension in the specified Folder. You can use a relative bus, but since the absolute path is easier to handle later, you will get it with the absolute path.
helper.py
def make_file_path_list(path: str, ext: str) -> [str]:
""" Make file path list from path and ext information
Parameters
-----------
path :str
project path
ext :str
specific the file extention in order to make path list
Returns
-----------
file path list :[str]
list of file path under the path with the specialied extention
"""
path_str = path + '*.' + ext
return glob.glob(path_str)
Next, read the image data based on the obtained list of absolute paths. If it is a PGM file, it does not make much sense, but as a convention, change the RGB sequence. Since OpenCV is read by BGR with Defalt setting, it is set to RGB to maintain consistency with other processing.
helper.py
def read_img_from_path(filepath: [str], width: int, height: int):
""" Read image file from path list
Parameters
----------
filepath :[str]
File list for image reading
width :int
Output image width
height: int
Output iamge height
Returns
----------
Return the read image data as RGB format
"""
stocker = []
for path in filepath:
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, dsize=(width, height), interpolation = cv2.INTER_CUBIC)
stocker.append(img)
return stocker
Let's implement the exporter here as well. The path list to write to, the image data, and the output Format are specified as arguments. If nothing is specified, it will be output in JPEG format.
helper.py
def write_img_to_path(outpath: str, filepaths: [str], imgs: [np.ndarray], ext: str='jpg'):
""" write images to path list
Parameters
-----------
outpath: str
ouput path
filepath: [str]
file path list for file saving
imgs: [np.ndarray]
image data
ext: str
file format and define extention
"""
for idx, filepath in enumerate(filepaths):
# extract file name
filename = filepath.split('/')[-1]; filename = filename.split('.')[0]
# make output file path from filename and extention
output_file_path = outpath + filename + '.' + ext
# output images
cv2.imwrite(output_file_path, imgs[idx])
Later, since it is necessary to judge whether it is a Left image or a Right image, implement a function that cuts out the Left or Right from the image file name and judges it. If it is a Left image, it returns True, and if it is Right, it returns False.
helper.py
def loc_detector_from_name(file_name: str) -> bool:
""" Split location information from file name and return location info
Parameters
-----------
file_name: str
file name which includes location info such as 'left' or 'right'
Returns
-----------
loc: bool
return location info as boolian, True: 'left', False: 'right'
"""
loc = file_name.split('_')[-1]; loc = loc.split('.')[0]
if loc == 'left':
return True
return False
Later, image processing will standardize the pixel values in the range of 0.0 to 1.0, so define a function for that as well.
helper.py
def normarize_img_data(img: np.ndarray, max_value: int = 255):
""" Normarize image data, find min_value and max_value, and normarize
img: np.ndarray
original image data
max_value: int
define maximum value after normarization
"""
# get min, max and range for normarization
min_value = img.min()
max_value = img.max()
value_range = max_value - min_value
# normalized image
norm_img = (img - min_value) / value_range * max_value
# return
return norm_img.astype(np.uint8)
This is the end of halper preparation.
Recommended Posts