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

Error with reinterpret when setting a scale #232

Closed
rsrock opened this issue Dec 10, 2014 · 9 comments
Closed

Error with reinterpret when setting a scale #232

rsrock opened this issue Dec 10, 2014 · 9 comments

Comments

@rsrock
Copy link
Collaborator

rsrock commented Dec 10, 2014

I have an image where I can display it just fine, but when I attempt to assign a scale (ScaleAutoMinMax(Ufixed16)), I run into problems. The error seems to be in map.jl:339, through take() at line 347, and up into ScaleMinMax around line 161, but I lose track of things at that point. Here's a working example, followed by the scaling and the error:

julia> using Images, ImageView

julia> img = imread("BZ363A\ 1\ redgreen.tif")
Gray Image with:
  data: 512x512x583 Array{Gray{UfixedBase{Uint16,16}},3}
  properties:
    timedim: 3
    imagedescription: <suppressed>
    spatialorder:  x y
    pixelspacing:  1 1

julia> view(img)

...works...

julia> using FixedPointNumbers

julia> img["scalei"] = ScaleAutoMinMax(Ufixed16)
ScaleAutoMinMax{UfixedBase{Uint16,16}}()

julia> view(img)

ERROR: reinterpret: expected bits type as first argument
 in map! at /Users/rrock/.julia/v0.3/Images/src/map.jl:339
 in uint32color! at /Users/rrock/.julia/v0.3/Images/src/map.jl:481
 in anonymous at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:317
 in rerender at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:689
 in view at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:327

julia>

This also happens when I take a slice of this image stack, and apply the scaling to the slice.
Any ideas where to go from here?

@timholy
Copy link
Member

timholy commented Dec 11, 2014

The argument you supply to ScaleAutoMinMax refers to the type you want to convert to, not the type you convert from. Try using Color; img["scalei"] = ScaleAutoMinMax(RGB24). (RGB24 is effectively the type that Cairo uses for display; it's equivalent to a Uint32, but far clearer in intent.)

@rsrock
Copy link
Collaborator Author

rsrock commented Dec 11, 2014

Ah, thanks for that tip. I was always thinking of the Scale types and map methods as "convert the specified type for display purposes". After rereading the docs, I see that it's for converting to just about anything.

However, I still seem to have the same problem here. I get an error with ScaleAutoMinMax still, while ScaleMinMax works for me. I put in a couple of @show statements in map! on line 336 here.

julia> using Images, ImageView, Color, FixedPointNumbers

julia> img = imread("BZ363A\ 1\ redgreen.tif")
Gray Image with:
  data: 512x512x583 Array{Gray{UfixedBase{Uint16,16}},3}
  properties:
    timedim: 3
    imagedescription: <suppressed>
    spatialorder:  x y
    pixelspacing:  1 1

julia> img["scalei"] = ScaleMinMax(RGB24, 0.0, 0.05)
ScaleMinMax{RGB24,Float64,Float32}(0.0,0.05,20.0f0)

julia> view(img)

...works...

mapi => ScaleMinMax{RGB24,Float64,Float32}(0.0,0.05,20.0f0)
img => Gray Image with:
  data: 512x512 SubArray{Gray{UfixedBase{Uint16,16}},2,Array{Gray{UfixedBase{Uint16,16}},3},(UnitRange{Int64},UnitRange{Int64},Int64)}
  properties:
    timedim: 0
    scalei: ScaleMinMax{RGB24,Float64,Float32}(0.0,0.05,20.0f0)
    imagedescription: <suppressed>
    spatialorder:  x y
    pixelspacing:  1 1
(ImageCanvas,ImageSlice2d: zoom = BoundingBox(0.0,512.0,0.0,512.0), t = 1)

julia> img["scalei"] = ScaleAutoMinMax(RGB24)
ScaleAutoMinMax{RGB24}()

julia> view(img)

...fails...

mapi => ScaleAutoMinMax{RGB24}()
img => Gray Image with:
  data: 512x512 SubArray{Gray{UfixedBase{Uint16,16}},2,Array{Gray{UfixedBase{Uint16,16}},3},(UnitRange{Int64},UnitRange{Int64},Int64)}
  properties:
    timedim: 0
    scalei: ScaleAutoMinMax{RGB24}()
    imagedescription: <suppressed>
    spatialorder:  x y
    pixelspacing:  1 1
ERROR: reinterpret: expected bits type as first argument
 in map! at /Users/rrock/.julia/v0.3/Images/src/map.jl:338
 in uint32color! at /Users/rrock/.julia/v0.3/Images/src/map.jl:478
 in anonymous at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:317
 in rerender at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:689
 in view at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:327

@rsrock
Copy link
Collaborator Author

rsrock commented Dec 11, 2014

I guess the real trouble is that I don't know where reinterpret is being called here.

@timholy
Copy link
Member

timholy commented Dec 12, 2014

After rereading the docs, I see that it's for converting to just about anything.

Yes, MapInfo is really powerful, and perhaps under-appreciated (maybe, under-documented 😄). Think of it as your gateway to "lazy" computation on images; at the very least it is convert on steroids. Not only do you specify the type that you want out, but you can also specify the algorithm. I use it all the time for sophisticated things like tingeing ROIs of grayscale images each with a different color during display. You could define a MapInfo type that blurs the image, etc.

Regarding your error, that's quite puzzling. I don't have that particular file, but:

julia> using Images, Color, FixedPointNumbers, ImageView

julia> img = grayim(rand(Uint16, 5,5,5))
Gray Image with:
  data: 5x5x5 Array{UfixedBase{Uint16,16},3}
  properties:
    colorspace: Gray
    spatialorder:  x y z

julia> map(ScaleAutoMinMax(RGB24), img)
RGB24 Image with:
  data: 5x5x5 Array{RGB24,3}
  properties:
    colorspace: Gray
    spatialorder:  x y z

julia> img["scalei"] = ScaleAutoMinMax(RGB24)
ScaleAutoMinMax{RGB24}()

julia> view(img)
(ImageCanvas,ImageSlice2d: zoom = BoundingBox(0.0,5.0,0.0,5.0), z = 1)

works for me.

Try showing mi after the take operation. Theoretically, you shouldn't even need ImageView to test this.

@rsrock
Copy link
Collaborator Author

rsrock commented Dec 12, 2014

Your test worked for me. Now I think I have a way for you to reproduce this. It does seem to need ImageView, because the final map call here works. I think it also needs an image of type Gray{Ufixed16} rather than just Ufixed16. That matches my problematic image. It's not a 3D issue, a 2D image will trigger it:

julia> using Images, Color, FixedPointNumbers, ImageView
julia> img = Image(reinterpret(Gray{Ufixed16}, rand(Uint16, 5, 5)))
Gray Image with:
  data: 5x5 Array{Gray{UfixedBase{Uint16,16}},2}
  properties:

julia> img["spatialorder"] = ["x", "y"]
2-element Array{ASCIIString,1}:
 "x"
 "y"

julia> img["scalei"] = ScaleAutoMinMax(RGB24)
ScaleAutoMinMax{RGB24}()

julia> view(img)
ERROR: reinterpret: expected bits type as first argument
 in map! at /Users/rrock/.julia/v0.3/Images/src/map.jl:336
 in uint32color! at /Users/rrock/.julia/v0.3/Images/src/map.jl:477
 in anonymous at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:317
 in rerender at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:689
 in view at /Users/rrock/.julia/v0.3/ImageView/src/display.jl:327

julia> map(ScaleAutoMinMax(RGB24), img)
mi => ScaleMinMax{RGB24,Gray{UfixedBase{Uint16,16}},Float32}(Gray{Ufixed16}(0.1293),Gray{Ufixed16}(0.96741),1.1931725f0)
RGB24 Image with:
  data: 5x5 Array{RGB24,2}
  properties:
    scalei: ScaleAutoMinMax{RGB24}()
    spatialorder:  x y

@rsrock
Copy link
Collaborator Author

rsrock commented Dec 12, 2014

P.S. I'll have to play around with those other uses of MapInfo here. Very cool.

@timholy
Copy link
Member

timholy commented Dec 14, 2014

Sorry this took so long. It was drattedly subtle. The call chain turned out to be map! -> take -> ScaleMinMax -> minfinite -> reinterpret. Absolutely none of those showed up in the backtrace. Inlining definitely makes debugging harder sometimes. This was enough to make me think we really need a --noinline startup mode.

ImageView creates a SubArray (to handle zoom regions), which was the final ingredient to the puzzle.

My main trick was to insert @show @which func(args...) right before the offending function calls, and then insert the same command in whichever function was being called, and so on down the chain.

@rsrock
Copy link
Collaborator Author

rsrock commented Dec 17, 2014

Thanks! Works great again. I did make it all the way to minfinite, but some testing at the REPL had fooled me into thinking that I was on a wild goose chase. This one was a pain.

I also remember seeing that the @which and @show macros were triggering different errors for me, and so I was a bit suspicious of what they were telling me. Do you ever see that?

@timholy
Copy link
Member

timholy commented Dec 17, 2014

Glad it's working. You may have also noted that this inspired JuliaLang/julia#9354

I haven't seen errors from @show or @which except (1) from typos or (2) too early in julia's bootstrap process.

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