Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unsharp mask #432

Merged
merged 7 commits into from
Oct 4, 2024
32 changes: 28 additions & 4 deletions src/normalization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,35 @@ function imsharpen(

image_equalized_gray = Gray.(image_equalized)

image_smoothed = imfilter(image_equalized_gray, Kernel.gaussian(smoothing_param))
return unsharp_mask(image_equalized_gray, smoothing_param, intensity)
end

"""
unsharp_mask(image_gray, smoothing_param, intensity)
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved

Apply unsharp masking on (equalized) grayscale ([0, `clapmax`]) image to enhance its sharpness.
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved

# Arguments
- `image_gray: The input grayscale image, typically already equalized.
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved
- `smoothing_param::Int`: The pixel radius for Gaussian blurring (typically between 1 and 10).
- `intensity: The amount of sharpening to apply. Higher values result in more pronounced sharpening.
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved

cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved
# Returns
The sharpened grayscale image with values clipped between 0 and `clapmax`.
"""
function unsharp_mask(image_gray, smoothing_param, intensity, clampmax)
image_smoothed = imfilter(image_gray, Kernel.gaussian(smoothing_param))
clamp!(image_smoothed, 0.0, clampmax)
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved
image_sharpened = image_gray * (1 + intensity) .- image_smoothed * intensity
clamp!(image_sharpened, 0.0, clampmax)
return round.(Int, image_sharpened)
end

image_sharpened =
image_equalized_gray .* (1 + intensity) .+ image_smoothed .* (-intensity)
return min.(max.(image_sharpened, 0.0), 1.0)
# For old workflow in final2020.m
function unsharp_mask(image_gray, smoothing_param, intensity)
cpaniaguam marked this conversation as resolved.
Show resolved Hide resolved
image_smoothed = imfilter(image_gray, Kernel.gaussian(smoothing_param))
image_sharpened = image_gray * (1 + intensity) .- image_smoothed * intensity
return clamp.(image_sharpened, 0.0, 1.0)
end

"""
Expand Down
Loading