Cluster Spatial Pixels CIELab Homework Answers Needed
Your Question:
macbeth = cv2.imread("Images/macbeth_cc.jpg")
macbeth_original_cieLab = cv2.cvtColor(macbeth,cv2.COLOR_BGR2LAB)
# macbeth_resized = cv2.resize(macbeth,(desired_width,desired_height),interpolation=cv2.INTER_AREA)
# macbeth_cieLab = cv2.cvtColor(macbeth_resized,cv2.COLOR_BGR2LAB)
desired_width = int(desired_height * aspect_ratio)
macbeth_resized = cv2.resize(macbeth_original_cieLab,(desired_width,desired_height),interpolation=cv2.INTER_AREA)
#this function converts from opencv to normal CIELAb values
# def convertToLab(array):
# a = float(array[col][row][1])
# b = float(array[col][row][2])
# b = b - 128.0
# array[col][row][1] = a
#print(ImageCopy_2d)
#this step involves normalizing data
a_channel_normalized = (a_channel - np.min(a_channel)) / (np.max(a_channel) - np.min(a_channel))
b_channel_normalized = (b_channel - np.min(b_channel)) / (np.max(b_channel) - np.min(b_channel))
optimal_k = -1
myExit = 0 # keep track to exit from loop
silhoutte_avg = silhouette_score(normalized_cielab_array,labels)
print(f"silhouete average: {silhoutte_avg}")
else:
myExit += 1
macbeth_original_cieLab_reshaped2D = macbeth_original_cieLab_reshaped.reshape((-1,3))
macbeth_kmeans = KMeans(n_clusters=optimal_k,random_state=0).fit(macbeth_original_cieLab_reshaped2D)
The next step of the project should be to run your clustering algorithm in each of the masks, find spatial clusters and classify each pixel according to its [row, column] position in the image.
Step By Step Answers with Explanation
To achieve the goal of separating spatial clusters of pixels within each of the masks generated by your KMeans clustering algorithm, you'll need to follow a step-by-step process. I'll outline the procedure for you:
num_labels, labels = cv2.connectedComponents(cluster_mask.astype(np.uint8))
The labels array will contain integers where each unique value represents a different spatial cluster within the mask.
Here, spatial_clusters is a dictionary where keys are cluster labels, and values are arrays of [row, column] positions of pixels within that spatial cluster.
Optional: Visualize Spatial Clusters: If you want to visualize the spatial clusters, you can mark them on your original image using different colors or contours. This step is optional but can be helpful for debugging and analysis.