Constrain the clustering to pixels in the histogram range from 0 - 55. The modal value…
- User
Constrain the clustering to pixels in the histogram range from 0 - 55. The modal value should be based on the darkest pixels in the largest cluster. Code: import os from PIL import Image import numpy as np from sklearn.cluster import KMeans
def find_black_value(image_path, n_clusters=5): # Open the image and convert to grayscale with Image.open(image_path).convert('L') as img: # Convert image to numpy array img_array = np.array(img)
code# Reshape the image to a 2D array of pixels pixels = img_array.reshape((-1, 1)) # Perform k-means clustering kmeans = KMeans(n_clusters=n_clusters, n_init=10) kmeans.fit(pixels) # Get the cluster centers centers = kmeans.cluster_centers_.flatten() # Sort the centers sorted_centers = np.sort(centers) # Calculate the differences between adjacent centers differences = np.diff(sorted_centers) # Find the index of the largest difference largest_diff_index = np.argmax(differences) # The black value is the average of all centers below the largest difference black_value = np.mean(sorted_centers[:largest_diff_index+1]) return int(black_value)def process_directory(directory): results = {} for root, dirs, files in os.walk(directory): for file in files: if file.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')): image_path = os.path.join(root, file) try: black_value = find_black_value(image_path) results[image_path] = black_value except Exception as e: print(f"Error processing {image_path}: {str(e)}") return results
Use the specified directory
directory_path = r"D:\Bilder\Python\1. Input" black_values = process_directory(directory_path)
Print results
for image_path, black_value in black_values.items(): print(f"Image: {image_path}") print(f"Estimated black value: {black_value}") print()
Save results to a file
output_file = os.path.join(directory_path, 'black_values_results.txt') with open(output_file, 'w') as f: for image_path, black_value in black_values.items(): f.write(f"Image: {image_path}\n") f.write(f"Estimated black value: {black_value}\n") f.write("\n")
print(f"Results have been saved to: {output_file}")
- Assistant
- User
- Assistant
- User
instead of center use the most common value: # Get the center for the largest cluster black_value = centers[largest_cluster_index]
- Assistant
- User
Error processing D:\Bilder\Python\1. Input\Testset\Color Correction - Normal\01.png: invalid index to scalar variable.
- Assistant
- User
what is in the center of the largest custer? # Find the largest clusterlargest_cluster_index = max(cluster_sizes, key=cluster_sizes.get)# Get the center for the largest clusterblack_value = centers[largest_cluster_index]
- Assistant
The center of a cluster in k-means clustering refers to the centroid, which is the mean of all points assigned to that cluster. In the context of your code, after you perform k-means clustering, you have several cluster centers (or centroids), each representing the "average" value of the cluster it corresponds to.
Here's a detailed breakdown of what the code you provided does:
-
Find the Largest Cluster:
largest_cluster_index = max(cluster_sizes, key=cluster_sizes.get):- This line identifies the index of the cluster with the most data points. is a dictionary where keys are the cluster indices and values are the counts of data points in each cluster. finds the key (i.e., the cluster index) with the maximum value (i.e., the largest number of data points).
-