2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2015-12-13 09:43:58 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
PY3 = sys.version_info[0] == 3
|
|
|
|
|
|
|
|
if PY3:
|
|
|
|
xrange = range
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
from numpy import random
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
def make_gaussians(cluster_n, img_size):
|
|
|
|
points = []
|
|
|
|
ref_distrs = []
|
2017-08-25 00:45:14 +08:00
|
|
|
for _i in xrange(cluster_n):
|
2012-10-17 07:18:30 +08:00
|
|
|
mean = (0.1 + 0.8*random.rand(2)) * img_size
|
|
|
|
a = (random.rand(2, 2)-0.5)*img_size*0.1
|
|
|
|
cov = np.dot(a.T, a) + img_size*0.05*np.eye(2)
|
|
|
|
n = 100 + random.randint(900)
|
|
|
|
pts = random.multivariate_normal(mean, cov, n)
|
|
|
|
points.append( pts )
|
|
|
|
ref_distrs.append( (mean, cov) )
|
|
|
|
points = np.float32( np.vstack(points) )
|
|
|
|
return points, ref_distrs
|
|
|
|
|
|
|
|
def draw_gaussain(img, mean, cov, color):
|
2021-06-30 03:15:37 +08:00
|
|
|
x, y = mean
|
2017-12-11 17:55:03 +08:00
|
|
|
w, u, _vt = cv.SVDecomp(cov)
|
2012-10-17 07:18:30 +08:00
|
|
|
ang = np.arctan2(u[1, 0], u[0, 0])*(180/np.pi)
|
|
|
|
s1, s2 = np.sqrt(w)*3.0
|
2021-06-30 17:50:21 +08:00
|
|
|
cv.ellipse(img, (int(x), int(y)), (int(s1), int(s2)), ang, 0, 360, color, 1, cv.LINE_AA)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
def main():
|
2012-10-17 07:18:30 +08:00
|
|
|
cluster_n = 5
|
|
|
|
img_size = 512
|
|
|
|
|
2015-12-13 09:43:58 +08:00
|
|
|
print('press any key to update distributions, ESC - exit\n')
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
while True:
|
2015-12-13 09:43:58 +08:00
|
|
|
print('sampling distributions...')
|
2012-10-17 07:18:30 +08:00
|
|
|
points, ref_distrs = make_gaussians(cluster_n, img_size)
|
|
|
|
|
2015-12-13 09:43:58 +08:00
|
|
|
print('EM (opencv) ...')
|
2017-12-11 17:55:03 +08:00
|
|
|
em = cv.ml.EM_create()
|
2015-12-13 09:43:58 +08:00
|
|
|
em.setClustersNumber(cluster_n)
|
2017-12-11 17:55:03 +08:00
|
|
|
em.setCovarianceMatrixType(cv.ml.EM_COV_MAT_GENERIC)
|
2015-12-13 09:43:58 +08:00
|
|
|
em.trainEM(points)
|
|
|
|
means = em.getMeans()
|
2016-06-14 21:01:36 +08:00
|
|
|
covs = em.getCovs() # Known bug: https://github.com/opencv/opencv/pull/4232
|
2012-10-17 07:18:30 +08:00
|
|
|
found_distrs = zip(means, covs)
|
2015-12-13 09:43:58 +08:00
|
|
|
print('ready!\n')
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
img = np.zeros((img_size, img_size, 3), np.uint8)
|
|
|
|
for x, y in np.int32(points):
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.circle(img, (x, y), 1, (255, 255, 255), -1)
|
2012-10-17 07:18:30 +08:00
|
|
|
for m, cov in ref_distrs:
|
|
|
|
draw_gaussain(img, m, cov, (0, 255, 0))
|
|
|
|
for m, cov in found_distrs:
|
|
|
|
draw_gaussain(img, m, cov, (0, 0, 255))
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imshow('gaussian mixture', img)
|
|
|
|
ch = cv.waitKey(0)
|
2012-10-17 07:18:30 +08:00
|
|
|
if ch == 27:
|
|
|
|
break
|
2019-03-20 02:03:58 +08:00
|
|
|
|
|
|
|
print('Done')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(__doc__)
|
|
|
|
main()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.destroyAllWindows()
|