# How Can I Transform a Colored Image into Pre-defined Matrix?

I am new to Mathematica and I would like to use it to postprocess some of the experimental data I measure. I would like to visualize voltage measurements over a non-uniformly segmented plate. I would like to read in the segmentation in terms of a colored scheme. The color scheme would define the segmentation in geometrical shapes. For instance, I would have a red rectangle, an orange triangle, a blue triangle and a green square.

From what I know, I can represent the colors in terms of a Matrix using the ImageData[] command. However, with this, I obtain a 3X1 matrix for each color. I would like to know if there is a way to represent each color by one number. I ask this since I would like to take the Matrix I obtain and correlate it to the matrix which identifies the segment numbers according to the segmentation as follows:

Red --> Segment 1 (Rectangle)

Orange --> Segment 2 (Triangle)

Blue --> Segment 3 (Triangle)

Green --> Segment 4 (Square)

• A sample image would help. – C. E. Jul 30 '13 at 12:40
• You might look at ColorQuantize -- this will reduce the color space to a specified number of colors. – bill s Jul 30 '13 at 13:04
• Although I tried to answer this, I now realize that I don't understand the question... Could you clarify: what data you have; how you want to transform it; and what you want the end result to consist of? Thanks. – cormullion Jul 30 '13 at 15:08

Are you looking for something like this:

i = ImageResize[ExampleData[{"TestImage", "Lena"}], 60];
data = ImageData[i, DataReversed -> True];
getColor[{r_, c_}] :=
Which[
Mean[data[[r, c]]] < 0.1, Blue,
Mean[data[[r, c]]] < 0.3, Green,
Mean[data[[r, c]]] < 0.5, Red,
Mean[data[[r, c]]] < 0.7, Orange,
True, Pink]
getShape[{r_, c_}] :=
Switch[
getColor[{r, c}],
Red, {Red, Rectangle[{c, r}, {c + .75, r + .75}]},
Green, {Green, Rectangle[{c, r}, {c + .75, r + .75}]},
Blue, {Blue, Polygon[{{c, r}, {c + .5, r + 1}, {c + 1, r}}]},
Orange, {Orange, Polygon[{{c, r}, {c + .5, r + 1}, {c + 1, r}}]},
Pink, {Pink, Rectangle[{c, r}, {c + .75, r + .75}]}
]
Graphics[
Table[
getShape[{row, column}],
{row, 1, Length[data]},
{column, 1, Length[data]}],
Background -> Black,