6

I am using Images.jl in Julia. I am trying to convert an image into a graph-like data structure (v,w,c) where

  • v is a node

  • w is a neighbor and

  • c is a cost function

I want to give an expensive cost to those neighbors which have not the same color. However, when I load an image each pixel has the following Type RGBA{U8}(1.0,1.0,1.0,1.0), is there any way to convert this into a number like Int64 or Float?

Priya
  • 1,359
  • 6
  • 21
  • 41

3 Answers3

4

If all you want to do is penalize adjacent pairs that have different color values (no matter how small the difference), I think img[i,j] != img[i+1,j] should be sufficient, and infinitely more performant than calling colordiff.

Images.jl also contains methods, raw and separate, that allow you to "convert" that image into a higher-dimensional array of UInt8. However, for your apparent application this will likely be more of a pain, because you'll have to choose between using a syntax like A[:, i, j] != A[:, i+1, j] (which will allocate memory and have much worse performance) or write out loops and check each color channel manually. Then there's always the slight annoyance of having to special case your code for grayscale and color, wondering what a 3d array really means (is it 3d grayscale or 2d with a color channel?), and wondering whether the color channel is stored as the first or last dimension.

None of these annoyances arise if you just work with the data directly in RGBA format. For a little more background, they are examples of Julia's "immutable" objects, which have at least two advantages. First, they allow you to clearly specify the "meaning" of a certain collection of numbers (in this case, that these 4 numbers represent a color, in a particular colorspace, rather than, say, pressure readings from a sensor)---that means you can write code that isn't forced to make assumptions that it can't enforce. Second, once you learn how to use them, they make your code much prettier all while providing fantastic performance.

The color types are documented here.

tholy
  • 11,882
  • 1
  • 29
  • 42
  • Thank you for answering. I am trying to build the graph in such a way when I am running the shortest path algorithm I can get the contour of the figure. I am thinking on penalize the difference but it seems I will need to take in account the gradient too. Is there in Images.jl a way to calculate this? – Ivan Felipe Rodríguez Apr 29 '16 at 20:09
  • There's a handy function reference at http://timholy.github.io/Images.jl/stable/function_reference/. Or see the help on `imgradients`. – tholy Apr 29 '16 at 21:07
4

Might I recommend converting each pixel to greyscale if all you want is a magnitude difference.

See this answer for a how-to:

Converting RGB to grayscale/intensity

This will give you a single value for intensity that you can then use to compare.

Community
  • 1
  • 1
It'sPete
  • 5,083
  • 8
  • 39
  • 72
3

Following @daycaster's suggestion, colordiff from Colors.jl can be used.

colordiff takes two colors as arguments. To use it, you should extract the color part of the pixel with color i.e. colordiff(color(v),color(w)) where v would be RGBA{U8(0.384,0.0,0.0,1.0) value.

Dan Getz
  • 17,002
  • 2
  • 23
  • 41