#417 Filter-Chains
parent
49c1164b8b
commit
1caf4beb72
@ -0,0 +1,17 @@
|
||||
import itertools
|
||||
|
||||
|
||||
class Filter():
|
||||
|
||||
filterChain: list = []
|
||||
|
||||
def __init__(self, filter):
|
||||
|
||||
self.filterChain.append(filter)
|
||||
|
||||
def getFilterChain(self):
|
||||
return self.filterChain
|
||||
|
||||
def getChainYield(self):
|
||||
for fy in itertools.product(*[f.getYield() for f in self.filterChain]):
|
||||
yield fy
|
@ -0,0 +1,162 @@
|
||||
import itertools
|
||||
|
||||
from .filter import Filter
|
||||
|
||||
|
||||
class NlmeansFilter(Filter):
|
||||
|
||||
IDENTIFIER = 'nlmeans'
|
||||
|
||||
DEFAULT_STRENGTH: float = 2.8
|
||||
DEFAULT_PATCH_SIZE: int = 13
|
||||
DEFAULT_CHROMA_PATCH_SIZE: int = 9
|
||||
DEFAULT_RESEARCH_WINDOW: int = 23
|
||||
DEFAULT_CHROMA_RESEARCH_WINDOW: int= 17
|
||||
|
||||
STRENGTH_KEY = 'strength'
|
||||
PATCH_SIZE_KEY = 'patch_size'
|
||||
CHROMA_PATCH_SIZE_KEY = 'chroma_patch_size'
|
||||
RESEARCH_WINDOW_KEY = 'research_window'
|
||||
CHROMA_RESEARCH_WINDOW_KEY = 'chroma_research_window'
|
||||
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
self.__useHardware = kwargs.get('use_hardware', False)
|
||||
|
||||
self.__strengthList = []
|
||||
strength = kwargs.get(NlmeansFilter.STRENGTH_KEY, '')
|
||||
if strength:
|
||||
strengthTokens = strength.split(',')
|
||||
for st in strengthTokens:
|
||||
try:
|
||||
strengthValue = float(st)
|
||||
except:
|
||||
raise ValueError('NlmeansFilter: Strength value has to be of type float')
|
||||
if strengthValue < 1.0 or strengthValue > 30.0:
|
||||
raise ValueError('NlmeansFilter: Strength value has to be between 1.0 and 30.0')
|
||||
self.__strengthList.append(strengthValue)
|
||||
else:
|
||||
self.__strengthList = [NlmeansFilter.DEFAULT_STRENGTH]
|
||||
|
||||
self.__patchSizeList = []
|
||||
patchSize = kwargs.get(NlmeansFilter.PATCH_SIZE_KEY, '')
|
||||
if patchSize:
|
||||
patchSizeTokens = patchSize.split(',')
|
||||
for pst in patchSizeTokens:
|
||||
try:
|
||||
patchSizeValue = int(pst)
|
||||
except:
|
||||
raise ValueError('NlmeansFilter: Patch size value has to be of type int')
|
||||
if patchSizeValue < 0 or patchSizeValue > 99:
|
||||
raise ValueError('NlmeansFilter: Patch size value has to be between 0 and 99')
|
||||
if patchSizeValue % 2 == 0:
|
||||
raise ValueError('NlmeansFilter: Patch size value has to an odd number')
|
||||
self.__patchSizeList.append(patchSizeValue)
|
||||
else:
|
||||
self.__patchSizeList = [NlmeansFilter.DEFAULT_PATCH_SIZE]
|
||||
|
||||
self.__chromaPatchSizeList = []
|
||||
chromaPatchSize = kwargs.get(NlmeansFilter.CHROMA_PATCH_SIZE_KEY, '')
|
||||
if chromaPatchSize:
|
||||
chromaPatchSizeTokens = chromaPatchSize.split(',')
|
||||
for cpst in chromaPatchSizeTokens:
|
||||
try:
|
||||
chromaPatchSizeValue = int(pst)
|
||||
except:
|
||||
raise ValueError('NlmeansFilter: Chroma patch size value has to be of type int')
|
||||
if chromaPatchSizeValue < 0 or chromaPatchSizeValue > 99:
|
||||
raise ValueError('NlmeansFilter: Chroma patch value has to be between 0 and 99')
|
||||
if chromaPatchSizeValue % 2 == 0:
|
||||
raise ValueError('NlmeansFilter: Chroma patch value has to an odd number')
|
||||
self.__chromaPatchSizeList.append(chromaPatchSizeValue)
|
||||
else:
|
||||
self.__chromaPatchSizeList = [NlmeansFilter.DEFAULT_CHROMA_PATCH_SIZE]
|
||||
|
||||
self.__researchWindowList = []
|
||||
researchWindow = kwargs.get(NlmeansFilter.RESEARCH_WINDOW_KEY, '')
|
||||
if researchWindow:
|
||||
researchWindowTokens = researchWindow.split(',')
|
||||
for rwt in researchWindowTokens:
|
||||
try:
|
||||
researchWindowValue = int(rwt)
|
||||
except:
|
||||
raise ValueError('NlmeansFilter: Research window value has to be of type int')
|
||||
if researchWindowValue < 0 or researchWindowValue > 99:
|
||||
raise ValueError('NlmeansFilter: Research window value has to be between 0 and 99')
|
||||
if researchWindowValue % 2 == 0:
|
||||
raise ValueError('NlmeansFilter: Research window value has to an odd number')
|
||||
self.__researchWindowList.append(researchWindowValue)
|
||||
else:
|
||||
self.__researchWindowList = [NlmeansFilter.DEFAULT_RESEARCH_WINDOW]
|
||||
|
||||
self.__chromaResearchWindowList = []
|
||||
chromaResearchWindow = kwargs.get(NlmeansFilter.CHROMA_RESEARCH_WINDOW_KEY, '')
|
||||
if chromaResearchWindow:
|
||||
chromaResearchWindowTokens = chromaResearchWindow.split(',')
|
||||
for crwt in chromaResearchWindowTokens:
|
||||
try:
|
||||
chromaResearchWindowValue = int(crwt)
|
||||
except:
|
||||
raise ValueError('NlmeansFilter: Chroma research window value has to be of type int')
|
||||
if chromaResearchWindowValue < 0 or chromaResearchWindowValue > 99:
|
||||
raise ValueError('NlmeansFilter: Chroma research window value has to be between 0 and 99')
|
||||
if chromaResearchWindowValue % 2 == 0:
|
||||
raise ValueError('NlmeansFilter: Chroma research window value has to an odd number')
|
||||
self.__chromaResearchWindowList.append(chromaResearchWindowValue)
|
||||
else:
|
||||
self.__chromaResearchWindowList = [NlmeansFilter.DEFAULT_CHROMA_RESEARCH_WINDOW]
|
||||
|
||||
super().__init__(self)
|
||||
|
||||
|
||||
def getPayload(self, iteration):
|
||||
|
||||
strength = iteration[0]
|
||||
patchSize = iteration[1]
|
||||
chromaPatchSize = iteration[2]
|
||||
researchWindow = iteration[3]
|
||||
chromaResearchWindow = iteration[4]
|
||||
|
||||
suffices = []
|
||||
|
||||
if len(self.__strengthList) > 1:
|
||||
suffices += [f"ds{strength}"]
|
||||
if len(self.__patchSizeList) > 1:
|
||||
suffices += [f"dp{patchSize}"]
|
||||
if len(self.__chromaPatchSizeList) > 1:
|
||||
suffices += [f"dpc{chromaPatchSize}"]
|
||||
if len(self.__researchWindowList) > 1:
|
||||
suffices += [f"dr{researchWindow}"]
|
||||
if len(self.__chromaResearchWindowList) > 1:
|
||||
suffices += [f"drc{chromaResearchWindow}"]
|
||||
|
||||
filterName = 'nlmeans_opencl' if self.__useHardware else 'nlmeans'
|
||||
|
||||
payload = {'identifier': NlmeansFilter.IDENTIFIER,
|
||||
'parameters': {
|
||||
'strength': strength,
|
||||
'patch_size': patchSize,
|
||||
'chroma_patch_size': chromaPatchSize,
|
||||
'research_window': researchWindow,
|
||||
'chroma_research_window': chromaResearchWindow
|
||||
},
|
||||
'suffices': suffices,
|
||||
'variant': f"DS{strength}-DP{patchSize}-DPC{chromaPatchSize}"
|
||||
+ f"-DR{researchWindow}-DRC{chromaResearchWindow}",
|
||||
'tokens': ['-vf', f"{filterName}=s={strength}"
|
||||
+ f":p={patchSize}"
|
||||
+ f":pc={chromaPatchSize}"
|
||||
+ f":r={researchWindow}"
|
||||
+ f":rc={chromaResearchWindow}"]}
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
def getYield(self):
|
||||
for it in itertools.product(self.__strengthList,
|
||||
self.__patchSizeList,
|
||||
self.__chromaPatchSizeList,
|
||||
self.__researchWindowList,
|
||||
self.__chromaResearchWindowList):
|
||||
yield self.getPayload(it)
|
@ -0,0 +1,54 @@
|
||||
import itertools
|
||||
|
||||
from .filter import Filter
|
||||
|
||||
|
||||
class PresetFilter(Filter):
|
||||
|
||||
IDENTIFIER = 'preset'
|
||||
|
||||
DEFAULT_PRESET = 5
|
||||
|
||||
PRESET_KEY = 'preset'
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
self.__presetsList = []
|
||||
presets = str(kwargs.get(PresetFilter.PRESET_KEY, ''))
|
||||
if presets:
|
||||
presetTokens = presets.split(',')
|
||||
for q in presetTokens:
|
||||
try:
|
||||
presetValue = int(q)
|
||||
except:
|
||||
raise ValueError('PresetFilter: Preset value has to be of type int')
|
||||
if presetValue < 0 or presetValue > 13:
|
||||
raise ValueError('PresetFilter: Preset value has to be between 0 and 13')
|
||||
self.__presetsList.append(presetValue)
|
||||
else:
|
||||
self.__presetsList = [PresetFilter.DEFAULT_PRESET]
|
||||
|
||||
super().__init__(self)
|
||||
|
||||
|
||||
def getPayload(self, preset):
|
||||
|
||||
suffices = []
|
||||
|
||||
if len(self.__presetsList) > 1:
|
||||
suffices += [f"p{preset}"]
|
||||
|
||||
payload = {'identifier': PresetFilter.IDENTIFIER,
|
||||
'parameters': {
|
||||
'preset': preset
|
||||
},
|
||||
'suffices': suffices,
|
||||
'variant': f"P{preset}",
|
||||
'tokens': []}
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
def getYield(self):
|
||||
for q in self.__presetsList:
|
||||
yield self.getPayload(q)
|
@ -0,0 +1,54 @@
|
||||
import itertools
|
||||
|
||||
from .filter import Filter
|
||||
|
||||
|
||||
class QualityFilter(Filter):
|
||||
|
||||
IDENTIFIER = 'quality'
|
||||
|
||||
DEFAULT_QUALITY = 32
|
||||
|
||||
QUALITY_KEY = 'quality'
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
self.__qualitiesList = []
|
||||
qualities = kwargs.get(QualityFilter.QUALITY_KEY, '')
|
||||
if qualities:
|
||||
qualityTokens = qualities.split(',')
|
||||
for q in qualityTokens:
|
||||
try:
|
||||
qualityValue = int(q)
|
||||
except:
|
||||
raise ValueError('QualityFilter: Quality value has to be of type int')
|
||||
if qualityValue < 0 or qualityValue > 63:
|
||||
raise ValueError('QualityFilter: Quality value has to be between 0 and 63')
|
||||
self.__qualitiesList.append(qualityValue)
|
||||
else:
|
||||
self.__qualitiesList = [QualityFilter.DEFAULT_QUALITY]
|
||||
|
||||
super().__init__(self)
|
||||
|
||||
|
||||
def getPayload(self, quality):
|
||||
|
||||
suffices = []
|
||||
|
||||
if len(self.__qualitiesList) > 1:
|
||||
suffices += [f"q{quality}"]
|
||||
|
||||
payload = {'identifier': QualityFilter.IDENTIFIER,
|
||||
'parameters': {
|
||||
'quality': quality
|
||||
},
|
||||
'suffices': suffices,
|
||||
'variant': f"Q{quality}",
|
||||
'tokens': []}
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
def getYield(self):
|
||||
for q in self.__qualitiesList:
|
||||
yield self.getPayload(q)
|
@ -0,0 +1,6 @@
|
||||
from .filter import Filter
|
||||
|
||||
class ScaleFilter(Filter):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(self)
|
@ -1,142 +0,0 @@
|
||||
class NlmeansController():
|
||||
"""
|
||||
s: double
|
||||
|
||||
Denoising strength (from 1 to 30) (default 1)
|
||||
Trade-off between noise removal and detail retention. Comparable to gaussian sigma.
|
||||
|
||||
p: int patch size (from 0 to 99) (default 7)
|
||||
|
||||
Catches larger areas reducing broader noise patterns, but costly
|
||||
|
||||
pc: int patch size for chroma planes (from 0 to 99) (default 0)
|
||||
|
||||
r: int research window (from 0 to 99) (default 15)
|
||||
|
||||
Range to search for comparable patches.
|
||||
Better filtering but costly
|
||||
|
||||
rc: int research window for chroma planes (from 0 to 99) (default 0)
|
||||
|
||||
Good values to denoise film grain that was subobtimally encoded:
|
||||
strength: float = 2.8
|
||||
patchSize: int = 12
|
||||
chromaPatchSize: int = 8
|
||||
researchWindow: int = 22
|
||||
chromaResearchWindow: int= 16
|
||||
"""
|
||||
|
||||
DEFAULT_STRENGTH: float = 2.8
|
||||
DEFAULT_PATCH_SIZE: int = 13
|
||||
DEFAULT_CHROMA_PATCH_SIZE: int = 9
|
||||
DEFAULT_RESEARCH_WINDOW: int = 23
|
||||
DEFAULT_CHROMA_RESEARCH_WINDOW: int= 17
|
||||
|
||||
def __init__(self,
|
||||
parameters: str = "none",
|
||||
strength: str = "",
|
||||
patchSize: str = "",
|
||||
chromaPatchSize: str = "",
|
||||
researchWindow: str = "",
|
||||
chromaResearchWindow: str = "",
|
||||
useHardware: bool = False):
|
||||
|
||||
self.__isActive = (parameters != "none"
|
||||
or strength
|
||||
or patchSize
|
||||
or chromaPatchSize
|
||||
or researchWindow
|
||||
or chromaResearchWindow)
|
||||
self.__useHardware = useHardware
|
||||
|
||||
parameterTokens = parameters.split(',')
|
||||
|
||||
self.__strengthList = []
|
||||
if strength:
|
||||
strengthTokens = strength.split(',')
|
||||
for st in strengthTokens:
|
||||
try:
|
||||
strengthValue = float(st)
|
||||
except:
|
||||
raise ValueError('NlmeansController: Strength value has to be of type float')
|
||||
if strengthValue < 1.0 or strengthValue > 30.0:
|
||||
raise ValueError('NlmeansController: Strength value has to be between 1.0 and 30.0')
|
||||
self.__strengthList.append(strengthValue)
|
||||
else:
|
||||
self.__strengthList = [NlmeansController.DEFAULT_STRENGTH]
|
||||
|
||||
self.__patchSizeList = []
|
||||
if patchSize:
|
||||
patchSizeTokens = patchSize.split(',')
|
||||
for pst in patchSizeTokens:
|
||||
try:
|
||||
patchSizeValue = int(pst)
|
||||
except:
|
||||
raise ValueError('NlmeansController: Patch size value has to be of type int')
|
||||
if patchSizeValue < 0 or patchSizeValue > 99:
|
||||
raise ValueError('NlmeansController: Patch size value has to be between 0 and 99')
|
||||
if patchSizeValue % 2 == 0:
|
||||
raise ValueError('NlmeansController: Patch size value has to an odd number')
|
||||
self.__patchSizeList.append(patchSizeValue)
|
||||
else:
|
||||
self.__patchSizeList = [NlmeansController.DEFAULT_PATCH_SIZE]
|
||||
|
||||
self.__chromaPatchSizeList = []
|
||||
if chromaPatchSize:
|
||||
chromaPatchSizeTokens = chromaPatchSize.split(',')
|
||||
for cpst in chromaPatchSizeTokens:
|
||||
try:
|
||||
chromaPatchSizeValue = int(pst)
|
||||
except:
|
||||
raise ValueError('NlmeansController: Chroma patch size value has to be of type int')
|
||||
if chromaPatchSizeValue < 0 or chromaPatchSizeValue > 99:
|
||||
raise ValueError('NlmeansController: Chroma patch value has to be between 0 and 99')
|
||||
if chromaPatchSizeValue % 2 == 0:
|
||||
raise ValueError('NlmeansController: Chroma patch value has to an odd number')
|
||||
self.__chromaPatchSizeList.append(chromaPatchSizeValue)
|
||||
else:
|
||||
self.__chromaPatchSizeList = [NlmeansController.DEFAULT_CHROMA_PATCH_SIZE]
|
||||
|
||||
self.__researchWindowList = []
|
||||
if researchWindow:
|
||||
researchWindowTokens = researchWindow.split(',')
|
||||
for rwt in researchWindowTokens:
|
||||
try:
|
||||
researchWindowValue = int(rwt)
|
||||
except:
|
||||
raise ValueError('NlmeansController: Research window value has to be of type int')
|
||||
if researchWindowValue < 0 or researchWindowValue > 99:
|
||||
raise ValueError('NlmeansController: Research window value has to be between 0 and 99')
|
||||
if researchWindowValue % 2 == 0:
|
||||
raise ValueError('NlmeansController: Research window value has to an odd number')
|
||||
self.__researchWindowList.append(researchWindowValue)
|
||||
else:
|
||||
self.__researchWindowList = [NlmeansController.DEFAULT_RESEARCH_WINDOW]
|
||||
|
||||
self.__chromaResearchWindowList = []
|
||||
if chromaResearchWindow:
|
||||
chromaResearchWindowTokens = chromaResearchWindow.split(',')
|
||||
for crwt in chromaResearchWindowTokens:
|
||||
try:
|
||||
chromaResearchWindowValue = int(crwt)
|
||||
except:
|
||||
raise ValueError('NlmeansController: Chroma research window value has to be of type int')
|
||||
if chromaResearchWindowValue < 0 or chromaResearchWindowValue > 99:
|
||||
raise ValueError('NlmeansController: Chroma research window value has to be between 0 and 99')
|
||||
if chromaResearchWindowValue % 2 == 0:
|
||||
raise ValueError('NlmeansController: Chroma research window value has to an odd number')
|
||||
self.__chromaResearchWindowList.append(chromaResearchWindowValue)
|
||||
else:
|
||||
self.__chromaResearchWindowList = [NlmeansController.DEFAULT_CHROMA_RESEARCH_WINDOW]
|
||||
|
||||
def isActive(self):
|
||||
return self.__isActive
|
||||
|
||||
def generateDenoiseTokens(self):
|
||||
filterName = 'nlmeans_opencl' if self.__useHardware else 'nlmeans'
|
||||
return ['-vf', f"{filterName}=s={self.__strengthList[0]}"
|
||||
+ f":p={self.__patchSizeList[0]}"
|
||||
+ f":pc={self.__chromaPatchSizeList[0]}"
|
||||
+ f":r={self.__researchWindowList[0]}"
|
||||
+ f":rc={self.__chromaResearchWindowList[0]}"] if self.__isActive else []
|
||||
|
@ -0,0 +1,9 @@
|
||||
from ffx.filter.nlmeans_filter import NlmeansFilter
|
||||
from ffx.filter.quality_filter import QualityFilter
|
||||
|
||||
q = QualityFilter()
|
||||
#q = QualityFilter('32,34')
|
||||
# NlmeansFilter(researchWindow='5,7,9', strength='2.0,3.0,4.0')
|
||||
|
||||
for cy in q.getChainYield():
|
||||
print(cy)
|
Loading…
Reference in New Issue