import cv2
import numpy as np
from google.colab import files
from google.colab.patches import cv2_imshow
# Function to load an image (either uploaded or pre-existing)
def load_image(upload=True, filename=""):
if upload:
print("Please upload the image file.")
uploaded = files.upload() # Upload file(s)
filename = next(iter(uploaded)) # Get the first uploaded file
name
elif filename == "":
print("Error: No filename provided for loading the image.")
return None
# Read the image in grayscale mode
image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
if image is None:
print(f"Error: Unable to load image '{filename}'.")
return image
# Function to perform image negation
def negate_image(image):
negated_image = 255 - image
print("Negated Image Matrix:")
print(negated_image)
print("Negated Image:")
cv2_imshow(negated_image)
cv2.imwrite('negated_image.jpg', negated_image)
print("Negated image saved as 'negated_image.jpg'.")
# Function to apply thresholding
def threshold_image(image, threshold_value=127):
_, thresholded_image = cv2.threshold(image, threshold_value, 255,
cv2.THRESH_BINARY)
print("Thresholded Image Matrix:")
print(thresholded_image)
print("Thresholded Image:")
cv2_imshow(thresholded_image)
cv2.imwrite('thresholded_image.jpg', thresholded_image)
print("Thresholded image saved as 'thresholded_image.jpg'.")
# Function to extract and display bit planes
def extract_and_display_bit_planes(image):
def extract_bit_plane(image, bit):
return (image >> bit) & 1
print("Extracting and displaying bit planes:")
for i in range(8):
bit_plane = extract_bit_plane(image, i) * 255
print(f"Bit Plane {i}:")
cv2_imshow(bit_plane)
# Function for Gray Level Slicing
def gray_level_slicing(image, lower_bound, upper_bound):
# Create a mask where pixels within the specified range are set to
255, and others to 0
sliced_image = np.zeros_like(image)
sliced_image[(image >= lower_bound) & (image <= upper_bound)] = 255
print(f"Gray Level Slicing for range ({lower_bound}, {upper_bound}):")
print("Sliced Image Matrix:")
print(sliced_image)
cv2_imshow(sliced_image)
cv2.imwrite(f"gray_level_sliced_{lower_bound}_{upper_bound}.jpg",
sliced_image)
print(f"Gray level sliced image saved as
'gray_level_sliced_{lower_bound}_{upper_bound}.jpg'.")
# Function to display the menu and handle user choice
def menu():
print("\nImage Processing Menu:")
print("1. Negate Image")
print("2. Threshold Image")
print("3. Extract and Display Bit Planes")
print("4. Gray Level Slicing")
print("5. Exit")
choice = int(input("Enter your choice (1-5): "))
return choice
# Set `upload=True` to upload a file manually, or `upload=False` and
provide a filename for a pre-existing file
image = load_image(upload=True) # Change to `upload=False,
filename='dog.jpg'` if using a local file
if image is not None:
print("Original Image:")
cv2_imshow(image)
# Main loop for menu-driven interface
while True:
choice = menu()
if choice == 1:
negate_image(image)
elif choice == 2:
threshold_value = int(input("Enter threshold value (0-255):
"))
threshold_image(image, threshold_value)
elif choice == 3:
extract_and_display_bit_planes(image)
elif choice == 4:
lower_bound = int(input("Enter lower bound for slicing
(0-255): "))
upper_bound = int(input("Enter upper bound for slicing
(0-255): "))
gray_level_slicing(image, lower_bound, upper_bound)
elif choice == 5:
print("Exiting program.")
break
else:
print("Invalid choice. Please enter a number between 1 and
5.")
Output :
Please upload the image file.
Upload widget is only available when the cell has been executed in the current browser session.
Please rerun this cell to enable.
Saving DOG.JPG to DOG.JPG
Original Image:
Image Processing Menu:
1. Negate Image
2. Threshold Image
3. Extract and Display Bit Planes
4. Gray Level Slicing
5. Exit
Enter your choice (1-5): 1
Negated Image Matrix:
[[166 167 167 ... 194 194 195]
[166 166 167 ... 194 194 195]
[165 166 166 ... 194 194 194]
...
[111 111 110 ... 152 152 153]
[111 111 108 ... 152 152 153]
[111 110 106 ... 152 152 153]]
Negated Image:
Negated image saved as 'negated_image.jpg'.
Image Processing Menu:
1. Negate Image
2. Threshold Image
3. Extract and Display Bit Planes
4. Gray Level Slicing
5. Exit
Enter your choice (1-5): 2
Enter threshold value (0-255): 120
Thresholded Image Matrix:
[[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
...
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]]
Thresholded Image:
Thresholded image saved as 'thresholded_image.jpg'.
Image Processing Menu:
1. Negate Image
2. Threshold Image
3. Extract and Display Bit Planes
4. Gray Level Slicing
5. Exit
Enter your choice (1-5): 3
Extracting and displaying bit planes:
Bit Plane 0:
Bit Plane 1:
Bit Plane 2:
Bit Plane 3:
Bit Plane 4:
Bit Plane 5:
Bit Plane 6:
Bit Plane 7:
Image Processing Menu:
1. Negate Image
2. Threshold Image
3. Extract and Display Bit Planes
4. Gray Level Slicing
5. Exit
Enter your choice (1-5): 4
Enter lower bound for slicing (0-255): 154
Enter upper bound for slicing (0-255): 187
Gray Level Slicing for range (154, 187):
Sliced Image Matrix:
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
Gray level sliced image saved as 'gray_level_sliced_154_187.jpg'.
Image Processing Menu:
1. Negate Image
2. Threshold Image
3. Extract and Display Bit Planes
4. Gray Level Slicing
5. Exit
Enter your choice (1-5): 5
Exiting program.