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)
  • 1
    $\begingroup$ A sample image would help. $\endgroup$
    – C. E.
    Commented Jul 30, 2013 at 12:40
  • $\begingroup$ You might look at ColorQuantize -- this will reduce the color space to a specified number of colors. $\endgroup$
    – bill s
    Commented Jul 30, 2013 at 13:04
  • $\begingroup$ 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. $\endgroup$
    – cormullion
    Commented Jul 30, 2013 at 15:08

1 Answer 1


Are you looking for something like this:

i = ImageResize[ExampleData[{"TestImage", "Lena"}], 60];
data = ImageData[i, DataReversed -> True];
getColor[{r_, c_}] := 
  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_}] :=
  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}]}
  getShape[{row, column}],
  {row, 1, Length[data]}, 
  {column, 1, Length[data]}],
  Background -> Black,
 PlotRangePadding -> 0

lena gone geometric

I had trouble with my rows/columns/x/y... :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.