# GraphImage2List for multi-colored graph-image-data

**URL:** <https://community.wolfram.com/t/graphimage2list-for-multi-colored-graph-image-data/10572>\
**Category:** Staff Picks\
**Tags:** image-processing, graphics-and-visualization, interface-construction, machine-learning, staff-pick\
**Created:** [May 22, 2017, 8:20pm UTC](https://community.wolfram.com/t/graphimage2list-for-multi-colored-graph-image-data/10572 "2017-05-22T20:20:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![okazakikotaro](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/okazakikotaro/32/14323_2.png) [@okazakikotaro](https://community.wolfram.com/u/okazakikotaro)\
**Post date:** [May 22, 2017, 8:20pm UTC](https://community.wolfram.com/t/graphimage2list-for-multi-colored-graph-image-data/10572/1 "2017-05-22T20:20:01Z")

</div>

In my previous post [GraphImage2List](https://wolfram.discourse.group/t/10462/1) , I created the tool that changes graph-image-data to numeric-data in order to do a Machine Learning using graph-image-data. However, it can’t deal with multi-colored graph-image-data. So I create a new tool by **ImageGraphics** function. ImageGraphics is one of my favorite new functions in [Wolfram Language](http://reference.wolfram.com/language/ref/ImageGraphics.html) 11.1.

## **Goal**

The left is the original graph(image data) like CPU Utilization of five virtual machines.  
The right is the graph(ListPlot) of yellow-green line picked up by the new tool.

 ![screenshot.0.jpg](https://global.discourse-cdn.com/wolfram/original/3X/1/f/1f06fea9f6ab93084d39588565e8fdd1e5514b49.jpeg)

The tool comes in three steps.

## **Step 1**

In the first step, My MakeMasking function masks unnecessary areas around the original image data like plot label, frame and ticks. It outputs “maskedimage”.

```
MakeMasking[img_] := Module[{},
  size = {sizex, sizey} = ImageDimensions[img];
  backimg = Image[Table[1, {sizey}, {sizex}]];
  Manipulate[
   Grid[{{"input image", "masked image"},
     {Show[img, ImageSize -> size],
      Show[
       maskedimg = 
        ImageCompose[backimg, 
         ImageTrim[
          img, {{left, bottom}, {right, top}}], {left + right, 
           bottom + top}/2], ImageSize -> size]}}],
   Row[{Control[{left, 0, sizex, 1}], 
     Control[{{right, sizex}, 0, sizex, 1}]}, " "],
   Row[{Control[{bottom, 0, sizey, 1}], 
     Control[{{top, sizey}, 0, sizey, 1}]}, " "]
   ]
  ]

```

The left is the original. The right is masked image.

 ![9670screenshot.2.jpg](https://global.discourse-cdn.com/wolfram/original/2X/4/46507c36f1ec5cebd6f6d2261c0445d10e48bea4.jpeg)

## **Step 2**

In this step, My SelectColors function selects the areas of two near colors in ImageGraphics output of “maskedimage”.  
ImageGraphics function returns the content of image. The colors are below.

```
maskedimggraphics = ImageGraphics[maskedimg, PlotRangePadding -> None]; 
maskedimggraphics[[1, 2, #, 1]] & /@ Range[Length[maskedimggraphics[[1, 2]]]]

```

![1885screenshot.8.jpg](https://global.discourse-cdn.com/wolfram/optimized/2X/8/8a0ae2f0d5fad858df3d858b6388d02a261078a0_2_932x25.jpeg)

My SelectColors function uses the content and outputs the selected area as “selectpts”.

```
SelectColors[img_, maskedimggraphics_] := Module[{},
  {sizex, sizey} = size = ImageDimensions[img] // N;
  frame = {{0., 0.}, {0., sizey}, {sizex, sizey}, {sizex, 0.}};
  l = Length[maskedimggraphics[[1, 1]]];
  colors = 
   maskedimggraphics[[1, 2, #, 1]] & /@ 
    Range[Length[maskedimggraphics[[1, 2]]]];
  Manipulate[
   Grid[{{"input image", "selected image"},
     {Show[img, ImageSize -> ImageDimensions[img]], 
      Graphics[
       GraphicsComplex[Join[maskedimggraphics[[1, 1]], frame], 
        Join[{LABColor[
          0.9988949153893414, -3.6790844387894895`*^-6, 
           0.00042430735605277474`], 
          FilledCurve[{{Line[{l + 1, l + 2, l + 3, l + 4}]}}]}, 
         selectpts = 
          FirstCase[maskedimggraphics[[1, 2]], {#, ___}, 
             Nothing] & /@ {color1, color2}]], 
       ImageSize -> size]}}], {{color1, colors[[2]]}, colors, 
    ControlType -> RadioButton, 
    Method -> "Queued"}, {{color2, colors[[2]]}, colors, 
    ControlType -> RadioButton, Method -> "Queued"},
   SynchronousUpdating -> False]
  ]

```

The left is the original. The right is selected image.

 ![10670screenshot.3.jpg](https://global.discourse-cdn.com/wolfram/original/3X/4/7/4709a8da5c3b62c1c6aad65acb29ad5b822e02de.jpeg)

## **Step 3**

You can make list in the next process by using my GetList2 function.

1. Add/del points with alt+click(WINDOWS)/cmd+click(MAC) if necessary

2. Set x and y values(Min, Max, Accuracy) of red points

3. Click Calculate button

 ![1651screenshot.4.jpg](https://global.discourse-cdn.com/wolfram/original/2X/e/e0a7437a43e09431b544f0601fc01a41d98af9c1.jpeg)

Set x and y values(Min, Max, Accuracy) of red points and click.

 ![9510screenshot.5.jpg](https://global.discourse-cdn.com/wolfram/original/3X/f/7/f71bdd5df65052f671430c4326ea55d086b6b3ef.jpeg)

Then it outputs “list”.

```
list

```

![6445screenshot.12.jpg](https://global.discourse-cdn.com/wolfram/optimized/2X/7/7ba15a1c94a05c98fafd202b6ad4b3e1a1afe8ab_2_932x48.jpeg)

ListPlot of the list is below.

```
ListPlot[Style[list, RGBColor[204/255, 204/255, 0]], Joined -> True]

```

 ![2322screenshot.7.jpg](https://global.discourse-cdn.com/wolfram/original/3X/f/c/fc6768129287adc8339493f06ce0904e6b7249f5.jpeg)

## **Differences**

When GetList2 converts area selected in Step 2. to coordinates, there are some points of the same x coordinate. So my locator2coordinate2 function outputs 4 lists of y coordinate, high, low, middle and combination as nearyH, nearyL, nearyM and nearyC. As I show below, nearyC seems to be better than others.

I create this image data below.

```
data = {{6, 5, 33, 36, 9, 11, 23, 29, 34, 26, 3, 6, 26, 35, 21, 6, 26, 33, 20, 16, 30, 6, 1, 6},
   {41, 34, 43, 60, 33, 38, 54, 43, 29, 59, 45, 34, 42, 55, 42, 26, 59, 20, 20, 41, 41, 47, 28, 52}, 
   {58, 55, 61, 56, 40, 47, 50, 72, 72, 66, 69, 69, 78, 75, 70, 66, 56, 76, 66, 43, 47, 79, 56, 49}, 
   {88, 96, 84, 62, 69, 67, 61, 60, 94, 76, 75, 70, 69, 86, 68, 61, 72, 91, 89, 71, 69, 83, 88, 75}, 
   {17, 9, 23, 19, 23, 47, 45, 30, 82, 88, 58, 24, 59, 61, 17, 82, 95, 83, 40, 81, 68, 5, 40, 7}}; 
graph = 
   DateListPlot[data, {2017, 5, 20, 0}, Frame -> True, 
   FrameLabel -> {"Time", "CPU Utilization %"}, PlotStyle -> 96];
img = Rasterize[graph]

```

Mean and Variance of the difference between the true list(data) and calculated 4 list(nearyH, nearyL, nearyM and nearyC) are below and nearyC is the best of all.

```
Grid[
 Join[{{"", "nearyH", "nearyM", "nearyL", "nearyc"}}, 
   Join[{{"Mean", "Variance"}},
         {Mean[#], Variance[#]} & /@ {nearyH - #, nearyM - #, nearyL - #,
          nearyC - #} &@data[[5]] // N] // Transpose] // Transpose,
 Frame -> All]

```

 ![1844screenshot.9.jpg](https://global.discourse-cdn.com/wolfram/original/2X/4/460221f5d5ab6c0f4cf5c01f1181fc63f433e380.jpeg)

```
ListPlot[{Legended[Style[data[[5]], RGBColor[204/255, 204/255, 0]], 
   "data[[5]]"], Legended[Style[nearyC, Red], "nearyC"]}, 
   Joined -> {True, False}, PlotRange -> {0, 100}]

```

 ![6267screenshot.6.jpg](https://global.discourse-cdn.com/wolfram/original/1X/b82e3ba78f3b1fed156059044d73818c07aaf9fe.jpeg)

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [September 22, 2017, 2:06pm UTC](https://community.wolfram.com/t/graphimage2list-for-multi-colored-graph-image-data/10572/2 "2017-09-22T14:06:54Z")

</div>

![2015-11-02_10-07-11.png](https://global.discourse-cdn.com/wolfram/original/3X/5/0/50e59c9ead62f729e7130d3ea1482555a75602b6.png)

- Congratulations! This post is now a [Staff Pick](http://community.wolfram.com/content?curTag=staff+picks) as distinguished by a badge on [your profile](http://community.wolfram.com/web/okazakikotaro)! Thank you, keep it coming!
