-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnomain.py
More file actions
72 lines (54 loc) · 2.22 KB
/
nomain.py
File metadata and controls
72 lines (54 loc) · 2.22 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
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
# Step 1: Load image
img_rgb = cv.imread('ferry.jpg') # Replace with your image path
# Step 2: Convert RGB to Grayscale
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
# Step 3: Apply Padding (border reflect) with 10% of the image size
padded_img = cv.copyMakeBorder(img_gray, 44, 44, 39, 39, cv.BORDER_REFLECT)
# Step 4: Histogram Equalization (applied after grayscale conversion)
equalized_img = cv.equalizeHist(padded_img)
# Step 5: Apply edge detection with different kernels
kernel1 = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
kernel2 = np.array([[1, 0, -1],
[1, 0, -1],
[1, 0, -1]])
# Edge detection using Laplacian kernel
edge1 = cv.filter2D(equalized_img, -1, kernel1)
# Edge detection using Sobel kernel
edge2 = cv.filter2D(equalized_img, -1, kernel2)
# Step 6: Plot in a 3x3 Grid Layout
fig, axs = plt.subplots(3, 3, figsize=(12, 12))
# Top row: Original image, Grayscale, and Padded image
axs[0, 0].imshow(cv.cvtColor(img_rgb, cv.COLOR_BGR2RGB))
axs[0, 0].set_title('Citra RGB')
axs[0, 0].axis('off')
axs[0, 1].imshow(img_gray, cmap='gray')
axs[0, 1].set_title('Citra Gray')
axs[0, 1].axis('off')
axs[0, 2].imshow(padded_img, cmap='gray')
axs[0, 2].set_title('Padded Image (Border Reflect)')
axs[0, 2].axis('off')
# Middle row: Equalized image, Edge detection with kernel 1, Edge detection with kernel 2
axs[1, 0].imshow(equalized_img, cmap='gray')
axs[1, 0].set_title('Equalized Image')
axs[1, 0].axis('off')
axs[1, 1].imshow(edge1, cmap='gray')
axs[1, 1].set_title('Extract Edge Kernel 1')
axs[1, 1].axis('off')
axs[1, 2].imshow(edge2, cmap='gray')
axs[1, 2].set_title('Extract Edge Kernel 2')
axs[1, 2].axis('off')
# Bottom row: Histograms for grayscale image, equalized image, and edge 2 result
axs[2, 0].hist(img_gray.ravel(), bins=256, range=[0, 256])
axs[2, 0].set_title('Histogram Citra Gray')
axs[2, 1].hist(equalized_img.ravel(), bins=256, range=[0, 256])
axs[2, 1].set_title('Histogram Equalized Image')
axs[2, 2].hist(edge2.ravel(), bins=256, range=[0, 256])
axs[2, 2].set_title('Histogram Edge Kernel 2')
# Adjust layout and display the plot
plt.tight_layout()
plt.show()