Пример #1
0
def generate_semi_markov_non_stat3(length,
                                   mu,
                                   sigma,
                                   lawu=np.array([[[0.2, 0.2, 0.2, 0.2, 0.2],
                                                   [0.2, 0.2, 0.2, 0.2, 0.2]],
                                                  [[0.2, 0.2, 0.2, 0.2, 0.2],
                                                   [0.2, 0.2, 0.2, 0.2,
                                                    0.2]]])):
    nbc_x = 2
    nbc_u = lawu.shape[-1]
    nb_class = nbc_x * nbc_u
    hidden = np.zeros(length, dtype=int)
    visible = np.zeros((length, mu.shape[-1]))
    mu = np.repeat(mu, nbc_u, axis=0)
    sigma = np.repeat(sigma, nbc_u, axis=0)
    p0 = np.random.uniform(0, 1, (nbc_x * nbc_u, ))
    p0 = p0 / p0.sum()
    test = np.random.multinomial(1, p0)
    hidden[0] = np.argmax(test)
    visible[0] = multivariate_normal.rvs(mu[hidden[0]], sigma[hidden[0]])
    for l in range(1, length):
        deltai = np.random.uniform(0, 1)
        a = np.array([[deltai, 1 - deltai], [1 - deltai, deltai]])
        b = np.repeat(np.eye(nbc_x, nbc_x, k=0), nbc_u, axis=0)
        idx = [i for i in range(b.shape[0]) if ((i + 1) % nbc_u == 0)]
        for i, e in enumerate(idx):
            b[e] = a[i]

        a = lawu
        ut = [[np.eye(nbc_u, k=1) for n1 in range(nbc_x)]
              for n2 in range(int(nb_class / nbc_u))]
        for i, e in enumerate(ut):
            for j, p in enumerate(e):
                p[-1] = a[i, j]
        ut = np.block(ut)
        for i in range(1, nbc_x + 1):
            ut[:, (i - 1) * nbc_u:i *
               nbc_u] = (ut[:, (i - 1) * nbc_u:i * nbc_u].T * b[:, i - 1]).T
        T = ut
        test = np.random.multinomial(1, T[hidden[l - 1], :])
        hidden[l] = np.argmax(test)
        visible[l] = multivariate_normal.rvs(mu[hidden[l]], sigma[hidden[l]])
    return convert_multcls_vectors(hidden, (nbc_u, nbc_x))[:, 1], visible
Пример #2
0
) * 3  #Cela va servir a associer les classes avec des couleurs différentes si on a plus de deux classes (cela fonctionne jusqu'a 8 classes)

#Prétraiement de l'image (on ne travaille qu'avec des dimensions carrée en puissance de 2 pour l'instant (ex:(64,64), (256,256))
img_noisy = cv.resize(img, (256, 256))
max_val = np.max(img_noisy)
img_noisy = img_noisy / max_val
im_res2 = cv.resize(img_noisy, (64, 64))

test = get_peano_index(img_noisy.shape[0])  #Parcours de peano
#test = [a.flatten() for a in np.indices((256, 256))] #Parcours ligne par ligne
data = img_noisy[test[0], test[1]].reshape(-1, dimY)

#On segmente par kmeans pour initialiser les modèles suivants (on garde la segmentation kmeans pour la comparer avec les autres)
kmeans = KMeans(n_clusters=4, random_state=0).fit(data)
seg_kmeans = np.zeros((img_noisy.shape[0], img_noisy.shape[1], 3))
seg_kmeans[test[0], test[1]] = convert_multcls_vectors(kmeans.labels_,
                                                       shape_seg_vect)
cv.imwrite('./img/res/testroute1_seg_kmeans_color.jpg', seg_kmeans * max_val)

# #Creation de la chaine de markov caché
# hmc = HMC_ctod()
# hmc.init_kmeans(data, kmeans.labels_) #initialisation grace au kmeans
# hmc.get_param_ICE(data, 10, 10) #estimation des paramètres avec ICE, (on peut utiliser SEM ou EM avec get_param_EM ou get_param_SEM)
# seg_hmc = np.zeros((img_noisy.shape[0], img_noisy.shape[1], 3)) #Création d'une matrice vide qui va recevoir l'image segmentée
# seg_hmc[test[0], test[1]] = convert_multcls_vectors(hmc.seg_mpm(data),shape_seg_vect) #Remplir notre matrice avec les valeurs de la segmentation
# cv.imwrite('./img/res/testroute1_seg_hmc_color.jpg', seg_hmc*max_val) #Sauvegarder l'image

#Creation de la chaine de markov caché multiresolution

mhmc = HMC_multiR_ctod(
    resoffset=(4, ), resnbc=(2, 2)
)  #resoffset correspond au décallage entre les deux résolution et resnbc correspond au nombre de classe par résoltuion