-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
55 lines (49 loc) · 1.23 KB
/
demo.py
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
import skimage.filters
from skimage.io import imread, imshow, imsave
from skimage.color import rgb2gray
from skimage import feature
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import time
from sklearn.cluster import KMeans
import image_kmeans
import HMRF_EM
from sklearn.cluster import Birch
from numpy import unique
from numpy import where
# load image:
I = cv.imread('1.png')
Y = rgb2gray(I)
# Edge Image:
Z = feature.canny(Y, sigma=3)
#imsave('edge.png', np.uint8(Z))
#imsave('edge2.png', np.uint8(Z2*255))
imshow(Z, cmap='gray')
plt.show()
#imshow(Z2, cmap='gray')
#plt.show()
# Blur Image:
Y = Y*255
Y = cv.GaussianBlur(Y, (3, 3), 0)
#imsave('blurredImage.png', Y)
imshow(Y, cmap='gray')
plt.show()
# Set parameters:
k = 3
EM_iter = 10
MAP_iter = 10
start_time = time.clock()
print('start K-Means Segmentation...')
X, mu, sigma = image_kmeans.image_kmeans(Y, k)
#imsave('segmentedIamge.png', X)
#print(mu)
#print(sigma)
#plt.imshow(X)
#plt.show()
[X, mu, sigma] = HMRF_EM.HMRF_EM(X, Y, Z, mu, sigma, k, EM_iter, MAP_iter)
imsave('finla_labeled_Image.png', np.uint8(X*120))
#plt.imshow(X)
#plt.show()
end_time = time.clock()
print('Duration= {}'.format(end_time-start_time))