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

Breaking: Rename contrast method to histogram method. Add more test #22

Merged
merged 3 commits into from
Jan 3, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:

- name: Run benchmarks
run: |
python test/benchmarking/cachegrind.py python all_in_one.py test/images --interal_image_extension png --contrast_method histogram_clahe --scale_down 540 --auto_stack --stack_method ECC --stack_amount 2 2> /dev/null > /dev/null
python test/benchmarking/cachegrind.py python all_in_one.py test/images --interal_image_extension png --histogram_method histogram_clahe --scale_down 540 --auto_stack --stack_method ECC --stack_amount 2 2> /dev/null > /dev/null
mv benchmark.txt /tmp/current.txt
cat /tmp/current.txt

Expand All @@ -82,7 +82,7 @@ jobs:
- name: Run benchmarks
run: |
rm -f test/images/*.hdf5 test/images/*.png
python test/benchmarking/cachegrind.py python all_in_one.py test/images --interal_image_extension png --contrast_method histogram_clahe --scale_down 540 --auto_stack --stack_method ECC --stack_amount 2 2> /dev/null > /dev/null
python test/benchmarking/cachegrind.py python all_in_one.py test/images --interal_image_extension png --histogram_method histogram_clahe --scale_down 540 --auto_stack --stack_method ECC --stack_amount 2 2> /dev/null > /dev/null
cat benchmark.txt

- name: Compare benchmarks
Expand Down
42 changes: 21 additions & 21 deletions all_in_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,68 @@ def save_image(path, image, extension="png"):


# Create main and do any processing if needed
def single_image(images, input_dir, contrast_method, image_extension="png"):
def single_image(images, input_dir, histogram_method, image_extension="png"):
# Default to second image if exists if not first
if len(images) > 1:
image = images[1]
else:
image = images[0]

if contrast_method != "none":
image = single_histogram_processing(image, contrast_method)
if histogram_method != "none":
image = single_histogram_processing(image, histogram_method)

output_image = os.path.join(input_dir, f"main.{image_extension}")

save_image(output_image, image, image_extension)
print(f"Saved {output_image}")


def single_histogram_processing(image, contrast_method):
def single_histogram_processing(image, histogram_method):
"""
Equalize the histogram of a single image.

Parameters:
image (np.ndarray): The image to process.
contrast_method (str): The method to use for contrast enhancement.
histogram_method (str): The method to use for histogram enhancement.

Returns:
np.ndarray: The processed image.

"""
# Check if the image is a grayscale image or has multiple channels
if image.ndim == 2:
# If the image is grayscale, apply the contrast enhancement method directly
if contrast_method == "histogram_clahe":
# If the image is grayscale, apply the histogram enhancement method directly
if histogram_method == "histogram_clahe":
clahe = cv2.createCLAHE(clipLimit=1.5, tileGridSize=(8, 8))
image = clahe.apply(image)
elif contrast_method == "histogram_equalize":
elif histogram_method == "histogram_equalize":
image = cv2.equalizeHist(image)
else:
raise Exception("ERROR: Unknown contrast method")
raise Exception("ERROR: Unknown histogram method")
else:
# If the image has multiple channels, convert it to the YUV color space
yuv_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
# Apply the contrast enhancement method to the Y channel
if contrast_method == "histogram_clahe":
# Apply the histogram enhancement method to the Y channel
if histogram_method == "histogram_clahe":
clahe = cv2.createCLAHE(clipLimit=1.5, tileGridSize=(8, 8))
yuv_image[:, :, 0] = clahe.apply(yuv_image[:, :, 0])
elif contrast_method == "histogram_equalize":
elif histogram_method == "histogram_equalize":
yuv_image[:, :, 0] = cv2.equalizeHist(yuv_image[:, :, 0])
else:
raise Exception("ERROR: Unknown contrast method")
raise Exception("ERROR: Unknown histogram method")
# Convert the image back to the RGB color space
image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2RGB)

return image


def histogram_processing(numpy_array, contrast_method):
def histogram_processing(numpy_array, histogram_method):
"""
Equalize the histograms of the images in a numpy array.

Parameters:
numpy_array (np.ndarray): A numpy array containing the images to process.
contrast_method (str): The method to use for contrast enhancement.
histogram_method (str): The method to use for histogram enhancement.

Returns:
np.ndarray: A numpy array containing the processed images.
Expand All @@ -91,7 +91,7 @@ def histogram_processing(numpy_array, contrast_method):
# Iterate over the images in the numpy array
for i, image in enumerate(numpy_array):
# Process the image
processed_array[i] = single_histogram_processing(image, contrast_method)
processed_array[i] = single_histogram_processing(image, histogram_method)

return processed_array

Expand All @@ -106,9 +106,9 @@ def setup_args():
)
parser.add_argument("--single_image", help="Single image mode", action="store_true")
parser.add_argument(
"--contrast_method",
"--histogram_method",
default="none",
help="Contrast method to use",
help="histogram method to use",
choices=["histogram_clahe", "histogram_equalize"],
)
parser.add_argument(
Expand Down Expand Up @@ -224,7 +224,7 @@ def main(args):
single_image(
numpy_images,
args.input_dir,
args.contrast_method,
args.histogram_method,
args.interal_image_extension,
)

Expand All @@ -243,10 +243,10 @@ def main(args):
processed_image = True
print(f"Shunk in {time() - shrink_tic} seconds")

if args.contrast_method != "none":
if args.histogram_method != "none":
equalize_tic = time()

numpy_images = histogram_processing(numpy_images, args.contrast_method)
numpy_images = histogram_processing(numpy_images, args.histogram_method)

processed_image = True
print(f"Histogram equalized in {time() - equalize_tic} seconds")
Expand Down
4 changes: 1 addition & 3 deletions stacking/stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def chunker(numpy_array, method="ECC", stacking_amount=3, scale_down=720):
]

stacked = []
print("Stacing images...")
print(len(chunks))

# Stack each chunk using the ECC method
for chunk in chunks:
if len(chunk) > 1:
Expand All @@ -151,7 +150,6 @@ def chunker(numpy_array, method="ECC", stacking_amount=3, scale_down=720):

# While there are more than 1 image in the stacked array, keep stacking using the ECC method
while len(stacked) > 1:
print("Looping\n\n\n\n")
temp_stacked = []
# Split the stacked array into chunks of size stacking_amount
chunks = [
Expand Down
Binary file added test/images/0.dng
Binary file not shown.
26 changes: 26 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,35 @@ def setup_images():
# Load ground truth images
ground_truth = loadImages("test/images")

# Remove first image from ground truth due to low contrast
ground_truth = ground_truth[1:]

return noisy_images, ground_truth


def test_filter_low_contrast():
from utils.utils import filterLowContrast, loadImages

ground_truth = loadImages("test/images")

# Filter out low contrast images
filtered_images = filterLowContrast(ground_truth)

# Check if there are only 4 images left
assert len(filtered_images) == 4


def test_shrink_images():
from utils.utils import shrink_images

_, ground_truth = setup_images()

# Shrink images
shrunk_images = shrink_images(ground_truth)

# Check if images are half the size
assert shrunk_images.shape[1] == ground_truth.shape[1] / 2

def test_denoise_fast():
from denoise.denoise import denoiser

Expand Down