Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presumed interference between FileIO and Images or FixedPointNumbers #113

Open
joa-quim opened this issue Mar 24, 2017 · 11 comments
Open

Presumed interference between FileIO and Images or FixedPointNumbers #113

joa-quim opened this issue Mar 24, 2017 · 11 comments

Comments

@joa-quim
Copy link

First, let me apologize for this kinda difficult to reproduce issue report but it depends on a interaction with a C library that I just fixed for this to work. The background is that explained in this thread

What happens is the following. Somehow the order by which I load an image is important.
This works well:

using FixedPointNumbers, Images, FileIO, GMT

julia> img1=load("lixa.JPG")
4×3 Array{RGB{N0f8},2}:
 RGB{N0f8}(0.282,0.263,0.239)  RGB{N0f8}(0.514,0.486,0.373)  RGB{N0f8}(0.89,0.843,0.694)
 RGB{N0f8}(0.216,0.176,0.137)  RGB{N0f8}(0.871,0.835,0.714)  RGB{N0f8}(0.278,0.227,0.09)
 RGB{N0f8}(0.333,0.267,0.204)  RGB{N0f8}(0.969,0.91,0.78)    RGB{N0f8}(0.298,0.239,0.11)
 RGB{N0f8}(0.596,0.502,0.408)  RGB{N0f8}(0.671,0.6,0.459)    RGB{N0f8}(0.239,0.18,0.067)

julia> I = gmt("read lixa.JPG -Ti"); imf8 = reinterpret(N0f8, I.image);

julia> img2 = colorview(RGB, imf8)
4×3 Array{RGB{N0f8},2}:
 RGB{N0f8}(0.282,0.263,0.239)  RGB{N0f8}(0.514,0.486,0.373)  RGB{N0f8}(0.89,0.843,0.694)
 RGB{N0f8}(0.216,0.176,0.137)  RGB{N0f8}(0.871,0.835,0.714)  RGB{N0f8}(0.278,0.227,0.09)
 RGB{N0f8}(0.333,0.267,0.204)  RGB{N0f8}(0.969,0.91,0.78)    RGB{N0f8}(0.298,0.239,0.11)
 RGB{N0f8}(0.596,0.502,0.408)  RGB{N0f8}(0.671,0.6,0.459)    RGB{N0f8}(0.239,0.18,0.067)

But if I just revert the order of the operations

using FixedPointNumbers, Images, FileIO, GMT

julia> I = gmt("read lixa.JPG -Ti"); imf8 = reinterpret(N0f8, I.image);

julia> img2 = colorview(RGB, imf8)
4×3 Array{RGB{N0f8},2}:
 RGB{N0f8}(0.282,0.263,0.239)  RGB{N0f8}(0.514,0.486,0.373)  RGB{N0f8}(0.89,0.843,0.694)
 RGB{N0f8}(0.216,0.176,0.137)  RGB{N0f8}(0.871,0.835,0.714)  RGB{N0f8}(0.278,0.227,0.09)
 RGB{N0f8}(0.333,0.267,0.204)  RGB{N0f8}(0.969,0.91,0.78)    RGB{N0f8}(0.298,0.239,0.11)
 RGB{N0f8}(0.596,0.502,0.408)  RGB{N0f8}(0.671,0.6,0.459)    RGB{N0f8}(0.239,0.18,0.067)

julia> img1=load("lixa.JPG")
Error encountered while loading "v:\lixa.JPG".
Fatal error:
ERROR: RegistryKeyLookupFailed `CoderModulesPath' @ error/module.c/GetMagickModulePath/666
 in error(::ImageMagick.MagickWand) at c:\j\.julia\v0.5\ImageMagick\src\libmagickwand.jl:198
 in readimage(::ImageMagick.MagickWand, ::String) at c:\j\.julia\v0.5\ImageMagick\src\libmagickwand.jl:277
 in #load_#20(::Type{T}, ::String, ::Void, ::Function, ::String) at c:\j\.julia\v0.5\ImageMagick\src\ImageMagick.jl:72
 in #load#13(::Array{Any,1}, ::Function, ::FileIO.File{FileIO.DataFormat{:JPEG}}) at c:\j\.julia\v0.5\ImageMagick\src\ImageMagick.jl:51
 in anonymous at .\<missing>:?
 in eval(::Module, ::Any) at .\boot.jl:234
 in #load#17(::Array{Any,1}, ::Function, ::FileIO.File{FileIO.DataFormat{:JPEG}}) at c:\j\.julia\v0.5\FileIO\src\loadsave.jl:87
 in load(::FileIO.File{FileIO.DataFormat{:JPEG}}) at c:\j\.julia\v0.5\FileIO\src\loadsave.jl:75
 in #load#13(::Array{Any,1}, ::Function, ::String) at c:\j\.julia\v0.5\FileIO\src\loadsave.jl:45
 in load(::String) at c:\j\.julia\v0.5\FileIO\src\loadsave.jl:45

I'm attaching the (tiny tiny) test image for the case you find it useful

lixa

@timholy
Copy link
Member

timholy commented Mar 3, 2021

Not sure if this is still relevant? GMT is a bit harder to install than I'm willing to work through in order to test this.

@joa-quim
Copy link
Author

joa-quim commented Mar 3, 2021

I had forgotten about this, so probably not relevant. Tried to run it again but got a different error that can be replicated with

im = rand(UInt8, 4,3,3);
imf8 = reinterpret(N0f8, im);

 img2 = colorview(RGB, imf8)
ERROR: ArgumentError: `reinterpret(reshape, RGB{N0f8}, a)` where `eltype(a)` is N0f8 requires that `axes(a, 1)` (got Base.OneTo(4)) be equal to 1:3 (from the ratio of element sizes)
Stacktrace:
 [1] (::Base.var"#throwsize1#244")(a::Base.ReinterpretArray{N0f8, 3, UInt8, Array{UInt8, 3}, false}, T::Type)

Not pushing you on this but GMT is not difficult to install (but it's not a single ] add)

https://github.com/GenericMappingTools/gmt/blob/master/INSTALL.md#ubuntudebian
https://github.com/GenericMappingTools/gmt/blob/master/INSTALL.md#install-via-conda

Just not sure if the conda install makes it visible outside conda environment, which is needed for GMT.jl to find it

@timholy
Copy link
Member

timholy commented Mar 3, 2021

That error has nothing to do with this package; your first dimension has to be of size 3 since RGB has 3 color channels.

@joa-quim
Copy link
Author

joa-quim commented Mar 3, 2021

Well I made up that example because (and before it worked up to this point)

julia> using FixedPointNumbers, Images, FileIO, GMT

julia> I = gmtread("lixa.JPG");

julia> imf8 = reinterpret(N0f8, I.image)
4×3×3 reinterpret(N0f8, ::Array{UInt8, 3}):
[:, :, 1] =
 0.282  0.486  0.694
 0.263  0.373  0.216
 0.239  0.89   0.176
 0.514  0.843  0.137

[:, :, 2] =
 0.871  0.227  0.204
 0.835  0.09   0.969
 0.714  0.333  0.91
 0.278  0.267  0.78

[:, :, 3] =
 0.298  0.502  0.459
 0.239  0.408  0.239
 0.11   0.671  0.18
 0.596  0.6    0.067

julia> img2 = colorview(RGB, imf8)
ERROR: ArgumentError: `reinterpret(reshape, RGB{N0f8}, a)` where `eltype(a)` is N0f8 requires that `axes(a, 1)` (got Base.OneTo(4)) be equal to 1:3 (from the ratio of element sizes)

@timholy
Copy link
Member

timholy commented Mar 4, 2021

Use rawview(channeview(I.image)). I'm guessing that I.image is either an RGBA or perhaps an RGBX. (Are you on MacOS?)

@joa-quim
Copy link
Author

joa-quim commented Mar 4, 2021

rawview(channeview(I.image)) doesn't error but gives actually the same content of I.image

julia> rawview(channelview(I.image))
4×3×3 Array{UInt8, 3}:
[:, :, 1] =
 0x48  0x7c  0xb1
 0x43  0x5f  0x37
 0x3d  0xe3  0x2d
 0x83  0xd7  0x23

...

julia> I.image
4×3×3 Array{UInt8, 3}:
[:, :, 1] =
 0x48  0x7c  0xb1
 0x43  0x5f  0x37
 0x3d  0xe3  0x2d
 0x83  0xd7  0x23
...

I.image is a RGB (Array{UInt8,3}). See https://www.generic-mapping-tools.org/GMT.jl/dev/types/#Image-type
but never mind, as I said I was only trying to see if original issue still stand ed.

And, I'm on Windows.

@timholy
Copy link
Member

timholy commented Mar 4, 2021

OK, so it's not color at all. I'm confused about why you need the reinterpret if it's already Array{UInt8,3}. EDIT: now I get it, it was UInt8 and you're reinterpreted it as N0f8. 👍 For colorview to work you just need to permute the dimensions, I'm guessing you have the color channel last. (You can either use permutedims or the lazy variant PermutedDimsArray.)

The bottom line is this: when you do colorview(RGB, A), what happens is that A[1, :, :, ...] gets used for the red channel, A[2, :, :, ...] for the green channel, and A[3, :, :, ...] for the blue channel. When you also have an A[4, :, :, ...], it throws an error because what is it supposed to do with the 4th element?

But again, this has nothing to do with FileIO.

@joa-quim
Copy link
Author

joa-quim commented Mar 4, 2021

Yes it is a color. It comes from reading the tinny tinny jpg image at the bottom of first post (just changed its name to fit with what's in that first post).

I = gmtread("lixa.JPG");

I guess that at the time I was seeking a way to display the images in GMTimage type using colors. I now have some code to change the memory layout so that it's easier to do that.
Again, I don't remember anymore the motivation that lead to first post and now I was trying to reproduce it.

@timholy
Copy link
Member

timholy commented Mar 4, 2021

No worries. I tried installing GMT again but I get an error. For the record it's

julia> using GMT
[ Info: Precompiling GMT [5752ebe1-31b9-557e-87aa-f909b540aa54]
ERROR: LoadError: LoadError: could not load library "/usr/lib/x86_64-linux-gnu/libgmt.so"
/usr/lib/x86_64-linux-gnu/libgmt.so: cannot open shared object file: No such file or directory
Stacktrace:
  [1] GMT_Create_Session
    @ ~/.julia/packages/GMT/zIejk/src/libgmt.jl:26 [inlined]
  [2] top-level scope
...

despite having installed those Ubuntu packages. 🤷

It's straightforward for FileIO to open the image itself, as you say. I'm not quite sure what to do here. Maybe just drop it until there's a clearer reproducer?

@joa-quim
Copy link
Author

joa-quim commented Mar 4, 2021

Yes, better to close this.
Regarding the GMT error. It looks like a dependency problem because otherwise it had correctly found the libgmt.so. What happens if you run this on a bash shell

gmt --show-modules

does it print a long list of modules?
nix is terrible with its permanent breaking of dependencies. For example my WSL version now complains (but it's nice that it is an understandable message)

julia> using GMT
gmt [ERROR]: Unable to open GMT shared libgmt library: libnetcdf.so.13: cannot open shared object file: No such file or directory

@joa-quim
Copy link
Author

joa-quim commented Mar 4, 2021

Does this command in a unix shell (in pure GMT syntax, not GMT.jl) pops up this nice map? (probably not since you had an error from Julia but that's what I'm trying to figure out what was)

gmt grdimage @earth_relief_05m -JR12 -png map

It takes some time since it downloads a 10 Mb file reformats it, must project, etc

map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants