[go: up one dir, main page]

0% found this document useful (0 votes)
13 views8 pages

Lab 1 Dip

digital image processing lab in python

Uploaded by

hamnah awan
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)
13 views8 pages

Lab 1 Dip

digital image processing lab in python

Uploaded by

hamnah awan
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/ 8

UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA

DEPARTMENT OF ELECTRICAL ENGINEERING

DIGITAL IMAGE PROCESSING LAB


Lab-01: Fundamentals
LAB TASK 01:.
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Check if the file exists
image_path = 'c:/Users/Hp/OneDrive/Desktop/Temp/standard_test_images/mandril_gray.tif' #
Replace with the correct path
image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
# Display the image in a window
# Add text (title) to the image
title = 'HAMNAH 21-EE-38'
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
color = (255, 255, 255) # White color (BGR format)
thickness = 2
position = (10, 30) # Position where you want to place the text (x, y)
# Put the title on the image
cv2.putText(image, title, position, font, font_scale, color, thickness, cv2.LINE_AA)
# Display the image with title
cv2.imshow('Image with Title', image)
 #PART A
# Get the resolution (size of the image)
rows, cols = image.shape
print(f"Resolution: {rows} x {cols}") # M × N (row × columns)
# Calculate the total number of pixels
total_pixels = rows * cols
print(f"Total number of pixels: {total_pixels}")
 #PART B
# Display the data type of the image
image_dtype = image.dtype
print(f"Data type of the image: {image_dtype}")
 #PART C
# Display the bit depth of the test image
itemsize = image.dtype.itemsize
bit_depth = itemsize * 8 # Convert bytes to bits
print(f"Bit depth of the image: {bit_depth} bits")
# Wait indefinitely until a key is pressed
cv2.waitKey(0)
# Close the image window
cv2.destroyAllWindows()
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

 #PART D
We are working with a TIFF file (mandril_gray.tif), which is a lossless format.
Therefore, image is most likely lossless, meaning there is no data loss due to
compression, and the image retains all of its original quality.

LAB TASK 02:.


import cv2
import matplotlib.pyplot as plt
# Load the color image using Matplotlib
image=plt.imread('c:/Users/Hp/OneDrive/Desktop/Temp/standard_test_images/mandril_color.tif')
# Display the image using Matplotlib
plt.imshow(image)
plt.title('Mandril Color Image')
plt.axis('off')
plt.show()
 #PART A
# Get the image resolution (rows, columns, and channels)
rows, columns, channels = image.shape
print(f"Resolution: {rows} x {columns} x {channels}")
 #PART B
# The third dimension (3) represents the three color channels (R, G, B).
# Separate the color channels
R = image[:, :, 0]
G = image[:, :, 1]
B = image[:, :, 2]
# Create a 2x2 grid for displaying the images
plt.figure(figsize=(10, 10))
# Add the main title
plt.suptitle('HAMNAH 21-EE-38', fontsize=16)
# Original color image
plt.subplot(2, 2, 1)
plt.imshow(image)
plt.title('Original Image')
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

plt.axis('off')
# Red channel
plt.subplot(2, 2, 2)
plt.imshow(R, cmap='gray')
plt.title('Red Channel')
plt.axis('off')
# Green channel
plt.subplot(2, 2, 3)
plt.imshow(G, cmap='gray')
plt.title('Green Channel')
plt.axis('off')
# Blue channel
plt.subplot(2, 2, 4)
plt.imshow(B, cmap='gray')
plt.title('Blue Channel')
plt.axis('off')
plt.show()

#PART C:
The Red, Green, and Blue channels represent the intensity of their respective colors
in the image. Brighter areas in each channel indicate stronger color intensity, while
darker areas show less or no presence of that color. Each channel is visualized in
grayscale, highlighting how much red, green, or blue contributes to the original image.
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

LAB TASK 03:.


import cv2
import matplotlib.pyplot as plt
image_path='c:/Users/Hp/OneDrive/Desktop/Temp/standard_test_images/lena_color_512.tif'
image = cv2.imread(image_path)
plt.imshow(image)
plt.title('Lena Color Image (BGR) part A\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.show()
#part B
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
plt.title('Lena Color Image (RGB) part B\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.show()
#part C
image_gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
cv2.imwrite('lena_grayscale.png', image_gray)
plt.imshow(image_gray, cmap='gray')
plt.title('Grayscale Image part C\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.show()
#part D
_, binary_img = cv2.threshold(image_gray, 0, 255, cv2.THRESH_BINARY +
cv2.THRESH_OTSU)
cv2.imwrite('lena_binary.jpg', binary_img)
plt.imshow(binary_img, cmap='gray')
plt.title('Binary Image Part D\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.show()
#part E
plt.figure(figsize=(10, 10))
plt.subplot(2, 2, 1)
plt.imshow(image_rgb)
plt.title('RGB Image\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.subplot(2, 2, 2)
plt.imshow(image)
plt.title('BGR Image\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(image_gray, cmap='gray')
plt.title('Grayscale Image\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(binary_img, cmap='gray')
plt.title('Binary Image\nHAMNAH (21-EE-38)')
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

plt.axis('off')
plt.tight_layout()
plt.show()
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

LAB TASK 04:.


import cv2
import numpy as np
import matplotlib.pyplot as plt
image_path = 'c:/Users/Hp/OneDrive/Desktop/Temp/standard_test_images/cameraman.tif'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
#PART A
image_flipped_ud = np.flipud(image)
#PART B
image_flipped_lr = np.fliplr(image)
#PART A
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image\nHAMNAH 21-EE-38')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(image_flipped_ud, cmap='gray')
plt.title('Upside-Down Image\nHAMNAH 21-EE-38')
plt.axis('off')
plt.suptitle('Task 2.a: Upside-down Flip\nHAMNAH 21-EE-38', fontsize=16)
plt.tight_layout()
plt.show()
#PART B
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image\nHAMNAH 21-EE-38')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(image_flipped_lr, cmap='gray')
plt.title('Left-Right Image\nHAMNAH 21-EE-38')
plt.axis('off')
plt.suptitle('Task 2.b: Left-Right Flip\nHAMNAH 21-EE-38', fontsize=16)
plt.tight_layout()
plt.show()
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING

LAB TASK 05:.


#PART A
import numpy as np
import matplotlib.pyplot as plt
cheque_pattern = np.zeros((256, 256), dtype=np.uint8)
cheque_pattern[:128, :128] = 0
cheque_pattern[:128, 128:] = 192
cheque_pattern[128:, :128] = 85
cheque_pattern[128:, 128:] = 128
plt.imshow(cheque_pattern, cmap='gray')
plt.title('256 × 256 Cheque Pattern\nHAMNAH 21-EE-38', fontsize=16)
plt.axis('off')
plt.show()
#PART B:
import numpy as np
import matplotlib.pyplot as plt
def create_stripe_pattern(rows, cols):
pattern = np.zeros((rows, cols), dtype=np.uint8)
stripe_width = cols // 4
pattern[:, 0:stripe_width] = 0 # Black
pattern[:, stripe_width:2*stripe_width] = 255 # White
pattern[:, 2*stripe_width:3*stripe_width] = 64 # Dark Gray
pattern[:, 3*stripe_width:4*stripe_width] = 192 # Light Gray
return pattern
pattern_512 = create_stripe_pattern(512, 512)
pattern_256_1024 = create_stripe_pattern(256, 1024)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(pattern_512, cmap='gray')
plt.title('512 x 512 Image\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(pattern_256_1024, cmap='gray')
plt.title('256 x 1024 Image\nHAMNAH (21-EE-38)')
plt.axis('off')
plt.tight_layout()
plt.show()

You might also like