-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
92 lines (70 loc) · 2.65 KB
/
index.py
File metadata and controls
92 lines (70 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 1. Load original image (replace 'image.jpg' with your image path)
image = cv2.imread('ferry.jpg')
# Convert BGR to RGB for displaying
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 2. Convert image to grayscale
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 3. Function to plot histogram
def plot_histogram(image, title):
plt.hist(image.ravel(), bins=256, range=[0, 256])
plt.title(title)
plt.show()
# 4. Apply histogram equalization for image enhancement
image_equalized = cv2.equalizeHist(image_gray)
# 6. Apply reflect padding
image_padded = cv2.copyMakeBorder(image_rgb, 50, 50, 50, 50, cv2.BORDER_REFLECT)
# 7. Edge detection using Laplacian
laplacian_edges = cv2.Laplacian(image_gray, cv2.CV_64F)
# 8. Edge detection using Sobel
sobelx = cv2.Sobel(image_gray, cv2.CV_64F, 1, 0, ksize=3) # x direction
sobely = cv2.Sobel(image_gray, cv2.CV_64F, 0, 1, ksize=3) # y direction
sobel_edges = cv2.magnitude(sobelx, sobely)
# 9. Negative or Invert Transformation (without using library)
def invert_image(image):
return 255 - image
image_inverted = invert_image(image_gray)
# Create the grid layout with 5x2 subplots
fig, axes = plt.subplots(5, 2, figsize=(15, 20))
# 1. Original image
axes[0, 0].imshow(image_rgb)
axes[0, 0].set_title("Original Image")
axes[0, 0].axis('off')
# 2. Grayscale image
axes[0, 1].imshow(image_gray, cmap='gray')
axes[0, 1].set_title("Grayscale Image")
axes[0, 1].axis('off')
# 3. Histogram of grayscale image
axes[1, 0].hist(image_gray.ravel(), bins=256, range=[0, 256])
axes[1, 0].set_title("Histogram of Grayscale Image")
# 4. Equalized image
axes[1, 1].imshow(image_equalized, cmap='gray')
axes[1, 1].set_title("Equalized Image")
axes[1, 1].axis('off')
# 5. Histogram of equalized image
axes[2, 0].hist(image_equalized.ravel(), bins=256, range=[0, 256])
axes[2, 0].set_title("Histogram of Equalized Image")
# 6. Padded image (reflect)
axes[2, 1].imshow(image_padded)
axes[2, 1].set_title("Padded Image (Reflect)")
axes[2, 1].axis('off')
# 7. Laplacian edge detection
axes[3, 0].imshow(laplacian_edges, cmap='gray')
axes[3, 0].set_title("Laplacian Edge Detection")
axes[3, 0].axis('off')
# 8. Sobel edge detection
axes[3, 1].imshow(sobel_edges, cmap='gray')
axes[3, 1].set_title("Sobel Edge Detection")
axes[3, 1].axis('off')
# 9. Inverted image
axes[4, 0].imshow(image_inverted, cmap='gray')
axes[4, 0].set_title("Inverted Image")
axes[4, 0].axis('off')
# 10. Histogram of inverted image
axes[4, 1].hist(image_inverted.ravel(), bins=256, range=[0, 256])
axes[4, 1].set_title("Histogram of Inverted Image")
# Adjust layout and display
plt.tight_layout()
plt.show()