0% found this document useful (0 votes)
56 views16 pages

Image Processing Lab

The document outlines a series of problem statements related to image processing using Google Drive and Google Colab. It includes code snippets for reading, resizing, displaying, and cropping images, as well as obtaining their size and representing them as matrices. Each section provides specific instructions and code to achieve the desired image manipulation tasks.

Uploaded by

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

Image Processing Lab

The document outlines a series of problem statements related to image processing using Google Drive and Google Colab. It includes code snippets for reading, resizing, displaying, and cropping images, as well as obtaining their size and representing them as matrices. Each section provides specific instructions and code to achieve the desired image manipulation tasks.

Uploaded by

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

INDE

SL Problem statement Page Date Signature


no. no

1 Read the Image 2

2 Get the size of the Image 3

3 Resize the Image (zoom in/zoom out) 4

4 Display the Image as Matrix 6

5 Crop the Image 7

REMARKS

1|Page
2|Page
Date:
Problem Statement: Read the Image System/Tools:

Google Drive and Google Colab

Code:

# First, we need to mount Google Drive to access files stored there.

from google.colab import drive

drive.mount('/content/drive')

# Now,It can read an image from your Drive using a library

import cv2

import matplotlib.pyplot as plt

image_path = '/content/drive/MyDrive/Colab Notebooks/DSC.JPG' try:


image = cv2.imread(image_path) if
image is not None:
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Original image")
plt.axis('off') plt.show()

print("Image loaded successfully!") else:


print("Error: Could not load the image. Please check
the path.")

except Exception as e:
print(f"An error occurred: {e}")

3|Page
Date:
Problem statement: Get the Image size System/Tools:

Google Drive and Google Colab

Code:

# Get and print the image size


if 'image' in locals() and image is not None:
image_size = image.shape
print(f"Image size (height, width, channels):
{image_size}") else:
print("Image variable not found or image not loaded
successfully in the previous cell.")

4|Page
Date:
Problem Statement: Resize the Image (Zoom in/Zoom out) System/Tools:

Google Drive and Google Colab

Code:

import cv2
import matplotlib.pyplot as plt

if 'image' in locals() and image is not None: # Get


the original image dimensions
original_height, original_width, _ = image.shape

new_width_larger = original_width * 2
new_height_larger = original_height * 3

# Resize the image


resized_larger_image = cv2.resize(image,
(new_width_larger, new_height_larger),
interpolation=cv2.INTER_LINEAR)

new_width_smaller = original_width // 2
new_height_smaller = original_height // 4

# Resize the image


resized_smaller_image = cv2.resize(image, (new_width_smaller,
new_height_smaller), interpolation=cv2.INTER_AREA)

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1) plt.imshow(cv2.cvtColor(image,
cv2.COLOR_BGR2RGB)) plt.title("Original Image")
plt.axis('off')

plt.subplot(1, 3, 2) plt.imshow(cv2.cvtColor(resized_larger_image,
cv2.COLOR_BGR2RGB))
plt.title("Resized Larger")
plt.axis('off')

plt.subplot(1, 3, 3) plt.imshow(cv2.cvtColor(resized_smaller_image,
cv2.COLOR_BGR2RGB))
5 | P a plt.title("Resized
ge Smaller")
plt.show()

print("Image resized with direct height and width values.")

else:
print("Image not found or image not loaded successfully..")

6|Page
7|Page
Date:
Problem Statement: Display the Image as Matrix System/Tools:

Google Drive and Google colab

Code:

import numpy as np

if 'image' in locals() and image is not None:


# Convert the image to a NumPy array (if it's not already)
image_matrix = np.array(image)

print("Image as a matrix (showing a small portion):")


display(image_matrix[0:10, 0:10])

else:

8|Page
Date:
Problem Statement: Crop the Image System/Tools:

Google drive and Google Colab

Code:

import matplotlib.pyplot as plt

if 'image' y1,
in y2 x1, x2and image is not None:
locals()
# Crop = 2000, 4800 # Rows (height)
= 600, 2600 # Columns (width)
the image using array slicing
cropped_image = image[y1:y2, x1:x2]

# Display the original and cropped images plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1) plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.title("Or


plt.axis('off')

plt.subplot(1, 2, 2) plt.imshow(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)) plt


plt.axis('off') plt.show()
print("Image cropped successfully!")

else:
print("Image variable not found or image not loaded successfully. Please run the cell to

9|Page
10 | P a g
e
OUTPUT:

1. Problem Statement: Read the Image.

2. Problem Statement: Get the size of the Image. Image size

(height, width, channels): (6000, 4000, 3)

3. Problem statement: Resize the Image (Zoom in/Zoom out).

11 | P a g
e
12 | P a g
e
4. Problem Statement: Display the Image as Matrix.

5. Problem Statement: Crop the Image.

13 | P a g
e
14 | P a g
e
10 | P a g
10 | P a g

You might also like