1 year ago
#334017
けいと差珍
how to cluster and then add the colors for clustering in thermal images
I want to cluster the image according so that i get the high temperature regions( white, red and yellow) and then instead of viewing those individually, I would like to view all the three color together.
I tried clustering and then add when the colors are in the bounderies of rgb color code, only to notice the color code differs for different images.
I added the specific colors manually, which happens to be exhausting and impossible. Is there any way to add them automattically
I am trying this in google colab btw
The code is;
path = '/content/drive/MyDrive/FLIR samples/img_mask/img/509.png'
img = cv2.imread(path)
# img = cv2.cvtColor(hsvImage, cv2.COLOR_BGR2RGB)
plt.figure(num=None, figsize=(8, 6), dpi=80)
imshow(img);
def image_to_pandas(image):
df = pd.DataFrame([image[:,:,0].flatten(),
image[:,:,1].flatten(),
image[:,:,2].flatten()]).T
df.columns = ['Red_Channel','Green_Channel','Blue_Channel']
return df
df_img= image_to_pandas(img)
plt.figure(num=None, figsize=(8, 6), dpi=80)
kmeans = KMeans(n_clusters= 5, random_state = 42).fit(df_img)
result = kmeans.labels_.reshape(img.shape[0],img.shape[1])
plt.figure(num=None, figsize=(8, 6), dpi=80)
imshow(result, cmap='twilight');
plt.figure(num=None, figsize=(8, 6), dpi=80)
kmeans = KMeans(n_clusters = 5, random_state = 42).fit(img)
result = kmeans.labels_.reshape(img.shape[0],img.shape[1])
plt.figure(num=None, figsize=(8, 6), dpi=80)
imshow(result, cmap='magma');
def masker(image,masks):
fig, axes = plt.subplots(2, 5, figsize=(100, 20))
image_copy = image.copy()
for n, ax in enumerate(axes.flatten()):
masked_image = np.dstack((image_copy[:, :, 0]*(masks==[n]),
image_copy[:, :, 1]*(masks==[n]),
image_copy[:, :, 2]*(masks==[n])))
ax.imshow(masked_image)
ax.set_title(f'Cluster : {n}', fontsize = 10)
ax.set_axis_off();
fig.tight_layout()
masker(img,result)
python
image-processing
colors
cluster-computing
imaging
0 Answers
Your Answer