Home > OS >  MATLAB to Julia RGB image conversion
MATLAB to Julia RGB image conversion

Time:06-02

I am working with RGB, 2D images.

In MATLAB I can do something like

imread(img, "file.png")
# access img(2, 4, 1) for the red color channel
# access img(2, 4, 2) for the green color channel

In Julia I want to do something similar...

load("file.png", img)
# access img[2, 4, 1] will not work

I know this is because of how images are encoded in Julia. But is there a way to convert these matrices in Julia to match the format used in MATLAB?

I apologize in advance for being a Julia newbie, and couldn't find any docs for this

CodePudding user response:

You can use the FileIO and Colors packages for this:

using FileIO, Colors
# load png
img = FileIO.load("file.png")
# access each channel with any of the following access patterns
green.(img)
red(img[begin, end-3])
blue.(img[1:4, 5:7])

If your PNG has transparency, you could access alpha by the function alpha similarly to the red, green and blue functions above.

  • Related