-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task_1 (Person).R
207 lines (152 loc) · 6.4 KB
/
Task_1 (Person).R
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
library(imager)
## Person ##
# Load the flower images
person1 <- load.image("C:/Users/User/UKM - Nur Azlin Binti Rusnan/Sem 2/Unstructured Data/Project 3/Images/Person1.jpg")
person2 <- load.image("C:/Users/User/UKM - Nur Azlin Binti Rusnan/Sem 2/Unstructured Data/Project 3/Images/Person2.jpg")
# Display the images
plot(person1, main = "Person 1")
plot(person2, main = "Person 2")
## Analysis ##
# i. Edge detection
# Compute image gradients
grad_person1 <- imgradient(person1, "xy")
grad_person2 <- imgradient(person2, "xy")
# Compute gradient magnitude
grad_mag_person1 <- sqrt(grad_person1$x^2 + grad_person1$y^2)
grad_mag_person2 <- sqrt(grad_person2$x^2 + grad_person2$y^2)
# Plot the original and gradient magnitude images
par(mfrow = c(2, 2))
plot(person1, main = "Original person1")
plot(grad_mag_person1, main = "Gradient Magnitude of Person 1")
plot(person2, main = "Original Person 2")
plot(grad_mag_person2, main = "Gradient Magnitude of Person 2")
# ii. Splitting & concatenating image
# Get dimensions
height1 <- dim(person1)[1]
width1 <- dim(person1)[2]
height2 <- dim(person2)[1]
width2 <- dim(person2)[2]
# Split person1
person1_top <- subim(person1, y <= height1 / 2)
person1_bottom <- subim(person1, y > height1 / 2)
# Split person2
person2_top <- subim(person2, y <= height2 / 2)
person2_bottom <- subim(person2, y > height2 / 2)
# Concatenate the segments
# Here we concatenate the top half of person1 with the bottom half of person2 and vice versa
concat_image1 <- imappend(list(person1_top, person2_bottom), axis = "y")
concat_image2 <- imappend(list(person2_top, person1_bottom), axis = "y")
# Plot the concatenated images
par(mfrow = c(1, 2))
plot(concat_image1, main = "Person 1 Top + Person 2 Bottom")
plot(concat_image2, main = "Person 2 Top + Person 1 Bottom")
# iii. Filtering image
# Convert images to grayscale
person1_gray <- grayscale(person1)
person2_gray <- grayscale(person2)
# Apply Deriche and Vanvliet filters
deriche_person1 <- deriche(person1_gray, sigma = 4, order = 2, axis = "y")
vanvliet_person1 <- vanvliet(person1_gray, sigma = 4, order = 2, axis = "y")
deriche_person2 <- deriche(person2_gray, sigma = 4, order = 2, axis = "y")
vanvliet_person2 <- vanvliet(person2_gray, sigma = 4, order = 2, axis = "y")
# Plot the original and filtered images
par(mfrow = c(2, 2))
plot(person1, main = "Original Person 1")
plot(deriche_person1, main = "Deriche Filtered Person 1")
plot(vanvliet_person1, main = "Vanvliet Filtered Person 1")
par(mfrow = c(2, 2))
plot(person2, main = "Original Person 2")
plot(deriche_person2, main = "Deriche Filtered Person 2")
plot(vanvliet_person2, main = "Vanvliet Filtered Person 2")
# iv. Blurry & sharpen
# Blurry & Sharpen
# Apply Gaussian filter with increased sigma for noticeable blurring
gaussian_person1 <- isoblur(person1, sigma = 10)
gaussian_person2 <- isoblur(person2, sigma = 10)
# Define a sharpening kernel
sharpening_kernel <- array(c(-1, -1, -1,
-1, 9, -1,
-1, -1, -1), c(3, 3, 1, 1))
# Apply sharpening filter using convolve function
sharpen_person1 <- imager::convolve(person1, sharpening_kernel)
sharpen_person2 <- imager::convolve(person2, sharpening_kernel)
# Plot the original, blurred, and sharpened images
par(mfrow = c(2, 3))
plot(person1, main = "Original Person 1")
plot(gaussian_person1, main = "Gaussian Blurred Person 1")
plot(sharpen_person1, main = "Sharpened Person 1")
plot(person2, main = "Original Person 2")
plot(gaussian_person2, main = "Gaussian Blurred Person 2")
plot(sharpen_person2, main = "Sharpened Person 2")
# v. Segmentation
# Apply thresholding for segmentation
threshold <- 0.5
segmented_person1 <- person1 > threshold
segmented_person2 <- person2 > threshold
# Plot the original and segmented images
par(mfrow = c(2, 2))
plot(person1, main = "Original Person 1")
plot(segmented_person1, main = "Segmented Person 1")
plot(person2, main = "Original Person 2")
plot(segmented_person2, main = "Segmented Person 2")
# Histogram Equalization
# Function for histogram equalization
hist_equalize <- function(image) {
hist <- hist(image, plot = FALSE)$counts
cdf <- cumsum(hist) / sum(hist)
equalized <- as.numeric(approx(x = seq(0, 1, length.out = length(hist)), y = cdf, xout = image)$y)
dim(equalized) <- dim(image)
return(as.cimg(equalized))
}
# Perform histogram equalization
person1_eq <- hist_equalize(person1)
person2_eq <- hist_equalize(person2)
# Plot the original and equalized images
par(mfrow = c(2, 2))
plot(person1, main = "Original Person 1")
plot(person1_eq, main = "Equalized Person 1")
plot(person2, main = "Original Person 2")
plot(person2_eq, main = "Equalized Person 2")
# vii. Morphological operations
# Convert to grayscale
#person1_gray <- grayscale(person1)
#person2_gray <- grayscale(person2)
# Define the structuring element (disk shape)
selem <- imfill(11, 11, val = 1) %>% as.cimg() %>% dilate_square(5)
# Apply erosion
eroded_person1 <- erode(person1, selem)
eroded_person2 <- erode(person2, selem)
# Apply dilation
dilated_person1 <- dilate(person1, selem)
dilated_person2 <- dilate(person2, selem)
# Apply opening
opened_person1 <- erode(person1, selem) %>% dilate(selem)
opened_person2 <- erode(person2, selem) %>% dilate(selem)
# Apply closing
closed_person1 <- dilate(person1, selem) %>% erode(selem)
closed_person2 <- dilate(person2, selem) %>% erode(selem)
# Plotting the results
par(mfrow = c(1, 2))
plot(person1, main = "Original Person 1")
plot(eroded_person1, main = "Eroded Person 1")
par(mfrow = c(1, 2))
plot(person2, main = "Original Person 2")
plot(eroded_person2, main = "Eroded Person 2")
par(mfrow = c(1, 2))
plot(person1, main = "Original Person 1")
plot(dilated_person1, main = "Dilated Person 1")
par(mfrow = c(1, 2))
plot(person2, main = "Original Person 2")
plot(dilated_person2, main = "Dilated Person 2")
par(mfrow = c(1, 2))
plot(person1, main = "Original Person 1")
plot(opened_person1, main = "Opened Person 1")
par(mfrow = c(1, 2))
plot(person2, main = "Original Person 2")
plot(opened_person2, main = "Opened Person 2")
par(mfrow = c(1, 2))
plot(person1, main = "Original Person 1")
plot(closed_person1, main = "Closed Person 1")
par(mfrow = c(1, 2))
plot(person2, main = "Original Person 2")
plot(closed_person2, main = "Closed Person 2")