Compare commits

...

2 Commits

Author SHA1 Message Date
Benjamin Loison
d7f7728211
Add Denoiser Enum 2024-05-03 03:49:15 +02:00
Benjamin Loison
9fd44debf5
Remove implicit getColorMeans denoiser argument 2024-05-03 03:39:57 +02:00
4 changed files with 30 additions and 23 deletions

View File

@ -3,7 +3,7 @@
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from utils import denoise, iterativeMean, getColorChannel, escapeFilePath, Color, mergeSingleColorChannelImagesAccordingToBayerFilter, rescaleRawImageForDenoiser, updateExtremes, saveNpArray, getColorMeans, getImageCrop
from utils import denoise, iterativeMean, getColorChannel, escapeFilePath, Color, mergeSingleColorChannelImagesAccordingToBayerFilter, rescaleRawImageForDenoiser, updateExtremes, saveNpArray, getColorMeans, getImageCrop, Denoiser
import sys
import os
import random
@ -12,7 +12,7 @@ sys.path.insert(0, '../../algorithms/distance/')
from rms_diff import rmsDiffNumpy
DENOISER = 'mean'
DENOISER = Denoiser.MEAN
IMAGES_CAMERAS_FOLDER = {
'RAISE': 'flat-field/nef',
'Rafael 23/04/24': 'rafael/230424',
@ -68,13 +68,13 @@ def getMultipleColorsImage(singleColorChannelImages):
return multipleColorsImage
def getImagePrnuEstimateNpArray(singleColorChannelImages, multipleColorsImage, camera):
singleColorChannelDenoisedImages = {color: denoise(singleColorChannelImages[color], DENOISER) if DENOISER != 'mean' else cameraColorMeans[camera][color] for color in Color}
singleColorChannelDenoisedImages = {color: denoise(singleColorChannelImages[color], DENOISER) if DENOISER != Denoiser.MEAN else cameraColorMeans[camera][color] for color in Color}
multipleColorsDenoisedImage = mergeSingleColorChannelImagesAccordingToBayerFilter(singleColorChannelDenoisedImages)
imagePrnuEstimateNpArray = multipleColorsImage - multipleColorsDenoisedImage
return imagePrnuEstimateNpArray
imagesCamerasFilePaths = {camera: [f'{IMAGES_CAMERAS_FOLDER[camera]}/{imagesCamerasFileName}' for imagesCamerasFileName in imagesCamerasFileNames[camera]] for camera in imagesCamerasFileNames}
cameraColorMeans = {camera: getColorMeans(imagesCamerasFilePaths[camera], Color, DENOISER, minimalColorChannelCameraResolution) for camera in imagesCamerasFilePaths}
cameraColorMeans = {camera: getColorMeans(imagesCamerasFilePaths[camera], Color, minimalColorChannelCameraResolution) for camera in imagesCamerasFilePaths}
from utils import silentTqdm
#tqdm = silentTqdm

View File

@ -4,14 +4,12 @@ import numpy as np
import os
from tqdm import tqdm
import csv
from utils import Color, denoise, iterativeMean, escapeFilePath, saveNpArray, getColorMeans, getImageNpArray
from utils import Color, denoise, iterativeMean, escapeFilePath, saveNpArray, getColorMeans, getImageNpArray, Denoiser
import matplotlib.pyplot as plt
IMAGES_FOLDER_PATH = 'rafael/230424'
imagesFolderPathFileName = escapeFilePath(IMAGES_FOLDER_PATH)
# Among:
# `denoise` possible denoisers and `mean`.
DENOISER = 'mean'
DENOISER = Denoiser.WAVELET
RAISE_NOT_FLAT_FIELDS = False
# `[Color.RED, Color.GREEN_RIGHT, ...]` or `Color` or `[None]` for not raw images.
@ -61,14 +59,14 @@ def treatImage(imageFileName, computeExtremes = False, color = None):
imageNpArray = getImageNpArray(imageFilePath, computeExtremes, color, DENOISER)
if imageNpArray is None:
return
if DENOISER != 'mean':
if DENOISER != Denoiser.MEAN:
imageDenoisedNpArray = denoise(imageNpArray, DENOISER)
else:
imageDenoisedNpArray = colorMeans[color]
imageNoiseNpArray = imageNpArray - imageDenoisedNpArray
estimatedPrnuIterativeMean.add(imageNoiseNpArray)
if (minColor is None or maxColor is None) and DENOISER != 'mean':
if (minColor is None or maxColor is None) and DENOISER != Denoiser.MEAN:
# Assuming same intensity scale across color channels.
for imageFileName in tqdm(imagesFileNames, 'Computing extremes of images'):
for color in COLORS:
@ -79,8 +77,8 @@ if (minColor is None or maxColor is None) and DENOISER != 'mean':
print(f'{minColor=}')
print(f'{maxColor=}')
if DENOISER == 'mean':
colorMeans = getColorMeans(imagesFileNames, COLORS, DENOISER)
if DENOISER == Denoiser.MEAN:
colorMeans = getColorMeans(imagesFileNames, COLORS)
for color in Color:
colorMeans[color] = colorMeans[color]
fileName = f'mean_{imagesFolderPathFileName}_{color}'

View File

@ -3,7 +3,7 @@
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from utils import denoise, iterativeMean, getColorChannel, escapeFilePath, Color, mergeSingleColorChannelImagesAccordingToBayerFilter, rescaleRawImageForDenoiser, updateExtremes, saveNpArray
from utils import denoise, iterativeMean, getColorChannel, escapeFilePath, Color, mergeSingleColorChannelImagesAccordingToBayerFilter, rescaleRawImageForDenoiser, updateExtremes, saveNpArray, Denoiser
import sys
import os
import random
@ -13,7 +13,7 @@ sys.path.insert(0, '../../algorithms/distance/')
from rms_diff import rmsDiffNumpy
NUMBER_OF_SUBGROUPS = 2
DENOISER = 'wavelet'
DENOISER = Denoiser.WAVELET
IMAGES_FOLDER = 'flat-field/NEF'
setting = escapeFilePath(IMAGES_FOLDER) + f'_{DENOISER}'

View File

@ -18,19 +18,28 @@ class Color(Enum):
def __str__(self):
return self.name.lower()
class Denoiser(Enum):
WAVELET = auto()
BILATERAL = auto()
TV_CHAMBOLLE = auto()
MEAN = auto()
def __str__(self):
return self.name.lower()
# Among:
# - `wavelet`
# - `bilateral`
# - `tv_chambolle`
def denoise(imageNpArray, denoiserName):
skImageRestorationDenoise = getattr(skimage.restoration, f'denoise_{denoiserName}')
def denoise(imageNpArray, denoiser):
skImageRestorationDenoise = getattr(skimage.restoration, f'denoise_{denoiser}')
match denoiserName:
case 'wavelet':
match denoiser:
case Denoiser.WAVELET:
imageDenoisedNpArray = skImageRestorationDenoise(imageNpArray, rescale_sigma=True)
case 'bilateral':
case Denoiser.BILATERAL:
imageDenoisedNpArray = skImageRestorationDenoise(imageNpArray, sigma_color=0.05, sigma_spatial=15)
case 'tv_chambolle':
case Denoiser.TV_CHAMBOLLE:
imageDenoisedNpArray = skImageRestorationDenoise(imageNpArray, weight=0.2)
return imageDenoisedNpArray
@ -132,12 +141,12 @@ def updateExtremes(imageNpArray, minColor, maxColor):
def print(*toPrint):
__builtin__.print(datetime.now(), *toPrint)
def getColorMeans(imagesFileNames, colors, denoiser, singleColorChannelCropResolution = None):
def getColorMeans(imagesFileNames, colors, singleColorChannelCropResolution = None):
colorMeans = {}
for color in colors:
colorIterativeMean = iterativeMean()
for imageFileName in tqdm(imagesFileNames, f'Computing mean of {color} colored images'):
imageNpArray = getImageNpArray(imageFileName, False, color, denoiser)
imageNpArray = getImageNpArray(imageFileName, False, color, Denoiser.MEAN)
if singleColorChannelCropResolution is not None:
imageNpArray = getImageCrop(imageNpArray, singleColorChannelCropResolution)
imageNpArray = gaussian_filter(imageNpArray, sigma = 5)
@ -153,7 +162,7 @@ def getImageNpArray(imageFilePath, computeExtremes, color, denoiser):
minColor, maxColor = updateExtremes(imageNpArray, minColor, maxColor)
return
if isARawImage(imageFilePath) and denoiser != 'mean':
if isARawImage(imageFilePath) and denoiser != Denoiser.MEAN:
imageNpArray = rescaleRawImageForDenoiser(imageNpArray, minColor, maxColor)
# Pay attention to range of values expected by the denoiser.
# Indeed if provide the thousands valued raw image, then the denoiser only returns values between 0 and 1 and making the difference between both look pointless.