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

Add some missing convert(Array, img) methods #208

Closed
tedsteiner opened this issue Oct 13, 2014 · 9 comments
Closed

Add some missing convert(Array, img) methods #208

tedsteiner opened this issue Oct 13, 2014 · 9 comments

Comments

@tedsteiner
Copy link

I am new to Images.jl, so there is definitely extensive functionality that I don't understand yet. Is there an easy way to interpolate a pixel value in an image, such as using Grid? I am looking to apply a homography to an image, and would like to do so as cleanly as possible.

Here is rough pseudocode of what I would like to do:

imgA = imread(filename)
grid = Grid.InterpGrid(imgA,BCnearest,InterpQuadratic)

imgB = NewEmptyImage(height,width)
for pixels in imgB
    imgB[pixel] = grid[i,j]
end

Where NewEmptyImage is some function that just creates an empty image of a prespecified size, and we can just assume that I know how to compute i and j for my purposes.

I'm trying to convert some code from Matlab, and in Matlab I just used generic arrays to store my images. That's another option for me here, but it seems like it would require a little more overhead any time I wanted to view or save an image. For my purposes, I only need 2D grayscale images, and was able to interpolate on that by converting it to an array of Float64's, but then I still need to convert it back into an image to see the result.

@timholy
Copy link
Member

timholy commented Oct 13, 2014

Grid is unnecessarily picky about its inputs; Grid is by this point a pretty old package, and a rewrite is slowly underway (https://github.com/tlycken/Interpolations.jl). Your best workaround is something like this: A = reinterpret(Float32, data(float32(imgA))) and then do your operations with A. If you want to go back to encoding image information, you can say share(imgA, A).

You shouldn't have to convert back to an Image to see the result; viewing should work just fine on plain arrays, modulo changes in orientation, etc.

@tedsteiner
Copy link
Author

Thanks for suggesting that workaround, I think that should do it for me. It results in a transpose, but that's not a big deal.

@timholy
Copy link
Member

timholy commented Oct 14, 2014

It doesn't actually transpose any data; the issue is that ImageView can handle images in either "row major" or "column major" format, but it requires the Image container to know what to do. Most/all image formats store data in "row major" format, and the rule in Images is "don't do anything you don't have to do," so it does not automatically take the transpose for you. You can alternatively say A = convert(Array, reinterpret(Float32, float32(img))) and it will do the transpose for you to put it in "canonical order." This works no matter how the original image is stored.

As a reminder to myself, I'm going to reopen this because I noticed that Images is missing some convert(Array, img) methods---you can't put the commands in that convert line in any order, and there's no good reason for that (other than the methods don't yet exist).

@timholy timholy reopened this Oct 14, 2014
@timholy timholy changed the title Interpolating Images with Grid.jl Add some missing convert(Array, img) methods Oct 14, 2014
@abrazhe
Copy link

abrazhe commented Oct 23, 2014

Since this thread's title suggests the convert topic, here I'll describe my issue:
I was trying to follow the README, but get an error like this:

    julia> img = testimage("mandrill")
    RGB Image with:
      data: 512x512 Array{RGB{UfixedBase{Uint8,8}},2}
      properties:
        IMcs: sRGB
        spatialorder:  x y
        pixelspacing:  1 1

    julia> a = convert(Array,img)
    ERROR: `convert` has no method matching convert(::Type{Array{T,N}}, ::Image{RGB{UfixedBase{Uint8,8}},2,Array{RGB{UfixedBase{Uint8,8}},2}})
     in convert at base.jl:13

Is this the correct behavior? Also, what would be the idiomatic way to just get a 2D array of floats from a grayscale image loaded with Images.imread?

I have julia 0.3.1 and these are my packages:

julia> Pkg.status()
11 required packages:
 - ArrayViews                    0.4.6
 - Cairo                         0.2.20
 - Distributions                 0.5.4
 - Gadfly                        0.3.9
 - HDF5                          0.4.5
 - IJulia                        0.1.15
 - ImageView                     0.1.7
 - Images                        0.4.21
 - Iterators                     0.1.7
 - PyPlot                        1.4.0
 - TestImages                    0.0.6
37 additional packages:
 - BinDeps                       0.3.6
 - Codecs                        0.1.2
 - Color                         0.3.10
 - Compat                        0.1.0
 - Compose                       0.3.9
 - Contour                       0.0.5
 - DataArrays                    0.2.4
 - DataFrames                    0.5.11
 - DataStructures                0.3.4
 - Dates                         0.3.2
 - Distances                     0.1.1
 - FixedPointNumbers             0.0.4
 - GZip                          0.2.13
 - Hexagons                      0.0.2
 - ImmutableArrays               0.0.6
 - IniFile                       0.2.3
 - JSON                          0.3.8
 - KernelDensity                 0.0.2
 - LaTeXStrings                  0.1.0
 - Loess                         0.0.3
 - Nettle                        0.1.6
 - PDMats                        0.2.4
 - PyCall                        0.4.10
 - REPLCompletions               0.0.3
 - Reexport                      0.0.2
 - SHA                           0.0.3
 - SIUnits                       0.0.2
 - Showoff                       0.0.2
 - SortingAlgorithms             0.0.2
 - StatsBase                     0.6.8
 - TexExtensions                 0.0.2
 - Tk                            0.2.15
 - URIParser                     0.0.3
 - Winston                       0.11.5
 - ZMQ                           0.1.14
 - ZipFile                       0.2.2
 - Zlib                          0.1.7

@abrazhe
Copy link

abrazhe commented Oct 23, 2014

Similar issue: when I use rgb2gray, I get deprecation warning, but when I'm trying to follow the suggested usage, I get an error:

julia> x = rgb2gray(img)
WARNING: rgb2gray(img::AbstractArray) is deprecated, use convert(Array{Gray},img) instead.
 in rgb2gray at deprecated.jl:26

julia> convert(Array{Gray},img)
ERROR: `convert` has no method matching convert(::Type{Array{Gray{T<:Union(FixedPoint,FloatingPoint)},N}}, ::Image{RGB{UfixedBase{Uint8,8}},2,Array{RGB{UfixedBase{Uint8,8}},2}})
 in convert at base.jl:13

@timholy
Copy link
Member

timholy commented Oct 26, 2014

Sorry for the delay; I've been away without internet for several days.

This is probably a julia bug, maybe related to #8631 (I know it doesn't look like it, but it just seems that convert is broken). Check this out:

julia> using Images, TestImages

julia> img = testimage("mandrill")
RGB Image with:
  data: 512x512 Array{RGB{UfixedBase{Uint8,8}},2}
  properties:
    IMcs: sRGB
    spatialorder:  x y
    pixelspacing:  1 1

julia> A = convert(Array, img)
ERROR: `convert` has no method matching convert(::Type{Array{T,N}}, ::Image{RGB{UfixedBase{Uint8,8}},2,Array{RGB{UfixedBase{Uint8,8}},2}})
 in convert at base.jl:13

julia> @which convert(Array, img)
convert(::Type{Array{T,N}},img::AbstractImage{T,N}) at /home/tim/.julia/v0.3/Images/src/core.jl:227

I inserted some printlns, which makes the line numbers different from what you would get from this test. Those statements reveal that it never calls that method reported by @which.

@timholy
Copy link
Member

timholy commented Oct 26, 2014

See JuliaLang/julia#8818, in case it's new behavior.

@abrazhe
Copy link

abrazhe commented Oct 26, 2014

Just in case, this works:

convert(Array, data(img))

@timholy
Copy link
Member

timholy commented Nov 15, 2014

This was fixed by Color 0.3.12 (JuliaAttic/Color.jl@e880cbf).

@timholy timholy closed this as completed Nov 15, 2014
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

3 participants