-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
26 lines (23 loc) · 731 Bytes
/
algorithms.py
File metadata and controls
26 lines (23 loc) · 731 Bytes
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
from skimage import io
from tkinter import *
import numpy as np
from sklearn.cluster import KMeans, MiniBatchKMeans
import os
from PIL import Image
def segmentation(imageDir, k, saveDir):
img = Image.open(imageDir)
if img.mode != 'RGB':
rgb = img.convert('RGB')
rgb.save(imageDir)
img = io.imread(imageDir)
dim = {
'height': img.shape[0],
'width': img.shape[1],
'channels': img.shape[2],
'pixels': img.shape[0]*img.shape[1],
}
dataMat = (img/255.0).reshape(dim['pixels'],3)
kmeans = MiniBatchKMeans(k).fit(dataMat)
kColors = kmeans.cluster_centers_[kmeans.predict(dataMat)]
k_img = np.reshape(kColors, (img.shape))
io.imsave(saveDir,k_img)