0% found this document useful (0 votes)
9 views7 pages

Input

Uploaded by

Zahid Hasan
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)
9 views7 pages

Input

Uploaded by

Zahid Hasan
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/ 7

Input: Output:

import cv2
import matplotlib.pyplot as plt
# Load an image using OpenCV
image =
cv2.imread('257803.jpg')
# Convert the image from BGR
to RGB
image_rgb =
cv2.cvtColor(image,
cv2.COLOR_BGR2RGB)
# Display the image using
Matplotlib
plt.imshow(image_rgb)
plt.title('Image Loaded with
OpenCV')
plt.axis('off')
plt.show()

Input: Output:

from PIL import Image


import matplotlib.pyplot as plt
# Load an image using Pillow
image =
Image.open('257803.jpg')

# Display the image using


Matplotlib
plt.imshow(image)
plt.title('Image Loaded with
Pillow')
plt.axis('off')
plt.show()
Input: Output:
import cv2

# Resize an image
image =
cv2.imread('257803.jpg')
resized_image =
cv2.resize(image, (1050, 1610))

# Display the resized image


plt.imshow(cv2.cvtColor(resized
_
image, cv2.COLOR_BGR2RGB))
plt.title('Resized Image')
plt.axis('off')
plt.show()

Input: Output:
# Convert image to
grayscale
gray_image =
cv2.cvtColor(image,
cv2.COLOR_BGR2GRAY)

# Display the grayscale


image
plt.imshow(gray_image,
cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')
plt.show()
Input: Output: (-0.5, 399.5, 499.5, -0.5)
import cv2
import matplotlib.pyplot as plt

# Load the image in grayscale


image =
cv2.imread('257803.jpg',
cv2.IMREAD_GRAYSCALE)

# Display the original image


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

Input: Output:

# Calculate the histogram


hist = cv2.calcHist([image],
[0], None, [256], [0, 256])

# Plot the histogram


plt.subplot(1, 2, 2)
plt.plot(hist, color='black')
plt.title('Histogram')
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.xlim([0, 256])
plt.show()
Input: Output:
# Apply histogram
equalization using OpenCV

equalized_image =
cv2.equalizeHist(gray_image)

# Display the equalized image


plt.imshow(equalized_image,
cmap='gray')
plt.title('Equalized Image')
plt.axis('off')
plt.show()

Input: Output:
# Calculate the
histogram of the
equalized image
equalized_hist =
cv2.calcHist([equalized
_
image], [0], None,
[256], [0, 256])
# Plot the histogram of
the equalized image
plt.plot(equalized_hist)
plt.title('Histogram of
Equalized Image')
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.show()

Input: Output:
# Apply Gaussian blur
blurred_image =
cv2.GaussianBlur(image, (15, 15), 0)

# Display the blurred image


plt.imshow(cv2.cvtColor(blurred_ima
ge, cv2.COLOR_BGR2RGB))
plt.title('Blurred Image')
plt.axis('off')
plt.show()

Input: Output:

import numpy as np

# Apply sharpening kernel


kernel = np.array([[0, -1, 0], [-1, 5, -
1], [0, -1, 0]])
print(kernel)
sharpened_image =
cv2.filter2D(image, -1, kernel)

# Display the sharpened image


plt.imshow(cv2.cvtColor(sharpened
_image, cv2.COLOR_BGR2RGB))
plt.title('Sharpened Image')
plt.axis('off')
plt.show()
Input: Output:
# Apply Canny edge detection
image =
cv2.imread('257803.jpg',
cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(gray_image,
threshold1=50,
threshold2=150)

# Display the edges


plt.imshow(edges, cmap='gray')
plt.title('Edge Detection')
plt.axis('off')
plt.show()

Input: Output:

"""**Image Segmentation**"""

# Apply binary thresholding


_, binary_image =
cv2.threshold(gray_image,
127, 255,
cv2.THRESH_BINARY)

# Display the binary image

plt.imshow(binary_image,
cmap='gray')
plt.title('Binary Image')
plt.axis('off')
plt.show()

You might also like