You are on page 1of 5

LAB 07

Q : Read a grayscale image in python and perform the following tasks:


 Display its negative image
 Select middle gray level as a threshold and create a binary image
 Select a range of values to create a binary image
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
# Load the grayscale image from your Google Drive (adjust the path)
image_path = '/content/tiger.jpg'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Display the original image
cv2_imshow(image)
# Task A: Display the negative image
negative_image = 255 - image
cv2_imshow(negative_image)
# Task B: Create a binary image using middle gray level as threshold
threshold_value = np.median(image)
_,binary_image = cv2.threshold(image, threshold_value, 255,
cv2.THRESH_BINARY)
cv2_imshow(binary_image)
#Task C:Create a binary image with pixel values in the range 100-200
lower_bound = 100
upper_bound = 200
range_binary_image = cv2.inRange(image, lower_bound, upper_bound)
cv2_imshow(range_binary_image)
Q : Implement im2 double() and mat2gray() python functions on any unit8 class grayscale
image and describe your observations when comparing the results .
import numpy as np

def im2double(image):
return image.astype(np.float64) / 255.0

def mat2gray(image):
min_val = np.min(image)
max_val = np.max(image)
return (image - min_val) / (max_val - min_val)
# Assuming 'image' is your uint8 grayscale image
double_image = im2double(image)
normalized_image = mat2gray(image)
# Compare the results
print("Original Image (uint8):")
print(image)
print("\nDouble Image:")
print(double_image)
print("\nNormalized Image:")
print(normalized_image)
Q : Read any grayscale image and apply the transform function as provided in fig 1(a) and
(c) if you roll number is even, else apply the transform function as provided in fig 1(b) and
(d). Select appropriate values for variable a and b.
import cv2
import numpy as np
import matplotlib.pyplot as plt
image =cv2.imread('/content/tiger.jpg',cv2.IMREAD_GRAYSCALE)
# Define transformation functions for even and odd roll
numbers
def transform_even(image):
a = 1.0 # Replace with your value
b = 0.0 # Replace with your value
transformed_image = a * image + b
return transformed_image

def transform_odd(image):
a = 1.0 # Replace with your value
b = 0.0 # Replace with your value
transformed_image = a * image + b
return transformed_image
# Check if your roll number is even or odd (replace
'roll_number' with your actual roll number)
roll_number = 123456 # Replace with your actual roll number
if roll_number % 2 == 0:
transformed_image = transform_even(image)
else:
transformed_image = transform_odd(image)
# Display the original and processed images
plt.subplot(121)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.subplot(122)
plt.title("Processed Image")
plt.imshow(transformed_image, cmap='gray')
plt.tight_layout()
plt.show()
Q : Read any grayscale image and enhanced the image during power law transmission c=1
always used different values of gamma(0.3,0.4,1.0,3.0,5.0) .
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the grayscale image
image = cv2.imread('/content/girl.jpg')
plt.imshow(image, cmap='gray')
# Define a list of gamma values to apply
gamma_values = [0.3, 0.4, 0.6,1.0, 3.0, 5.0]
# Create a figure to display the images
plt.figure(figsize=(12, 6))
# Apply power-law transformation for each gamma value and plot
the results
for i, gamma in enumerate(gamma_values):
# Normalize pixel values to the range [0, 1]
normalized_image = image / 255.0
# Apply the power-law transformation
enhanced_image = np.power(normalized_image, gamma)
# Rescale the pixel values back to the range [0, 255]
enhanced_image = np.uint8(enhanced_image * 255)
# Plot the enhanced image with the current gamma value
plt.subplot(1, len(gamma_values), i + 1)
plt.imshow(enhanced_image, cmap='gray')
plt.title(f'Gamma = {gamma}')
plt.axis('off')
plt.tight_layout()
plt.show()
Q : Apply imadjust function of python on any grayscale image and use the varying values
for low-in, high-in, low-out, high-out and the values of gamma combination.
import cv2
from google.colab.patches import cv2_imshow
import numpy as np
import matplotlib.pyplot as plt
import skimage
from skimage import exposure
image_path="/content/istockphoto-1189980095-612x612.jpg"
image=cv2.imread(image_path)
value_combinations=[([0,255],[0,255],3.0),([50,200],[0,255],4.
0),
([0,255],[100,200],2.5),([100,200],[50,200],1.8)]
cv2_imshow(image)
plt.figure(figsize=(15,10))
for i,(low_in,high_in,gamma)in enumerate (value_combinations):
adjusted_image=exposure.rescale_intensity(image,in_range=(lo
w_in[0],high_in[1]),out_range=(0,255))
gamma_corrected_image=exposure.adjust_gamma(adjusted_image,g
amma)
plt.subplot(2,4,i+1)
plt.title(f"y={gamma},in=[{low_in[0]},{high_in[1]}],out=[0,2
55]")
plt.imshow(gamma_corrected_image,cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()

You might also like