Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
2
Indexable
Never
import numpy as np
from skimage import io, measure
import matplotlib.pyplot as plt
import numpy as np
from skimage import io, measure
import matplotlib.pyplot as plt
from sklearn import cluster, datasets
from skimage import io,util,color


img = io.imread('trei.bmp')
plt.figure(), plt.imshow(img, cmap='gray'), plt.colorbar()

L=256
dims=np.shape(img)
H=dims[0]
W=dims[1]

Y=img[0:H,0:W]

h=np.zeros(L)
for l in range (0,H):
  for c in range (0,W):
    h[Y[l,c]]+=1

h=h/np.sum(h)

#transformare in binar imaginea
#img = color.rgb2gray(img)
#img = (img*255).astype(np.uint8)


#T = 120  #pt obiectele deschise
T=80   # pt obiecte inchise
epsilon = 0
h1_sum = 0
h2_sum = 0
while (epsilon > 1):    #< obiecte deschide   #>ob inchise
    
    T_old = T
    for i in range (T):
        h1_sum += i * h[i]
    
    for i in range (T, len(h)):
        h2_sum += i * h[i]
    
    
    m0 = h1_sum / (0.00000001 + np.sum(h[:T]))
    m1 = h2_sum / (0.00000001 + np.sum(h[T:]))
    
    T = 0.5 * (m0 + m1)
    
    epsilon = abs(T - T_old)
   
T = np.int64(T)
print(T)

segm = img < T

plt.figure(), plt.imshow(segm, cmap = 'gray')