-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for nuclear speckle image display in CytoDataFrame (#64)
* dynamic bounding box and scale image bit depth * add opencv * check images for adjustment; add tests * linting * coverage configuration * add note about configuration * fix coverage badge reference for pypi * move to emoji character instead of code for pypi * more descriptive parameter name Co-Authored-By: Jenna Tomkinson <[email protected]> * fix tests * format before lint --------- Co-authored-by: Jenna Tomkinson <[email protected]>
- Loading branch information
1 parent
2172952
commit af7afdf
Showing
10 changed files
with
234 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,3 +142,5 @@ cython_debug/ | |
*.csv | ||
|
||
.DS_Store | ||
|
||
tests/data/cytotable/Nuclear_speckles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Helper functions for working with images in the context of coSMicQC. | ||
""" | ||
|
||
import cv2 | ||
import numpy as np | ||
from PIL import Image, ImageEnhance | ||
|
||
|
||
def is_image_too_dark(image: Image, pixel_brightness_threshold: float = 10.0) -> bool: | ||
""" | ||
Check if the image is too dark based on the mean brightness. | ||
By "too dark" we mean not as visible to the human eye. | ||
Args: | ||
image (Image): | ||
The input PIL Image. | ||
threshold (float): | ||
The brightness threshold below which the image is considered too dark. | ||
Returns: | ||
bool: | ||
True if the image is too dark, False otherwise. | ||
""" | ||
# Convert the image to a numpy array and then to grayscale | ||
img_array = np.array(image) | ||
gray_image = cv2.cvtColor(img_array, cv2.COLOR_RGBA2GRAY) | ||
|
||
# Calculate the mean brightness | ||
mean_brightness = np.mean(gray_image) | ||
|
||
return mean_brightness < pixel_brightness_threshold | ||
|
||
|
||
def adjust_image_brightness(image: Image) -> Image: | ||
""" | ||
Adjust the brightness of an image using histogram equalization. | ||
Args: | ||
image (Image): | ||
The input PIL Image. | ||
Returns: | ||
Image: | ||
The brightness-adjusted PIL Image. | ||
""" | ||
# Convert the image to numpy array and then to grayscale | ||
img_array = np.array(image) | ||
gray_image = cv2.cvtColor(img_array, cv2.COLOR_RGBA2GRAY) | ||
|
||
# Apply histogram equalization to improve the contrast | ||
equalized_image = cv2.equalizeHist(gray_image) | ||
|
||
# Convert back to RGBA | ||
img_array[:, :, 0] = equalized_image # Update only the R channel | ||
img_array[:, :, 1] = equalized_image # Update only the G channel | ||
img_array[:, :, 2] = equalized_image # Update only the B channel | ||
|
||
# Convert back to PIL Image | ||
enhanced_image = Image.fromarray(img_array) | ||
|
||
# Slightly reduce the brightness | ||
enhancer = ImageEnhance.Brightness(enhanced_image) | ||
reduced_brightness_image = enhancer.enhance(0.7) | ||
|
||
return reduced_brightness_image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Tests cosmicqc image module | ||
""" | ||
|
||
from cosmicqc.image import adjust_image_brightness, is_image_too_dark | ||
from PIL import Image | ||
|
||
|
||
def test_is_image_too_dark_with_dark_image(fixture_dark_image: Image): | ||
assert is_image_too_dark(fixture_dark_image, pixel_brightness_threshold=10.0) | ||
|
||
|
||
def test_is_image_too_dark_with_bright_image(fixture_bright_image: Image): | ||
assert not is_image_too_dark(fixture_bright_image, pixel_brightness_threshold=10.0) | ||
|
||
|
||
def test_is_image_too_dark_with_mid_brightness_image( | ||
fixture_mid_brightness_image: Image, | ||
): | ||
assert not is_image_too_dark( | ||
fixture_mid_brightness_image, pixel_brightness_threshold=10.0 | ||
) | ||
|
||
|
||
def test_adjust_image_brightness_with_dark_image(fixture_dark_image: Image): | ||
adjusted_image = adjust_image_brightness(fixture_dark_image) | ||
# we expect that image to be too dark (it's all dark, so there's no adjustments) | ||
assert is_image_too_dark(adjusted_image, pixel_brightness_threshold=10.0) | ||
|
||
|
||
def test_adjust_image_brightness_with_bright_image(fixture_bright_image: Image): | ||
adjusted_image = adjust_image_brightness(fixture_bright_image) | ||
# Since the image was already bright, it should remain bright | ||
assert not is_image_too_dark(adjusted_image, pixel_brightness_threshold=10.0) | ||
|
||
|
||
def test_adjust_image_brightness_with_mid_brightness_image( | ||
fixture_mid_brightness_image: Image, | ||
): | ||
adjusted_image = adjust_image_brightness(fixture_mid_brightness_image) | ||
# The image should still not be too dark after adjustment | ||
assert not is_image_too_dark(adjusted_image, pixel_brightness_threshold=10.0) |