Last active
March 30, 2019 04:58
-
-
Save abrahamhurtado/795695bf40cf8f451c6ca7696e5f1988 to your computer and use it in GitHub Desktop.
Segmentacion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function [ Features ] = Create_Features(imagen, num_rows, num_cols) | |
| total_elems = num_rows * num_cols; | |
| Features = zeros(total_elems, 5); | |
| x = 1; | |
| y = 1; | |
| for i=1:total_elems | |
| Features(i,1) = x; | |
| Features(i,2) = y; | |
| Features(i,3) = imagen(x,y,1); | |
| Features(i,4) = imagen(x,y,2); | |
| Features(i,5) = imagen(x,y,3); | |
| x = x + 1; | |
| if (mod(i, num_rows) == 0) | |
| x = 1; | |
| y = y + 1; | |
| end | |
| end | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function [ denormalized ] = Denormalize_Centroids(Centroids, Features) | |
| denormalized = zeros(size(Centroids)); | |
| for i=1:5 | |
| valor_max = max(Features(:,i)); | |
| valor_min = min(Features(:,i)); | |
| denormalized(:, i) = round((Centroids(:,i) * (valor_max - valor_min)) + valor_min); | |
| end | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function [ centroids ] = init_centroids( Features_Norm, K ) | |
| centroids = zeros(K, size(Features_Norm, 2)); | |
| index_registros_representativos = randperm(length(Features_Norm), K); | |
| for i=1:K | |
| centroids(i,:) = Features_Norm(index_registros_representativos(i), :); | |
| end | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function [ dataset, predictions, centroids, centroids2, iterations ] = K_Means(image_og, K, w) | |
| [num_rows, num_cols, dimensions] = size(image_og); | |
| num_elems = num_rows * num_cols; | |
| image = zeros(num_rows, num_cols, dimensions); | |
| image(:,:,1) = image_og(:,:,1); | |
| image(:,:,2) = image_og(:,:,2); | |
| image(:,:,3) = image_og(:,:,3); | |
| % image_new = zeros(num_rows, num_cols, dimensions); | |
| Features = Create_Features(image, num_rows, num_cols); | |
| Features_Norm = normalize_matrix(Features, w); | |
| centroids = init_centroids(Features_Norm, K); | |
| centroids2 = zeros(size(centroids)); | |
| predictions = zeros(num_elems, 1); | |
| iterations = 0; | |
| % El algoritmo se ejecutar� hasta que centroids y centroids2 sean iguales, | |
| % es decir, que converjan. | |
| while isequal(centroids, centroids2) == false | |
| iterations = iterations + 1; | |
| % Recorremos por el total de datos, en este caso 633 | |
| for i = 1:num_elems | |
| % Obtenemos la distancia euclidiana del registro i del dataset | |
| % respecto a todos los centroides. | |
| % bsxfun(@minus, centroides, dataset(i, :)) nos permite a cada | |
| % rengl�n de la matriz de centroids, restarle el registro ubicado | |
| % en dataset(i), una operaci�n que no se puede ejecutar | |
| % naturalmente y que hubiera requerido otro ciclo for. | |
| % sum(@minus, centroides, dataset(i, :))', se hace la suma de la | |
| % matriz transpuesta porque las sumas de matrices se hacen columna | |
| % por columna, como est� acomodada originalmente la matriz | |
| % centroids 8 x 18, la suma resultar�a en una matriz horizontal de | |
| % 1 x 18, nosotros queremos 1 x 8. | |
| [~, claseAsignada] = min(sum(bsxfun(@minus, centroids, Features_Norm(i,:))'.^2)); | |
| predictions(i,:) = claseAsignada; | |
| end | |
| % centroids2 ahora almacena centroids | |
| centroids2 = centroids; | |
| % Recorremos por el total de clases que tenemos, en este caso 8 | |
| for j = 1:K | |
| % Filtramos los registros de dataset seg�n las predicciones para la | |
| % clase j. | |
| registrosAsignadosEnJ = Features_Norm(predictions == j, :); | |
| % El nuevo centroids es la media de las predicciones. | |
| centroids(j,:) = mean(registrosAsignadosEnJ); | |
| end | |
| end | |
| dataset = Features; | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| imagen = imread('Tiger1.tiff'); | |
| [ dataset, predictions, centroids, centroids2, iterations ] = K_Means(imagen, 3, 1); | |
| denormalized = Denormalize_Centroids(centroids, dataset); | |
| [num_rows, num_cols, dimensions] = (size(imagen)); | |
| new_image = zeros(num_rows, num_cols, dimensions); | |
| x = 1; | |
| y = 1; | |
| for i=1:length(predictions) | |
| R = denormalized(predictions(i), 3); | |
| G = denormalized(predictions(i), 4); | |
| B = denormalized(predictions(i), 5); | |
| new_image(x,y,1) = R; | |
| new_image(x,y,2) = G; | |
| new_image(x,y,3) = B; | |
| x = x + 1; | |
| if (mod(i, num_rows) == 0) | |
| x = 1; | |
| y = y + 1; | |
| end | |
| end | |
| NI = uint8(new_image); | |
| figure(1); | |
| imshow(NI); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function [ Feature_Norm ] = normalize_matrix( Features, w ) | |
| Feature_Norm = zeros(size(Features)); | |
| for i=1:5 | |
| valor_max = max(Features(:,i)); | |
| valor_min = min(Features(:,i)); | |
| Feature_Norm(:, i) = (Features(:,i) - valor_min)/(valor_max - valor_min); | |
| end | |
| Feature_Norm(:,1) = Feature_Norm(:,1) * w; | |
| Feature_Norm(:,2) = Feature_Norm(:,2) * w; | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment