alps / utils /preprocessing.py
yumikimi381's picture
Upload folder using huggingface_hub
daf0288 verified
raw
history blame
1.64 kB
import numpy.typing as npt
from numpy import uint8
import numpy as np
ImageType = npt.NDArray[uint8]
from typing import Tuple, List, Sequence, Optional, Union
import cv2
from PIL import Image
from PIL import ImageEnhance
def denoisingAndSharpening(images:List[ImageType]):
new_images = []
for img in images:
# Apply fastNlMeansDenoisingColored
# Parameters:
# - img: The input 8-bit 3-channel image.
# - None: The output image (in-place if None is passed).
# - h: Parameter regulating filter strength for luminance component. Higher h value removes noise better but also removes image details (10 is a good default value).
# - hForColorComponents: The same as h but for color images only. For most images, 10 will be a good value.
# - templateWindowSize: Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value is 7.
# - searchWindowSize: Size in pixels of the window that is used to compute a weighted average for the given pixel. Should be odd. Recommended value is 21.
h = 10
hForColorComponents = 10
templateWindowSize = 7
searchWindowSize = 21
img = cv2.fastNlMeansDenoisingColored(np.array(img), None, h, hForColorComponents, templateWindowSize, searchWindowSize)
#cv2.imwrite(debug_folder+"denoisedAndHigherContrast.png",img)
img = Image.fromarray(img).convert('RGB')
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(2) # Increase contrast by a factor of 2
new_images.append(img)
return new_images