-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcluster.lisp
282 lines (265 loc) · 13.3 KB
/
cluster.lisp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
;;; Copyright (c) 2011 Cyrus Harmon, All rights reserved.
;;; See COPYRIGHT file for details.
(in-package :opticl)
;; normally we'd take the sqrt here, but since we're just comparing
;; distances, we can compare the squared distances, so we'll elide the
;; sqrt call.
(defun l2-distance (pixel1 pixel2)
(reduce #'+ (map 'vector (lambda (p q)
(let ((d (- p q)))
(* d d)))
pixel1 pixel2)))
(defun l2-distance-3 (pixel1a pixel1b pixel1c pixel2a pixel2b pixel2c)
(declare (type fixnum pixel1a pixel1b pixel1c pixel2a pixel2b pixel2c)
(optimize (speed 3) (safety 0)))
(let ((d1 (- pixel1a pixel2a))
(d2 (- pixel1b pixel2b))
(d3 (- pixel1c pixel2c)))
(declare (type fixnum d1 d2 d3))
(the fixnum (+ (the fixnum (* d1 d1))
(the fixnum (* d2 d2))
(the fixnum (* d3 d3))))))
(declaim (ftype (function (fixnum fixnum fixnum fixnum fixnum fixnum) fixnum) l2-distance-3))
(defun k-means-cluster-image-pixels (image k &key (max-iterations 20))
(declare (type fixnum k))
(typecase image
(8-bit-gray-image
(locally
(declare (type 8-bit-gray-image image))
(with-image-bounds (height width channels)
image
(let ((means (make-array k :element-type '(unsigned-byte 32) :initial-element 0))
(counts (make-array k :element-type 'fixnum))
(z (make-array (list height width) :element-type 'fixnum)))
(declare (type (simple-array (unsigned-byte 32) (*)) means)
(type (simple-array fixnum (*)) counts)
(type (simple-array fixnum (* *)) z))
(flet ((recompute-means ()
;; clear out the old values
(declare (optimize (speed 3))
(type 8-bit-gray-image image)
(type fixnum k))
(dotimes (q k)
(declare (type fixnum q))
(setf (aref means q) 0)
(setf (aref counts q) 0))
;; use the means vector first as an accumulator to hold
;; the sums for each channel, later we'll scale by (/
;; num-pixels)
(do-pixels (i j) image
(let ((m (aref z i j)))
(declare (type fixnum m))
(setf (aref means m)
(+ (pixel image i j) (aref means m)))
(let* ((cluster (aref z i j))
(cluster-count (aref counts cluster)))
(setf (aref counts cluster)
(logand most-positive-fixnum (1+ cluster-count))))))
(dotimes (q k)
(when (plusp (aref counts q))
(setf (aref means q)
(round (/ (aref means q) (aref counts q)))))))
(assign-to-means ()
(declare (optimize (speed 3))
(type 8-bit-gray-image image))
(do-pixels (i j) image
(setf (aref z i j)
(let (min-val nearest-mean)
(loop for q below k
do (let ((dist (let ((d (- (pixel image i j) (aref means q))))
(declare (type (signed-byte 16) d))
(the fixnum (* d d)))))
(when (or (null min-val) (< dist min-val))
(setf min-val dist
nearest-mean q))))
nearest-mean)))))
;; randomly assign pixel values to the k means
(loop for i below k
for y = (random height)
for x = (random width)
do (setf (aref means i)
(pixel image y x)))
(loop for iter below max-iterations
with stop = nil
with old-means
until stop
do
(assign-to-means)
(recompute-means)
(when (and old-means (equalp old-means means))
(setf stop t))
(setf old-means (copy-array means)))
(values means z))))))
(8-bit-rgb-image
(locally
(declare (type 8-bit-rgb-image image))
(with-image-bounds (height width channels)
image
(let ((means (make-array (list k 1 3)
:element-type '(unsigned-byte 32)))
(counts (make-array k :element-type 'fixnum))
(z (make-array (list height width) :element-type 'fixnum)))
(declare (type (simple-array fixnum (* *)) z)
(type 32-bit-rgb-image means))
(flet ((recompute-means ()
(declare (type (simple-array fixnum (* *)) z)
(type (simple-array fixnum (*)) counts)
(optimize (speed 3)))
;; clear out the old values
(dotimes (q k)
(setf (pixel means q 0) (values 0 0 0))
(setf (aref counts q) 0))
;; use the means vector first as an accumulator to hold
;; the sums for each channel, later we'll scale by (/
;; num-pixels)
(do-pixels (i j) image
(let ((m (aref z i j)))
(multiple-value-bind (v1 v2 v3)
(pixel image i j)
(multiple-value-bind (m1 m2 m3)
(pixel means m 0)
(setf (pixel means m 0)
(values
(+ v1 m1)
(+ v2 m2)
(+ v3 m3)))))
(let* ((cluster (aref z i j))
(cluster-count (aref counts cluster)))
(setf (aref counts cluster)
(logand #xffffffff (1+ cluster-count))))))
(dotimes (q k)
(when (plusp (aref counts q))
(multiple-value-bind (m1 m2 m3)
(pixel means q 0)
(let ((factor (aref counts q)))
(setf (pixel means q 0)
(values (truncate (/ m1 factor))
(truncate (/ m2 factor))
(truncate (/ m3 factor))))))))
(let ((new-means-list
(loop for count across counts
for i below k
collect (list count (pixel* means i 0)))))
(declare (type list new-means-list))
(loop for i fixnum below k
for (count mean) in (sort new-means-list #'> :key #'first)
do
(setf (pixel* means i 0) mean)
(setf (aref counts i) count))))
(assign-to-means ()
(declare (type 8-bit-rgb-image image)
(optimize (speed 3)))
(do-pixels (i j) image
(setf (aref z i j)
(let (min-val nearest-mean)
(loop for q below k
do (let ((dist (multiple-value-call #'l2-distance-3
(pixel image i j)
(pixel means q 0))))
(when (or (null min-val) (< dist min-val))
(setf min-val dist
nearest-mean q))))
nearest-mean)))))
;; randomly assign pixel values to the k means
(loop for i below k
for y = (random height)
for x = (random width)
do (setf (pixel means i 0)
(pixel image y x)))
(loop for iter below max-iterations
with stop = nil
with oldz
until stop
do
(assign-to-means)
(recompute-means)
(when (and oldz (equalp oldz z))
(setf stop t))
(setf oldz (copy-array z)))
(values means z))))))
(t
(with-image-bounds (height width channels)
image
(let ((means (make-array (append (list k 1) (if channels (list channels)))
:element-type '(unsigned-byte 32)))
(counts (make-array k :element-type 'fixnum))
(z (make-array (list height width) :element-type '(unsigned-byte 32))))
(declare (type (simple-array (unsigned-byte 32) (* *)) z))
(flet ((recompute-means ()
;; clear out the old values
(let ((zero (make-list (or channels 1) :initial-element 0)))
(dotimes (q k)
(setf (pixel* means q 0) zero)
(setf (aref counts q) 0)))
;; use the means vector first as an accumulator to hold
;; the sums for each channel, later we'll scale by (/
;; num-pixels)
(do-pixels (i j) image
(let ((m (aref z i j)))
(setf (pixel* means m 0)
(mapcar #'+
(multiple-value-list (pixel image i j))
(pixel* means m 0)))
(incf (aref counts (aref z i j)))))
(dotimes (q k)
(when (plusp (aref counts q))
(setf (pixel* means q 0)
(mapcar (lambda (x) (round (/ x (aref counts q))))
(pixel* means q 0) )))))
(assign-to-means ()
(typecase image
(gray-image
(locally
(declare (type gray-image image))
(do-pixels (i j) image
(setf (aref z i j)
(let (min-val nearest-mean)
(loop for q below k
do (let ((dist (let ((d (- (pixel image i j)
(pixel means q 0))))
(* d d))))
(when (or (null min-val) (< dist min-val))
(setf min-val dist
nearest-mean q))))
nearest-mean)))))
(rgb-image
(locally
(declare (type rgb-image image))
(do-pixels (i j) image
(setf (aref z i j)
(let (min-val nearest-mean)
(loop for q below k
do (let ((dist (multiple-value-call #'l2-distance-3
(pixel image i j)
(pixel means q 0))))
(when (or (null min-val) (< dist min-val))
(setf min-val dist
nearest-mean q))))
nearest-mean)))))
(t (do-pixels (i j) image
(setf (aref z i j)
(let (min-val nearest-mean)
(loop for q below k
do (let ((dist (l2-distance (pixel* image i j)
(pixel* means q 0))))
(when (or (null min-val) (< dist min-val))
(setf min-val dist
nearest-mean q))))
nearest-mean)))))))
;; randomly assign pixel values to the k means
(loop for i below k
for y = (random height)
for x = (random width)
do (setf (pixel means i 0)
(pixel image y x)))
(loop for iter below max-iterations
with stop = nil
with oldz
until stop
do
(assign-to-means)
(recompute-means)
(when (and oldz (equalp oldz z))
(setf stop t))
(setf oldz (copy-array z)))
(values means z)))))))