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

Ip Lab

Uploaded by

asnakeketema12
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)
19 views8 pages

Ip Lab

Uploaded by

asnakeketema12
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

Digital Image Processing Using Jupyter Lab

Digital image processing using Jupyter Lab can be a powerful way to


manipulate and analyze images interactively. Here’s a basic outline of
how to get started, including some popular libraries and example code
snippets.

Setting Up Jupyter Lab


1. Install Jupyter Lab: If you haven't already, you can install
Jupyter Lab using pip:

pip install jupyterlab


2. Install Required Libraries: You’ll need libraries such as NumPy,
Matplotlib, and OpenCV or PIL. Install them using:

pip install numpy matplotlib opencv-python pillow

3. Startup/Run the working space:

jupyter lab

WHAT IS CV2?
CV2 is the module in OpenCV (Open Source Computer Vision Library)
that provides a wide range of functions for computer vision and image
processing tasks. It is typically imported in Python code using: - import cv2

Key Features of cv2

1. Image I/O: Functions to read, write, and display images.


o cv2.imread(): Load an image from a file.
o cv2.imwrite(): Save an image to a file.
o cv2.imshow(): Display an image in a window.
2. Image Processing: Functions for manipulating images, including
filtering, transformations, and color space conversions.
o cv2.cvtColor(): Convert between different color spaces
(e.g., BGR to RGB, RGB to grayscale).
o cv2.GaussianBlur(): Apply Gaussian blur to smooth images.
o cv2.Canny(): Perform edge detection.

Basic Image Processing Tasks


Here are some common tasks you can perform with image processing:

1. Loading and Displaying an Image:-Example


import cv2
import matplotlib.pyplot as plt
# Load an image using OpenCV
image = cv2.imread('path/to/image.jpg')
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()

2. Image Transformation:
o Resizing:

import cv2
import matplotlib.pyplot as plt

# Load an image using OpenCV


image = cv2.imread('path/to/image.jpg')
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()
#resizing
resized_image = cv2.resize(image, (width, height))
plt.imshow(resized_image)
plt.show()
o Contrast Adjustment
import cv2

import matplotlib.pyplot as plt

#contrust adjustment in image enhancment

A=cv2.imread('wine.jpg')

#contrust adjustment

B=A*2

#subplot(1,2,1)

plt.imshow(A)

plt.show()

plt.title('original image')

#subplot(1,2,2)

plt.imshow(B)

plt.title('after adjustment')

plt.show()

o Rotating:

import cv2
import matplotlib.pyplot as plt

# Load an image using OpenCV


image = cv2.imread('path/to/image.jpg')
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()
#Rotating Image
center = (image.shape[1] // 2, image.shape[0] // 2)
matrix = cv2.getRotationMatrix2D(center, angle, scale)
rotated_image = cv2.warpAffine(image, matrix, (image.shape[1],
image.shape[0]))
plt.imshow(rotated_image)
plt.show()

The line center = (image.shape[1] // 2, image.shape[0] // 2) calculates the center


point of an image. Here’s a breakdown:

1. image.shape: This returns a tuple representing the dimensions of the image. For a
typical image, it will be in the format (height, width, channels).
o image.shape[0] gives the height (number of rows).
o image.shape[1] gives the width (number of columns).
2. // 2: This is integer division, which divides the width and height by 2 to find the
midpoint.
3. center: The resulting center variable is a tuple representing the (x, y) coordinates of the
center of the image:
o The first value, image.shape[1] // 2, is the x-coordinate (horizontal position).
o The second value, image.shape[0] // 2, is the y-coordinate (vertical position).

3. Color Space Conversion:


import cv2
import matplotlib.pyplot as plt

# Load an image using OpenCV


image = cv2.imread('path/to/image.jpg')
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()
#Color Convertions
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(gray_image)
plt.show()
4. Filtering:
o Gaussian Blur:

import cv2
import matplotlib.pyplot as plt

# Load an image using OpenCV


image = cv2.imread('path/to/image.jpg')
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()
#Blurred Image
blurred_image = cv2.GaussianBlur(image, (5, 5), 4)
plt.imshow(blurred_image)
plt.show()

1. image: The input image you want to blur.


2. (5, 5): The size of the Gaussian kernel, specified as a tuple. This means the
kernel will be 5x5 pixels. The kernel size should be odd and positive.
3. 4: The standard deviation (sigma) in the X and Y directions. It controls the
amount of blur; a larger value results in a more pronounced blur.

5. Edge Detection: using Canny edge detection algorithm

import cv2
import matplotlib.pyplot as plt
# Load an image using OpenCV
image = cv2.imread('path/to/image.jpg')
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()
#Edge Detection
edges = cv2.Canny(image, 100, 200)
plt.imshow(edges)
plt.show()
1. image: The input image on which edge detection will be
performed. It's typically a grayscale image, but it can also work
with color images (the function will convert it internally).
2. 100: The lower threshold for the hysteresis procedure. Pixels with
intensity gradient values below this threshold are considered non-
edges.
3. 200: The upper threshold for the hysteresis procedure. Pixels with
gradient values above this threshold are considered strong edges.
Those between the two thresholds are considered weak edges and
are retained only if they are connected to strong edges.

6. Saving the Processed Image In Another Location

import cv2
import matplotlib.pyplot as plt
# Load an image using OpenCV
image = cv2.imread('wine.jpg')
# Convert BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image)
plt.axis('off') # Hide axes
plt.show()
#saving in another location/image extension matter, use png or jpg
cv2.imwrite('path/to/save/image.jpg', cv2.cvtColor(image, cv2.COLOR_RGB2GRAY))
Image restoration
import numpy as np

import matplotlib.pyplot as plt

def dft_2d(image):

M, N = image.shape

F = np.zeros((M, N), dtype=complex)

for k in range(M):

for l in range(N):

for m in range(M):

for n in range(N):

F[k, l] += image[m, n] * np.exp(-2j * np.pi * (k * m / M + l * n / N))

return F

def idft_2d(F):

M, N = F.shape

f = np.zeros((M, N), dtype=complex)

for m in range(M):

for n in range(N):

for k in range(M):

for l in range(N):

f[m, n] += F[k, l] * np.exp(2j * np.pi * (k * m / M + l * n / N))

f[m, n] /= (M * N)

return f

# Create a sample image (e.g., a simple checkerboard pattern)

image = np.zeros((8, 8))

image[::2, ::2] = 1

image[1::2, 1::2] = 1

# Perform DFT
F = dft_2d(image)

# Perform Inverse DFT

reconstructed_image = idft_2d(F)

# Plot the original image and its DFT

plt.figure(figsize=(12, 6))

plt.subplot(1, 3, 1)

plt.title('Original Image')

plt.imshow(image, cmap='gray')

plt.axis('off')

plt.subplot(1, 3, 2)

plt.title('Magnitude of DFT')

plt.imshow(np.abs(F), cmap='gray')

plt.axis('off')

plt.subplot(1, 3, 3)

plt.title('Reconstructed Image')

plt.imshow(np.real(reconstructed_image), cmap='gray')

plt.axis('off')

plt.show()

You might also like