Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions src/vstarstack/tool/image_processing/drop_unsharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,36 @@
import os
import numpy as np
import scipy.ndimage
from enum import Enum

import vstarstack.library.data
import vstarstack.tool.cfg
import vstarstack.tool.common

def measure_sharpness(img : np.ndarray) -> float:
sx = scipy.ndimage.sobel(img, axis=0, mode='constant')
sy = scipy.ndimage.sobel(img, axis=1, mode='constant')
sobel = np.sqrt(sx**2 + sy**2)
metric = np.sum(sobel)
summ = np.sum(img)
return metric / summ
class EstimationMethod(Enum):
"""Sharpness estimation method"""
SOBEL = 0
LAPLACE = 1

def measure_sharpness_df(df : vstarstack.library.data.DataFrame) -> float:
def measure_sharpness(img : np.ndarray, method : EstimationMethod) -> float:
if method == EstimationMethod.SOBEL:
sx = scipy.ndimage.sobel(img, axis=0, mode='constant')
sy = scipy.ndimage.sobel(img, axis=1, mode='constant')
sobel = np.sqrt(sx**2 + sy**2)
metric = np.sum(sobel)
summ = np.sum(img)
return metric / summ
elif method == EstimationMethod.LAPLACE:
sx = scipy.ndimage.laplace(img, mode='constant')
sy = scipy.ndimage.laplace(img, mode='constant')
sobel = np.sqrt(sx**2 + sy**2)
metric = np.sum(sobel)
summ = np.sum(img)
return metric / summ
else:
raise Exception(f"Unknown method {method}")

def measure_sharpness_df(df : vstarstack.library.data.DataFrame, method : EstimationMethod) -> float:
metric = 0
nch = 0
for channel in df.get_channels():
Expand All @@ -38,29 +54,29 @@ def measure_sharpness_df(df : vstarstack.library.data.DataFrame) -> float:
amax = np.amax(img)
amin = np.amin(img)
img = (img - amin)/(amax - amin)
metric += measure_sharpness(img)
metric += measure_sharpness(img, method)
nch += 1
if nch == 0:
return 0
return metric / nch

def select_sharpests(fnames : list[str], percent : int):
def select_sharpests(fnames : list[str], percent : int, method : EstimationMethod):
metrics = []
for fname in fnames:
df = vstarstack.library.data.DataFrame.load(fname)
metric = measure_sharpness_df(df)
metric = measure_sharpness_df(df, method)
print(f"{fname} : {metric}")
metrics.append((fname, metric))
metrics = sorted(metrics, key=lambda item: item[1], reverse=True)
metrics = metrics[:int(len(metrics)*percent/100)]
return [item[0] for item in metrics]

def run(project : vstarstack.tool.cfg.Project, argv : list[str]):
def _process(project : vstarstack.tool.cfg.Project, argv : list[str], method : EstimationMethod):
path = argv[0]
percent = int(argv[1])
files = vstarstack.tool.common.listfiles(path, ".zip")
fnames = [item[1] for item in files]
sharpests = select_sharpests(fnames, percent)
sharpests = select_sharpests(fnames, percent, method)
for i,fname in enumerate(sharpests):
basename = os.path.basename(fname)
dirname = os.path.dirname(fname)
Expand All @@ -71,3 +87,8 @@ def run(project : vstarstack.tool.cfg.Project, argv : list[str]):
for fname in fnames:
print(f"Removing {fname}")
os.remove(fname)

commands = {
"sobel" : (lambda project, argv : _process(project, argv, EstimationMethod.SOBEL), "Use Sobel filter for estimating sharpness", "path/ percent"),
"laplace" : (lambda project, argv : _process(project, argv, EstimationMethod.LAPLACE), "Use Laplace filter for estimating sharpness", "path/ percent"),
}
2 changes: 1 addition & 1 deletion src/vstarstack/tool/image_processing/fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ def copy(project: vstarstack.tool.cfg.Project, argv: list):
"normalize": (vstarstack.tool.image_processing.normalize.run, "normalize to weight"),
"blur": (vstarstack.tool.image_processing.blur.run, "gaussian blur"),
"deconvolution": (vstarstack.tool.image_processing.deconvolution.run, "RL deconvolution"),
"select-sharp" : (vstarstack.tool.image_processing.drop_unsharp.run, "select sharp images", "path/ percent"),
"select-sharp" : (vstarstack.tool.image_processing.drop_unsharp.commands, "select sharp images"),
}