Image compression - Divide matrix by 255

Hello,
What’s the idea behind this division (original_img / 255)? Is it mandatory?
Thanks

Hello @tbhuy,

The core idea is to make the pixel values to be between 0 and 1. If it’s already between 0 and 1 then it’s not necessary. If it’s in the range between 0 and 255, then we do the division to serve the purpose aforementioned.

Some image display functions require the pixel value to be between 0 and 1.

Raymond

Hello @rmwkwok but we can let them as they are, without division, and directly pass them to KMeans. Is it right? I wonder if there are a boost in performance when working with float instead of integer, thus the purpose of the division…
For info, in the lab we use [0,255] values for pixel for displaying:
ax[0].imshow(original_img*255)

For this particular problem which uses the R, G, B pixel values as sample features, i think it’s okay for us to either not do the division or do the division, so it remains your choice. However, as a rule of thumb, it’s always better to normalize the features when we use K-means because K-means is distance-based which means it is sensitive to the difference of your features’ scales. Normalizing your feature helps make the feature to scale similarly. if you load a photo in a way that the pixel values are represented by a range between 0 and 255, then dividing the pixel values by 255 is actually doing the Min-Max normalization. You may normalize it this way or using any other way you have learnt in this specialization.

For this, please verify the range yourself by printing the maximum value of original_img*255. Here is my previous reply about this.

Raymond

Anyway, the method “imread” of plt - in case of PNG images - already returns float arrays (0-1).
So dividing by 255, in this specific case, seems to be not necessary.
Is it correct?

Hi @Alessandro_Simonetti,

Correct!