Convert RGBA{U8}(0.384,0.0,0.0,1.0) to Integer
Asked Answered
P

3

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?

Pectase answered 28/4, 2016 at 4:50 Comment(4)
How about using colordiff() in Colors.jl to compare the color with, say, "black"?Castleberry
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 the RGBA{U8}(0.384,0.0,0.0,1.0).Whin
It worked perfectly fine. Thank you.Hoke
@DanGetz Could you post that as an answer, instead of as a comment?Shaveling
W
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.

Whin answered 28/4, 2016 at 21:49 Comment(0)
A
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.

Aorta answered 29/4, 2016 at 1:10 Comment(2)
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?Hoke
There's a handy function reference at timholy.github.io/Images.jl/stable/function_reference. Or see the help on imgradients.Aorta
C
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.

Chrissa answered 29/4, 2016 at 1:23 Comment(1)
In julia, you can just say convert(Gray, rgb).Aorta
W
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.

Whin answered 28/4, 2016 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.