So you are after image sorting metrics. That is mostly what 'Comparing Images' in IM examples is about.
Note however that predonimate color is not generally regarded as a good metric, Basic classification would be better. (sketch vs cartoon vs photo). Then more specific.
But for what you describe with a 3x3 color matrix, just 'scale' the image to 3x3\! The exclaimation tells IM to disregard aspect ratio during the process.
Just remeber that colors are themselves a 3 dimensional value, and then you need to decide if you compare basied on RGB color space, or YIQ, or CMYK. And finally, what about images containing 'spam text' in one corner, has extra 'framing', was 'cropped' or 'divided into two', or color changed to highligh some detail. Any of these things can throw of your 'color index'. Don't think it can't happen, I've seen all of these, in a archive of web images I tried to sort out and match up.
As for the API (application interface) you use. What I do on the comamnd line should in principle be able to be done in that API.
As for extractingthe colors, save to a image txt: format as I did in the examples above. In that case I save the 'txt' image to the terminal using '-' as the file name, but color.txt can also be used too. See
http://www.imagemagick.org/Usage/formats/#txt for more on that.
In any case, please let the forum, or just me, know what happens and what you decide, and your results. Feedback on this type of thing is rare, and most welcome.
Question, do you have an 18 dimensional (3x3 colors of 3 values each) sorting, clustering or segmentation algorithm?