Created
January 18, 2026 09:16
-
-
Save thinkphp/86d17655c3400ddfaf6155cb6ca6d6a1 to your computer and use it in GitHub Desktop.
T2
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
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn.cluster import KMeans | |
| #algoritmul de clusterizare: grupeaza datele in K clustere | |
| # cum se incarca o imagine? | |
| img = plt.imread('T2.jpeg') | |
| #citeste imaginea si o stocheaza ca array NumPy (matrice de pixeli) | |
| print("="*50) | |
| print(f"Dimensiunea imaginii: {img.shape}") | |
| print("="*50) | |
| print(f"Tip de date: {img.dtype}") | |
| print(f"valoarea minima: {img.min():.4f}") | |
| print(f"valoarea maxima: {img.max():.4f}") | |
| plt.imshow(img, cmap='jet') | |
| plt.show() | |
| #verificam daca imaginea este color sau grayscale | |
| if len(img.shape) == 3: | |
| print(f"Numar de canale: {img.shape[2]}") | |
| print(f"Tip: imagine color(RGB/RGBA)") | |
| #convertim la grayscale daca este necesar | |
| if img.shape[2] == 3: #RGB | |
| img_gray = np.mean(img, axis=2) | |
| elif img.shape[2] == 4: #RGBA | |
| img_gray = np.mean(img[:,:, :3], axis=2) | |
| else: | |
| print("Tip: Imagine grayscale") | |
| img_gray = img | |
| print(f"Dimensiunea imaginii grayscale: {img_gray.shape}") | |
| # obtim matricea de caracteristici cu valori > 0 | |
| #aplatizam imaginea si filtram valorile > 0 | |
| img_flat = img_gray.flatten() | |
| mask = img_flat > 0 | |
| pixels_valide = img_flat[ mask ] | |
| print("="*50) | |
| print("Matricea de caracteristici") | |
| print("="*50) | |
| print(f"Total pixeli in imagine: {len(img_flat)}") | |
| print(f"Pixeli cu valoarea > 0: {len(pixels_valide)}") | |
| print(f"Pixeli cu valoarea = 0: {len(img_flat) - len(pixels_valide)}") | |
| print(f"Procent pixeli valizi: {len(pixels_valide)/len(img_flat)}") | |
| print("\n") | |
| #pregatirea datelor pentru KMeans (trebuie sa fie 2D) | |
| X = pixels_valide.reshape(-1,1) | |
| print(f"Forma matricei X: {X.shape}") | |
| """ | |
| ------------------------ | |
| IMAGINE RGB | |
| Canal 0 RED Canal 1 GREEN | |
| 255 128 64 200 | |
| 100 50 150 75 | |
| Canal2 | |
| ------------------------ | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment