コード例 #1
0
img = cv.imread('./img/testroute1.jpg')  #Charger l'image
#img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #Si cette ligne est décommentée on travaille en niveau de gris

dimY = 3  #Dimension des observations (couleur = 3, niveau de gris = 1)
shape_seg_vect = (
    2,
) * 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
コード例 #2
0
import numpy as np
import os
import cv2 as cv
import json
from utils import get_peano_index, convert_multcls_vectors, moving_average, calc_err, sigmoid_np
from hmm import HMC_ctod, HSMC_ctod, HEMC_ctod, HESMC_ctod, HSEMC_ctod
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from scipy.stats import multivariate_normal

resolution = (64, 64)
signal_length = np.prod(resolution)
test = get_peano_index(resolution[0])  # Parcours de peano
# test = [a.flatten() for a in np.indices(resolution)] #Parcours ligne par ligne
max_val = 255


def generate_signal21(length, mu, sigma):
    hidden = np.zeros(length, dtype=int)
    visible = np.zeros((length, mu.shape[-1]))
    delta0 = 1 / length
    p0 = np.array([delta0, 1 - delta0])
    test = np.random.multinomial(1, p0)
    hidden[0] = np.argmax(test)
    visible[0] = multivariate_normal.rvs(mu[hidden[0]], sigma[hidden[0]])
    for i in range(1, length):
        deltai = (i + 1) / length
        T = np.array([[deltai, 1 - deltai], [1 - deltai, deltai]])
        test = np.random.multinomial(1, T[hidden[i - 1], :])
        hidden[i] = np.argmax(test)
        visible[i] = multivariate_normal.rvs(mu[hidden[i]], sigma[hidden[i]])
コード例 #3
0
for resolution in resolutions:
    for imgf in imgfs:
        if not os.path.exists(resfolder + '/' + imgf):
            os.makedirs(resfolder + '/' + imgf)

        img = cv.imread('./img/' + imgf + '.bmp')  # Charger l'image
        img = cv.cvtColor(
            img, cv.COLOR_BGR2GRAY
        )  # Si cette ligne est décommentée on travaille en niveau de gris
        img = cv.resize(img, resolution)
        img = heaviside_np(img)
        cv.imwrite(
            resfolder + '/' + imgf + '/' + str(resolution[0]) + '_' +
            str(resolution[1]) + '.bmp', img * max_val)
        test = get_peano_index(img.shape[0])  # Parcours de peano
        # test = [a.flatten() for a in np.indices((256, 256))] #Parcours ligne par ligne
        hidden = img[test[0], test[1]]
        for idx, noise in enumerate(gauss_noise):
            if not noise['corr']:
                img_noisy = (img == 0) * np.random.normal(
                    noise['mu1'], noise['sig1'],
                    img.shape) + (img == 1) * np.random.normal(
                        noise['mu2'], noise['sig2'], img.shape)
                corr = ''
                corr_param = ''
            else:
                img_noisy = moving_average(
                    (img == 0) *
                    np.random.normal(noise['mu1'], noise['sig1'], img.shape),
                    noise['corr_param'][0],
コード例 #4
0
import numpy as np
import cv2 as cv
import os
import json
from hmm import HMC_ctod, HSMC_ctod, HEMC_ctod, HESMC_ctod, HSEMC_ctod
from pmm import PMC_ctod, PSMC_ctod
from tmm import TMC_ctod
from utils import standardize_np, get_peano_index, resize_gray_im_to_square, convert_multcls_vectors, moving_average, \
    heaviside_np, sigmoid_np

resfolder = './img/res_test2/models_comparison'
ref_img = './img/img_reelles/img420.bmp'
terr = {}
resolution = (64, 64)
sample_length = np.prod(resolution)
test = get_peano_index(resolution[0])  # Parcours de peano
# test = [a.flatten() for a in np.indices(resolution)] #Parcours ligne par ligne
max_val = 255
epsilon = 0.20
alpha = 0.04
c = np.array([[0.40, 0.05], [0.05, 0.50]])
u = np.array([[c[0, 0] - epsilon, c[0, 1] - alpha, (alpha / 2)],
              [c[1, 0] - alpha, c[1, 1] - epsilon, (epsilon / 2)],
              [(alpha / 2), (epsilon / 2), alpha + epsilon]])
u1 = np.array([[[0.6, 0.2, 0.1, 0.05, 0.05], [0.6, 0.2, 0.1, 0.05, 0.05]],
               [[0.6, 0.2, 0.1, 0.05, 0.05], [0.6, 0.2, 0.1, 0.05, 0.05]]])
u2 = np.array([[[0.6, 0.2, 0.1, 0.05, 0.05], [0.6, 0.2, 0.1, 0.05, 0.05],
                [0.6, 0.2, 0.1, 0.05, 0.05]],
               [[0.6, 0.2, 0.1, 0.05, 0.05], [0.6, 0.2, 0.1, 0.05, 0.05],
                [0.6, 0.2, 0.1, 0.05, 0.05]],
               [[0.6, 0.2, 0.1, 0.05, 0.05], [0.6, 0.2, 0.1, 0.05, 0.05],