Created
April 24, 2022 08:02
-
-
Save joonas-yoon/4b609339ac99ff23527ba6d8ecfeca1d to your computer and use it in GitHub Desktop.
Generate CLAHE-applied + resized Image for Sorghum -100 Cultivar Identification - FGVC 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import torch | |
from torchvision import transforms as T | |
import PIL | |
import numpy as np | |
import cv2 | |
def tensor2np(tensor): | |
return tensor.permute(1, 2, 0).numpy().astype(np.uint8) | |
def np2tensor(nparr): | |
return torch.tensor(nparr).permute(2, 0, 1).float() | |
def CLAHE(image): | |
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) | |
clahe = cv2.createCLAHE(clipLimit=10) | |
hsv[:,:,-1] = clahe.apply(hsv[:,:,-1]) | |
rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) | |
return rgb | |
def makedirs(path): | |
if not os.path.exists(path): | |
os.makedirs(path) | |
################################################################ | |
root_dir = os.path.join('./x') | |
out_dir = os.path.join('./outputs') | |
TARGET_WIDTH_HEIGHT = 256 | |
transform = T.Compose([ | |
np.array, | |
CLAHE, | |
np2tensor, | |
T.Resize(TARGET_WIDTH_HEIGHT), | |
tensor2np, | |
]) | |
makedirs(root_dir) | |
for root, dirs, files in os.walk(root_dir): | |
for f in files: | |
in_path = os.path.join(root, f) | |
out_dir_path = os.path.join(out_dir, root) | |
out_path = os.path.join(out_dir_path, f) | |
makedirs(out_dir_path) | |
pil_image = PIL.Image.open(in_path) | |
np_image = transform(pil_image) | |
pil_image_out = PIL.Image.fromarray(np_image) | |
pil_image_out.save(out_path, 'png') | |
print(in_path, '->', out_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before
After