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

Assignment 01

Uploaded by

reemadnayak06
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)
2 views

Assignment 01

Uploaded by

reemadnayak06
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/ 10

import cv2

import matplotlib.pyplot as plt


import numpy as np
print(cv2.__version__) # Prints the version of OpenCV

4.10.0

import cv2
# Read an image in grayscale
image = cv2.imread(r'C:\Users\reema\OneDrive\Desktop\DIP\Fig0338(a)
(blurry_moon).tif', 0) # 0 for grayscale
# Read an image in color
image_color = cv2.imread(r'C:\Users\reema\OneDrive\Desktop\DIP\pexels-
mccutcheon-1191710.jpg', 1) # 1 for color (RGB)

# Convert the BGR image to RGB for proper display in matplotlib


image_color_rgb = cv2.cvtColor(image_color, cv2.COLOR_BGR2RGB)

# Display grayscale image


plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')

# Display color image


plt.subplot(1, 2, 2)
plt.imshow(image_color_rgb)
plt.title('Color Image')
plt.axis('off')

plt.show()
# Resize using specific dimensions (200x200 pixels)
resized_image = cv2.resize(image, (200, 200))
resized_image_color = cv2.resize(image_color, (200, 200))

# Resize using scale factors (reduce size by 50%)


scaled_image = cv2.resize(image, None, fx=0.5, fy=0.5,
interpolation=cv2.INTER_LINEAR)
scaled_image_color = cv2.resize(image_color, None, fx=0.5, fy=0.5,
interpolation=cv2.INTER_LINEAR)

# Convert the color images to RGB for proper display in matplotlib


image_color_rgb = cv2.cvtColor(image_color, cv2.COLOR_BGR2RGB)
resized_image_color_rgb = cv2.cvtColor(resized_image_color,
cv2.COLOR_BGR2RGB)
scaled_image_color_rgb = cv2.cvtColor(scaled_image_color,
cv2.COLOR_BGR2RGB)

# Display all images in the same window


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

# Resized Grayscale Image


plt.subplot(2, 3, 1)
plt.imshow(resized_image, cmap='gray')
plt.title('Resized Grayscale Image (200x200)')
plt.axis('off')

# Resized Color Image


plt.subplot(2, 3, 2)
plt.imshow(resized_image_color_rgb)
plt.title('Resized Color Image (200x200)')
plt.axis('off')

plt.tight_layout()
plt.show()

# Rotate the image 90 degrees clockwise


rotated_image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)

# Display the original and rotated images side by side


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

# Original Image
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

# Rotated Image
plt.subplot(1, 2, 2)
plt.imshow(rotated_image, cmap='gray')
plt.title('Rotated Image (90° Clockwise)')
plt.axis('off')

plt.tight_layout()
plt.show()
# Load two grayscale images
image1 = cv2.imread(r'C:\Users\reema\OneDrive\Desktop\DIP\image1.bmp',
cv2.IMREAD_GRAYSCALE) # Replace with your image path
image2 = cv2.imread(r'C:\Users\reema\OneDrive\Desktop\DIP\image2.bmp',
cv2.IMREAD_GRAYSCALE) # Replace with your image path

# Check and resize if dimensions don't match


if image1.shape != image2.shape:
image2 = cv2.resize(image2, (image1.shape[1], image1.shape[0]))

# Add the images


added_image = cv2.add(image1, image2)

# Display the result


plt.imshow(added_image, cmap='gray')
plt.title('Added Grayscale Image')
plt.axis('off')
plt.show()
plt.imshow(image2, cmap='gray')
plt.title('Image 2')
plt.axis('off')
plt.show()
# Calculate histogram for grayscale image
histogram = cv2.calcHist([image2], [0], None, [256], [0, 256])
# Display histogram
import matplotlib.pyplot as plt
plt.plot(histogram)
plt.title('Histogram')
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.show()
# Convert RGB image to grayscale
gray_image = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)

gray_image = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)

# Display the grayscale image using Matplotlib


plt.imshow(gray_image, cmap='gray') # Use 'gray' colormap for
grayscale images
plt.title('Grayscale Image')
plt.axis('off') # Hide the axes for better visualization
plt.show()
# Apply binary thresholding
_, binary_image = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY)

# Display the binary thresholded image using Matplotlib


plt.imshow(binary_image, cmap='gray') # Use 'gray' colormap for
binary images
plt.title('Binary Threshold')
plt.axis('off') # Hide axes for better visualization
plt.show()
# Create a noise array
noise = np.zeros_like(image, dtype=np.float32)
cv2.randn(noise, 0, 25) # Mean=0, StdDev=25

# Add noise to the original image


noisy_image = cv2.add(image.astype(np.float32), noise)
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8) # Clip
values to [0,255]

# Display the noisy image using Matplotlib


plt.imshow(noisy_image, cmap='gray') # Use 'gray' colormap for
grayscale images
plt.title('Noisy Image')
plt.axis('off') # Hide the axes for cleaner visualization
plt.show()

You might also like