import numpy as np
from skimage import io, color, img_as_float
from scipy.fft import fft2, ifft2, fftshift, ifftshift
from scipy.ndimage import zoom
import matplotlib.pyplot as plt
def homomorphic_filtering(image, d0=20, c=5., gamma_h=3.0, gamma_l=0.4):
"""
Perform homomorphic filtering on the input grayscale image using the given H(u, v) function.
Parameters:
image (numpy.ndarray): Input grayscale image in float format.
d0 (float): Cutoff frequency for the filter.
c (float): Parameter controlling the filter's sharpness.
gamma_h (float): High-frequency gain.
gamma_l (float): Low-frequency gain.
Returns:
numpy.ndarray: Enhanced image after homomorphic filtering.
"""
# Step 1: Ensure image is in float format and add small constant to avoid log(0)
img_float = img_as_float(image)
epsilon = 1e-6
img_float = img_float + epsilon
# Step 2: Apply log transformation
img_log = np.log(img_float)
# Step 3: Apply Fourier transform
dft = fft2(img_log)
dft_shift = fftshift(dft)
# Step 4: Create the H(u, v) filter
rows, cols = image.shape
crow, ccol = rows // 2, cols // 2
u = np.arange(rows).reshape(-1, 1) - crow
v = np.arange(cols).reshape(1, -1) - ccol
D = np.sqrt(u**2 + v**2)
H = (gamma_h - gamma_l) * (1 - np.exp(-c * (D ** 2) / (d0 ** 2))) + gamma_l
# Step 5: Apply the filter in the frequency domain
S = dft_shift * H
# Step 6: Inverse Fourier transform
dft_inv_shift = ifftshift(S)
img_back = ifft2(dft_inv_shift)
# Step 7: Inverse log transformation and take real part
img_exp = np.exp(np.real(img_back))
print(img_exp)
# Step 8: Normalize to [0, 1] range
# img_enhanced = (img_exp - np.min(img_exp)) / (np.max(img_exp) - np.min(img_exp))
img_enhanced = img_exp
return img_enhanced
# Image loading and processing
def process_image(image_path, target_size=(256, 256)):
"""
Load and preprocess image for homomorphic filtering.
"""
# Load image
input_image = io.imread(image_path)
# Convert to grayscale if needed
if input_image.ndim == 3:
input_image = color.rgb2gray(input_image)
# Resize to target size
if input_image.shape != target_size:
scale_factors = (target_size[0] / input_image.shape[0],
target_size[1] / input_image.shape[1])
input_image = zoom(input_image, scale_factors)
return input_image
# Main execution
if __name__ == "__main__":
# Load and process image
image_path = 'selected_images/00000001_000.png'
input_image = process_image(image_path)
# Apply homomorphic filtering
enhanced_image = homomorphic_filtering(input_image, d0=20, c=5.0, gamma_h=3.0, gamma_l=0.4)
# Display results
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(input_image, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title('Enhanced Image')
plt.imshow(enhanced_image, cmap='gray')
plt.axis('off')
plt.show()
Original Code:
https://omlc.org/software/mc/mcml-src/
Original Code manual:
https://omlc.org/software/mc/man_mcml.pdf
Python code:
working.zip is attached.
To run:
install python 3.11 or later
install numpy
then
python mcmlmain.py
Here you will find the lecture materials. PDF is the paper we followed during the lecture.
The codes developed by the authors is here:
https://omlc.org/software/mc/mcml-src/
The python code we started is still in progress and can be accessed from here:
https://tyetkin.web.cern.ch/tyetkin/code_conversion.py
The code is not complete and it will not work. We will continue to work on it.
BME5006 Project Topics
Utkan 11
Ugur 3
Mohammad 6
Beyza 9
Berivan 10
Ceren 13
Rumeysa 12
Irem 5
Kutalmis 14
Ezgi 8
Nisanur 1
Yunus Emre * kendi konusu
Seyma 15
Feyza 4
Melike 2
Sevval 24
Mustafa Cem Aksu 20
Rukiye Ozcanli 17
Serhat Unalan 19
Rukiye Akyuz Kendi Konusu
Emrah Aygun 7
Edanur Aciksoz 26
BME5006 Syllabus & Lecture 1 Note are attached.
Code tutorial is moved here:
Code repo for short tutorial:
https://github.com/tyetkin/mc-light-propagation-tissue/blob/main/README.md
Video tutorials:
Python training videos:
Short videos:
https://www.youtube.com/watch?v=kqtD5dpn9C8&pp=ygUXaW50cm9kdWN0aW9uIHRvIHB5dGhvbiA%3D
https://www.youtube.com/watch?v=b093aqAZiPU&pp=ygUXaW50cm9kdWN0aW9uIHRvIHB5dGhvbiA%3D
A full Pyhton Course Video:
https://www.youtube.com/watch?v=nLRL_NcnK-4&pp=ygUXaW50cm9kdWN0aW9uIHRvIHB5dGhvbiA%3D