# Calculate histogram for the range [0, 55]
hist_0_55, _ = np.histogram(img[(img >= 0) & (img < 56)], bins=56, range=(0, 56))
# Calculate mean and standard deviation for the range [0, 150]
hist_0_150, _ = np.histogram(img[(img >= 0) & (img < 151)], bins=151, range=(0, 151))
mean_0_150 = np.mean(hist_0_150)
std_dev_0_150 = np.std(hist_0_150)
# Calculate modal value for range [0, 40]
hist_0_40 = hist_0_55[:40]
modal_black_narrow = np.argmax(hist_0_40)
std_dev_above_mean_0_40 = (hist_0_40[modal_black_narrow] - mean_0_150) / std_dev_0_150 if std_dev_0_150 != 0 else 0
# Calculate modal value for the range [0, 55]
modal_black_wide = np.argmax(hist_0_55)
std_dev_above_mean_0_55 = (hist_0_55[modal_black_wide] - mean_0_150) / std_dev_0_150 if std_dev_0_150 != 0 else 0
# Set modal_black based on conditions
modal_black = 0
# Use narrow modal if it's 2 standard deviations above the mean
if std_dev_above_mean_0_40 >= 2:
modal_black = modal_black_narrow
else:
modal_black = modal_black_wide
# Convert image to float for processing
adjusted = img.astype(np.float64)
# Create a mask for extreme original values (0 and 255)
mask = (img == 0) | (img == 255)
# Exclude extreme values from the white adjustment
white_adjust_mask = ~mask
# Scale image if modal white is not at maximum intensity
if modal_white != 255:
adjusted[white_adjust_mask] *= 255.0 / modal_white
adjusted = np.clip(adjusted, 0, 255)
# After white value adjustment, identify new extreme values
new_extremes = (adjusted == 0) | (adjusted == 255)
# Combine masks to exclude all extreme values from black adjustment
total_extremes = mask | new_extremes
black_adjust_mask = ~total_extremes
# Mask for values between 0 and 55
range_0_55_mask = (adjusted >= 0) & (adjusted <= 55)
# Adjust image based on modal black value
if std_dev_above_mean_0_40 < 2 and std_dev_above_mean_0_55 < 2:
# Clip only values within 0 to 55 to 0 if they are below modal value
adjusted[range_0_55_mask & black_adjust_mask] = np.clip(
adjusted[range_0_55_mask & black_adjust_mask], modal_black + 1, None
) - (modal_black + 1)
else:
# Perform traditional adjustment if any is needed
if modal_black:
adjusted[black_adjust_mask] = (
adjusted[black_adjust_mask] - modal_black
) * (255.0 / (255 - modal_black))
adjusted = np.clip(adjusted, 0, 255)
gamma = 1.0 - (modal_black / 255.0)
adjusted[black_adjust_mask] = (
np.power(adjusted[black_adjust_mask] / 255.0, gamma) * 255
)
# Round to nearest integer and convert to uint8
adjusted = np.rint(adjusted).astype(np.uint8)
return adjusted, modal_black, modal_white
except ValueError as e:
log_error_message(f"Error adjusting values for image: {e}")
return img, 0, 255
The provided code function adjust_grayscale_values is designed to process a grayscale image by analyzing its histogram and adjusting it based on modal values and standard deviations. Here's a summary and explanation of the function, along with some implementation comments:
Function Explanation:
-
Histogram Calculation:
hist_white: Computes the histogram of pixel values in the range [250, 255] to find the modal white value.
hist_0_55: Computes the histogram for pixel values in the range [0, 55].
hist_0_150: Computes the histogram for pixel values in the range [0, 150] used for calculating the mean and standard deviation.
hist_0_40: Slice of hist_0_55 for values [0, 40] to find the narrow modal black value.
-
Standard Deviation Calculation:
mean_0_150 and std_dev_0_150 are computed to determine the average and spread of intensity values.
-
Modal Value Determination:
- Finds
modal_black_narrow and modal_black_wide for narrow and wide value ranges.
- Determines how much these modal values are above the mean in standard deviations (
std_dev_above_mean_0_40 and std_dev_above_mean_0_55).
-
Contrast Adjustment:
- If neither
modal_black_narrow nor modal_black_wide is significantly above the mean (i.e., less than 2 standard deviations), it employs contrast stretching using modal values rather than clipping:
- Adjust the image contrast by scaling the intensity based on the modal values determined, but preventing direct clipping unless required.
-
White Value Adjustment:
- If the modal white value is not 255, the function scales the pixel values to reach full intensity at 255, followed by clipping to handle boundary cases.
-
Extreme Value Mask:
- The function creates masks to identify and handle or exclude pixels at extreme values (0 and 255), both before and after adjustments.
-
Gamma Adjustment:
- Applies a gamma correction to enhance the contrast if needed after transforming based on the modal black value.
-
Error Handling:
- Includes a basic exception handling mechanism to log errors during processing, returning the original image if adjustments can't be made due to errors.
Potential Improvements:
- Ensure that the logging functionality (
log_error_message) is defined correctly elsewhere in your code to track issues.
- Consider including more detailed documentation within the code to make future maintenance easier.
- Review the logic ensuring that each histogram calculation and condition aligns precisely with your image processing goal.
- Since this function adjusts images in place, ensure proper testing to understand its effect on various image modalities.
Usage:
By implementing this function into a larger image processing workflow, you can adjust grayscale images to enhance their contrast efficiently. This method can dynamically accommodate the distinct intensities found across various input images.