Gray Scaling of Image
Gray Scaling of Image
environment.
# Implement the image negative transformation by subtracting each
pixel
# value from the maximum intensity value.
# Display the original and the transformed image.
import cv2
import matplotlib.pyplot as plt
plt.subplot(1, 2, 2)
plt.imshow(negative_image, cmap='gray')
plt.title('Negative Image')
plt.show()
Args:
image: The input grayscale image.
c: The scaling constant.
Returns:
The transformed image.
"""
# Convert the image to float for calculations
image_float = image.astype(np.float32)
# Apply the log transformation
transformed_image = c * np.log(1 + image_float)
# Clip values to the range [0, 255]
transformed_image = np.clip(transformed_image, 0, 255)
# Convert back to uint8
transformed_image = transformed_image.astype(np.uint8)
return transformed_image
for i, c in enumerate(c_values):
transformed_image = log_transform(image, c)
plt.subplot(1, len(c_values) + 1, i + 2)
plt.imshow(transformed_image, cmap='gray')
plt.title(f'Log Transformed (c = {c})')
plt.show()
# prompt: Load a grayscale image into the image processing
environment.
# Implement the power-law transformation using the formula:
# where $r$ is the input intensity value, $s$ is the output intensity
value, $c$
# is a constant for scaling, and $\gamma$ is the gamma parameter.
# Experiment with different values of $\gamma$.
# Display the original and transformed images.
import cv2
import matplotlib.pyplot as plt
import numpy as np
Args:
image: The input grayscale image.
c: The scaling constant.
gamma: The gamma parameter.
Returns:
The transformed image.
"""
# Convert the image to float for calculations
image_float = image.astype(np.float32)
# Apply the power-law transformation
transformed_image = c * np.power(image_float, gamma)
# Clip values to the range [0, 255]
transformed_image = np.clip(transformed_image, 0, 255)
# Convert back to uint8
transformed_image = transformed_image.astype(np.uint8)
return transformed_image
plt.show()
import cv2
import matplotlib.pyplot as plt
import numpy as np
Args:
image: The input grayscale image.
points: A list of tuples representing the control points
of the transformation. Each tuple contains
(input intensity, output intensity).
Returns:
The transformed image.
"""
# Create a lookup table (LUT)
lut = np.zeros(256, dtype=np.uint8)
# Sort the points based on input intensity
points.sort()
plt.subplot(1, 2, 2)
plt.imshow(transformed_image, cmap='gray')
plt.title('Transformed Image')
plt.show()
# prompt: Apply contrast stretching to a grayscale image to enhance
its contrast.
# Experiment with different stretching ranges (minimum and maximum
intensity
# values).
import cv2
import matplotlib.pyplot as plt
import numpy as np
Args:
image: The input grayscale image.
min_intensity: The minimum intensity value for stretching.
max_intensity: The maximum intensity value for stretching.
Returns:
The contrast-stretched image.
"""
# Convert the image to float for calculations
image_float = image.astype(np.float32)
# Normalize the image to the range [0, 1]
normalized_image = (image_float - np.min(image_float)) /
(np.max(image_float) - np.min(image_float))
# Stretch the contrast
stretched_image = (normalized_image * (max_intensity -
min_intensity)) + min_intensity
# Clip values to the range [0, 255]
stretched_image = np.clip(stretched_image, 0, 255)
# Convert back to uint8
stretched_image = stretched_image.astype(np.uint8)
return stretched_image
plt.show()
import cv2
import matplotlib.pyplot as plt
import numpy as np
Args:
image: The input grayscale image.
lower_threshold: The lower threshold value.
upper_threshold: The upper threshold value.
Returns:
The sliced image.
"""
# Create a mask for the specified range
mask = (image >= lower_threshold) & (image <= upper_threshold)
# Set all other pixels to zero
sliced_image = np.where(mask, image, 0)
return sliced_image
plt.show()
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the grayscale image
image = cv2.imread('football.jpg', cv2.IMREAD_GRAYSCALE)
Args:
image: The input grayscale image.
Returns:
A list of bit planes.
"""
bit_planes = []
for i in range(8):
bit_plane = (image >> i) & 1
bit_planes.append(bit_plane)
return bit_planes
Args:
bit_planes: A list of bit planes.
planes_to_combine: A list of indices of bit planes to combine.
Returns:
The reconstructed image.
"""
combined_image = np.zeros_like(bit_planes[0])
for i in planes_to_combine:
combined_image = combined_image + (bit_planes[i] << i)
return combined_image
plt.subplot(1, 2, 2)
plt.imshow(reconstructed_image, cmap='gray')
plt.title('Reconstructed Image (4-bit)')
plt.show()