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.
colordiff()
in Colors.jl to compare the color with, say, "black"? – Castleberrycolordiff
takes two colors as arguments. To use it, you should extract the color part of the pixel withcolor
i.e.colordiff(color(v),color(w))
wherev
would be theRGBA{U8}(0.384,0.0,0.0,1.0)
. – Whin