If we convert an image into a series of pixel values with Matlab, how can we "count how many colors there are in the image" based on these pixel values? (Similar colors are considered as the same one.)
I would suggest you read the header of the file first , it gives the format of image data, say RGB , YCbCr , Lab so on . It also has the bpp (bits per pixel representation) say 16 bit of RGB may be (5 - Red , 6 -Green and 5-blue) or RGB(8 bit of each ) . All files have a format of representation so you need to read the metadata (worst case see properties of the file )
Depending on how you want to process , you can make it unsigned integer of 8 bits or combine as one pixel by say x2 * Red + x * Green + Blue
For color space : https://en.wikipedia.org/wiki/CIE_1931_color_space
You could convert your image to the Lab-Colorspace (or similar as L*a*b*) that is constructed so that the euclidean distance between colors corresponds to the perceived difference of the colors. If the distance is below a threshold, they are seen as the same color and could be counted as one color in your case. A clustering of your pixels in groups where the pairwise distance is below this threshold could give you a list of the different colors and their number. This clustering however is most probably not unique but the question is also not exactly defined.