0% found this document useful (0 votes)
8 views

ex-p1

The document outlines an experiment conducted by C. Muni Mahesh on image preprocessing commands using Python and OpenCV. It includes source code for various image processing functions such as reading, displaying, converting to grayscale, cropping, resizing, rotating images, and performing histogram equalization. The experiment was successfully implemented, demonstrating the application of these commands on an uploaded image.

Uploaded by

munimahesh2605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

ex-p1

The document outlines an experiment conducted by C. Muni Mahesh on image preprocessing commands using Python and OpenCV. It includes source code for various image processing functions such as reading, displaying, converting to grayscale, cropping, resizing, rotating images, and performing histogram equalization. The experiment was successfully implemented, demonstrating the application of these commands on an uploaded image.

Uploaded by

munimahesh2605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment.No: 1 Name:C.

Muni Mahesh
RollNumber:21121A0534

Image preprocessing Commands


Aim: Implement the Image Preprocessing Commands.
Source code:
from google.colab import files
uploaded = files.upload()
import cv2
import matplotlib.pyplot as plt
import numpy as np
# a. To read an image
def read_image(filepath):
image = cv2.imread(filepath)
return image
# b. To show an image
def show_image(title, image, is_gray=False):
if is_gray:
plt.imshow(image, cmap='gray')
else:
# Convert the image from BGR to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
plt.title(title)
plt.axis('off') # Hide axis
plt.show()
# c. Convert RGB to Gray Scale
def convert_rgb_to_gray(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return gray_image
# d. Read RGB values of a pixel
def read_rgb_values(image, x, y):
# Note: OpenCV uses (y, x) indexing for pixel values
pixel_value = image[y, x]
return pixel_value
# e. Convert Gray Scale to Binary
def convert_gray_to_binary(gray_image, threshold=128):
_, binary_image = cv2.threshold(gray_image, threshold, 255, cv2.THRESH_BINARY)
return binary_image
# f. Perform Image Crop
def crop_borders(image, margin):
height, width = image.shape[:2]
cropped_image = image[margin:height-margin, margin:width-margin]
return cropped_image
# g. Perform Image Resize
def resize_image(image, new_width, new_height):
resized_image = cv2.resize(image, (new_width, new_height))
return resized_image
# h. Perform Image Rotation
def rotate_image(image, angle, scale=1.0):
height, width = image.shape[:2]
center = (width // 2, height // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
return rotated_image
# i. Histogram Equalization
def plot_histogram(image, title):
hist, bins = np.histogram(image.flatten(), bins=256, range=[0,256])
plt.figure(figsize=(5, 5))
plt.bar(bins[:-1], hist, width=1, edgecolor='black')
plt.title(title)
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.show()
# Get the uploaded image filename
filename = list(uploaded.keys())[0]
# Read the image
img = read_image(filename)
# Show the original image
show_image('Original Image', img)
# Convert to Gray Scale and show the gray image
gray_img = convert_rgb_to_gray(img)
show_image('Gray Scale Image', gray_img, is_gray=True)
# Read RGB values of a specific pixel (e.g., at position (50, 100))
x, y = 50, 100
pixel_values = read_rgb_values(img, x, y)
print(f'RGB values at ({x}, {y}): {pixel_values}')
# Convert Gray Scale to Binary and show the binary image
binary_img = convert_gray_to_binary(gray_img)
show_image('Binary Image', binary_img, is_gray=True)
# Crop the image and show the cropped image
margin = 60 # Define the margin to be removed from each side
cropped_img = crop_borders(img, margin)
show_image('Cropped Image', cropped_img)
# Resize the image and show the resized image
new_width, new_height = 150, 150 # Define the new dimensions
resized_img = resize_image(img, new_width, new_height)
show_image('Resized Image', resized_img)
# Rotate the image and show the rotated image
angle = -90 # Define the rotation angle
rotated_img = rotate_image(img, angle)
show_image('Rotated Image', rotated_img)
# Perform histogram equalization and show the equalized image
plot_histogram(gray_img, 'Original Image Histogram')
equalized_img = cv2.equalizeHist(gray_img)
plot_histogram(equalized_img, 'Equalized Image Histogram')
OUTPUT:
RESULT: The above program for image processing commands has been implemented
successfully.

You might also like