Ip Lab
Ip Lab
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
2. Image Transformation:
o Resizing:
import cv2
import matplotlib.pyplot as plt
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
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).
import cv2
import matplotlib.pyplot as plt
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.
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
def dft_2d(image):
M, N = image.shape
for k in range(M):
for l in range(N):
for m in range(M):
for n in range(N):
return F
def idft_2d(F):
M, N = F.shape
for m in range(M):
for n in range(N):
for k in range(M):
for l in range(N):
f[m, n] /= (M * N)
return f
image[::2, ::2] = 1
image[1::2, 1::2] = 1
# Perform DFT
F = dft_2d(image)
reconstructed_image = idft_2d(F)
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()