IVP Lab
Image and Video Processing Lab
Suggested list of Assignments
Steps to be followed to execute these below assignments.
Install latest version of python from the python official website.
Install numpy for numerical operations for manipulating pixels in image processing (use the command in CMD: "pip install numpy").
Install Matplotlib for data visualization, scientific plotting, and creating various types of charts and graphs. (use command in CMD: "pip install matplotlib ").
Install OpenCV (Open Source Computer Vision Library) is a popular open-source computer vision and image processing library for Python. It provides a wide range of functionalities for working with images and videos. (use the command in CMD: "pip install python-opencv").
Make sure path of the image in your system as per the image location.
Assignment no.1: Apply any 4 point processing techniques on sample image.
i. Image Negation:
import cv2
import numpy as np
# Load an image from file
image = cv2.imread('food.jpg')
# Ensure the image is not None
if image is not None:
# Negate the image
negated_image = 255 - image
# Display the original and negated images (optional)
cv2.imshow('Original Image', image)
cv2.imshow('Negated Image', negated_image)
# Save the negated image
cv2.imwrite('negated_image.jpg', negated_image)
# Wait for a key press and close windows
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Error loading the image.")
ii. Thresholding:
import cv2
import numpy as np
# Load an image from file in grayscale
image = cv2.imread('cameraman.jpg', cv2.IMREAD_GRAYSCALE)
# Ensure the image is not None
if image is not None:
# Set a threshold value (you may need to adjust this based on your image)
threshold_value = 127
# Apply thresholding
_, thresholded_image = cv2.threshold(image, threshold_value, 255, cv2.THRESH_BINARY)
# Display the original and thresholded images (optional)
cv2.imshow('Original Image', image)
cv2.imshow('Thresholded Image', thresholded_image)
# Save the thresholded image
cv2.imwrite('thresholded_image.jpg', thresholded_image)
# Wait for a key press and close windows
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Error loading the image.")
iii. Gray level slicing:
import cv2
# Load an image from file
image = cv2.imread('food.jpg')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Save the grayscale image
cv2.imwrite('grayscale_image.jpg', gray_image)
# Display the original and grayscale images (optional)
cv2.imshow('Original Image', image)
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
iv. Bit plane slicing:
import cv2
import numpy as np
# Load an image from file in grayscale
image = cv2.imread('cameraman.jpg', cv2.IMREAD_GRAYSCALE)
# Ensure the image is not None
if image is not None:
# Get the height and width of the image
height, width = image.shape
# Initialize an array to store the bit planes
bit_planes = np.zeros((8, height, width), dtype=np.uint8)
# Iterate through each bit position (from 0 to 7)
for bit_position in range(8):
# Extract the bit plane by masking with 2^bit_position
bit_planes[bit_position] = (image >> bit_position) & 1
# Convert the bit plane to a 0-255 range for visualization
bit_planes[bit_position] *= 255
# Display the bit plane (optional)
cv2.imshow(f'Bit Plane {bit_position}', bit_planes[bit_position])
# Wait for a key press and close windows
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Error loading the image.")
v. Darkening and Lightning of image:
import cv2
import numpy as np
# Load an image from file
image = cv2.imread('cameraman1.jpg')
# Ensure the image is not None
if image is not None:
# Define a constant value for darkening or lightening
brightness_change = 50 # You can adjust this value based on your needs
# Darken the image
darkened_image = np.clip(image - brightness_change, 0, 255)
# Lighten the image
lightened_image = np.clip(image + brightness_change, 0, 255)
# Display the original, darkened, and lightened images (optional)
cv2.imshow('Original Image', image)
cv2.imshow('Darkened Image', darkened_image.astype(np.uint8))
cv2.imshow('Lightened Image', lightened_image.astype(np.uint8))
# Save the darkened and lightened images
cv2.imwrite('darkened_image.jpg', darkened_image.astype(np.uint8))
cv2.imwrite('lightened_image.jpg', lightened_image.astype(np.uint8))
# Wait for a key press and close windows
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Error loading the image.")
Assignment no.2: Apply any 4 mask processing techniques on sample image.
import cv2
import numpy as np
# Read the input image
image = cv2.imread('F:/PCET/TY B.Tech/Image and Video Processing/IVP_Lab/cameraman.jpg')
# Ensure the image is not None
if image is not None:
# Apply the average filter with a kernel size of 5x5
average_filter = cv2.blur(image, (5, 5))
median_filter = cv2.medianBlur(image, 5)
gaussian_image = cv2.GaussianBlur(image, (5, 5), 0)
# Apply Laplacian filter
laplacian = cv2.Laplacian(image, cv2.CV_64F)
# Convert the result to uint8 and normalize
laplacian = cv2.convertScaleAbs(laplacian)
kernel = np.array( [[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]] )
# Apply the high boost filter
high_boost_image = cv2.filter2D(image, -1, kernel)
# Display the original and filtered images
cv2.imshow('Original Image', image)
cv2.imshow('Average_filter', average_filter)
cv2.imshow('Median_filter', median_filter)
cv2.imshow('Gaussian_image', gaussian_image)
cv2.imshow('Laplacian_image', laplacian)
cv2.imshow('High Boost Filtered Image', high_boost_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Error loading the image.")
Assignment no.3: Plot the histogram of an image and perform histogram equalization.
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the image
image = cv2.imread('F:/PCET/TY B.Tech/Sem2/Image and Video Processing/IVP_Lab/cameraman.jpg', cv2.IMREAD_GRAYSCALE)
# Perform histogram equalization
equalized_image = cv2.equalizeHist(image)
# Plot histograms
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.hist(image.flatten(), 256, [0, 256], color='b')
plt.title('Original Image Histogram')
plt.subplot(1, 2, 2)
plt.hist(equalized_image.flatten(), 256, [0, 256], color='r')
plt.title('Equalized Image Histogram')
plt.show()
# Display original and equalized images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(equalized_image, cmap='gray')
plt.title('Equalized Image')
plt.axis('off')
plt.show()
Assignment no.4: Perform Edge detection using Sobel, Prewitt and Roberts gradient operators.
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the image
image = cv2.imread('F:/PCET/TY B.Tech/Sem2/Image and Video Processing/IVP_Lab/cameraman.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Sobel operator
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
sobel = np.sqrt(sobel_x**2 + sobel_y**2)
# Apply Prewitt operator
prewitt_x = cv2.filter2D(image, -1, np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]))
prewitt_y = cv2.filter2D(image, -1, np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]))
prewitt = np.sqrt(prewitt_x**2 + prewitt_y**2)
# Apply Roberts operator
roberts_x = cv2.filter2D(image, -1, np.array([[1, 0], [0, -1]]))
roberts_y = cv2.filter2D(image, -1, np.array([[0, 1], [-1, 0]]))
roberts = np.sqrt(roberts_x**2 + roberts_y**2)
# Plotting the results
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.subplot(2, 2, 2)
plt.imshow(sobel, cmap='gray')
plt.title('Sobel Edge Detection')
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(prewitt, cmap='gray')
plt.title('Prewitt Edge Detection')
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(roberts, cmap='gray')
plt.title('Roberts Edge Detection')
plt.axis('off')
plt.show()
Assignment no.5: Compress given image using cosine transform.
import cv2
import numpy as np
def compress_image(img, block_size=8, quality=50):
# Convert image to grayscale
if len(img.shape) == 3:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Pad image to make its dimensions multiples of block size
h, w = img.shape
padded_h = ((h + block_size - 1) // block_size) * block_size
padded_w = ((w + block_size - 1) // block_size) * block_size
padded_img = np.zeros((padded_h, padded_w), dtype=np.uint8)
padded_img[:h, :w] = img
# Apply DCT to each block
dct_img = np.zeros_like(padded_img, dtype=np.float32)
for y in range(0, padded_h, block_size):
for x in range(0, padded_w, block_size):
block = padded_img[y:y+block_size, x:x+block_size].astype(np.float32)
dct_block = cv2.dct(block)
dct_img[y:y+block_size, x:x+block_size] = dct_block
# Quantization
quantization_matrix = np.array([[16, 11, 10, 16, 24, 40, 51, 61],
[12, 12, 14, 19, 26, 58, 60, 55],
[14, 13, 16, 24, 40, 57, 69, 56],
[14, 17, 22, 29, 51, 87, 80, 62],
[18, 22, 37, 56, 68, 109, 103, 77],
[24, 35, 55, 64, 81, 104, 113, 92],
[49, 64, 78, 87, 103, 121, 120, 101],
[72, 92, 95, 98, 112, 100, 103, 99]])
quantized_img = np.zeros_like(dct_img, dtype=np.float32)
for y in range(0, padded_h, block_size):
for x in range(0, padded_w, block_size):
dct_block = dct_img[y:y+block_size, x:x+block_size]
quantized_block = np.round(dct_block / (quantization_matrix * (float(quality) / 100)))
quantized_img[y:y+block_size, x:x+block_size] = quantized_block
# Compression: Store only significant coefficients
# Reconstruction
reconstructed_img = np.zeros_like(quantized_img, dtype=np.uint8)
for y in range(0, padded_h, block_size):
for x in range(0, padded_w, block_size):
quantized_block = quantized_img[y:y+block_size, x:x+block_size]
idct_block = cv2.idct(quantized_block)
reconstructed_img[y:y+block_size, x:x+block_size] = idct_block
return reconstructed_img[:h, :w]
# Load image
image = cv2.imread("F:/PCET/TY B.Tech/Sem2/Image and Video Processing/IVP_Lab/cameraman.jpg")
# Compress image
compressed_image = compress_image(image)
# Normalize pixel values to [0, 255]
compressed_image_normalized = cv2.normalize(compressed_image, None, 0, 255, cv2.NORM_MINMAX)
# Display original and compressed images
cv2.imshow("Original Image", image)
cv2.imshow("Compressed Image", compressed_image_normalized)
cv2.waitKey(0)
cv2.destroyAllWindows()
Assignment no.6: Extract the visual part from digital video and perform point processing method on video frames
import cv2
# Function to apply point processing method to a frame
def point_processing(frame):
# Example: Convert to grayscale
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Apply additional point processing methods as needed
return gray_frame
# Input and output video filenames
input_video_filename = "F:/PCET/TY B.Tech/Sem2/Image and Video Processing/IVP_Lab/input_video1.mp4"
output_video_filename = "output_video.mp4"
# Open the video file
video_capture = cv2.VideoCapture(input_video_filename)
# Get video properties
frame_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_rate = int(video_capture.get(cv2.CAP_PROP_FPS))
frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_video = cv2.VideoWriter(output_video_filename, fourcc, frame_rate, (frame_width, frame_height))
# Process each frame in the video
while True:
# Read a frame from the video
ret, frame = video_capture.read()
# If no frame is retrieved, break the loop
if not ret:
break
# Apply point processing method to the frame
processed_frame = point_processing(frame)
# Write the processed frame to the output video
output_video.write(processed_frame)
# Display the processed frame (optional)
cv2.imshow('Processed Frame', processed_frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
# Release video objects
video_capture.release()
output_video.release()
cv2.destroyAllWindows()