Imref2d alternative in Julia

Hello Everyone,

I am beginner to Julia and was trying to convert some MATLAB code to Julia. MATLAB has a function imref2d which according to MATLAB - " stores the relationship between the intrinsic coordinates anchored to the rows and columns of a 2-D image and the spatial location of the same row and column locations in a world coordinate system."

I was struggling to implement this in Julia. Any help in this matter would be appreciated.

Thanks

@Adi_Rai welcome to the community. Perhaps you could share with us what you are trying to achieve instead of trying to find an exact equivalent for the function.

1 Like

Of course.

I am trying to plot data which is in my MAT file (a MATLAB specific file which stores the workspace variables) . I am able to read the MAT file in Julia using the MAT.jl package.

This is the original MATLAB code I am converting:




This is the respective Julia code I have written:




And the respective image output:



The problem comes when I try to implement imref2d in Julia. I have no clue how imref2d references local image coordinates with respect to world coordinates. So I tried to set the axis limits using xlims and ylims. However the output image becomes distorted as the coordinates do not change wrt to the global coordinates:



I hope I was able to explain the issue. Kindly let me know if its still unclear.

This function also exists in octave, so I hoped to be able to find the source code, but I did not find it yet: Function Reference: @imref2d/imref2d.m

If you’re plotting this image with a package like Plots (or probably Makie for that matter), you actually don’t do this as a separate function but rather just by providing vectors of x and y coordinates to plot along with the image you want to plot

For example:

julia> using Images, Plots

julia> img = Images.load("cat.jpg")
355×474 Array{RGB{N0f8},2} with eltype RGB{N0f8}:
 RGB{N0f8}(0.455,0.306,0.161)  …  RGB{N0f8}(0.694,0.647,0.655)
 RGB{N0f8}(0.451,0.302,0.157)     RGB{N0f8}(0.69,0.643,0.651)
 RGB{N0f8}(0.451,0.302,0.157)     RGB{N0f8}(0.686,0.639,0.647)
 RGB{N0f8}(0.451,0.302,0.157)     RGB{N0f8}(0.682,0.635,0.643)
 RGB{N0f8}(0.451,0.302,0.157)     RGB{N0f8}(0.678,0.631,0.639)
 ⋮                             ⋱
 RGB{N0f8}(0.678,0.004,0.004)  …  RGB{N0f8}(0.384,0.122,0.008)
 RGB{N0f8}(0.659,0.008,0.008)     RGB{N0f8}(0.38,0.122,0.02)
 RGB{N0f8}(0.647,0.043,0.035)     RGB{N0f8}(0.408,0.161,0.09)
 RGB{N0f8}(0.729,0.149,0.141)     RGB{N0f8}(0.478,0.235,0.176)
 RGB{N0f8}(0.796,0.216,0.208)     RGB{N0f8}(0.467,0.235,0.173)

julia> xmin, xmax, ymin, ymax = -10,10,-10,10
(-10, 10, -10, 10)

julia> xscale = range(xmin, xmax, length=size(img,1))
-10.0:0.05649717514124294:10.0

julia> yscale = range(ymin, ymax, length=size(img,2)) # note: first pixel is top left, so want that to be ymax
-10.0:0.042283298097251586:10.0

julia> plot(xscale, yscale, img, xlabel="X", ylabel="Y") # for images, yflip=true by default. Set that to false if you don't want plots to auto-flip the picture right-way up

Result:

Now you should be able to select regions within the image using xlims and ylims and have that be with reference to the proper image coordinates rather than the pixel indices

Hi again @brenhinkeller,

Sorry for responding so late. I will try to implement your solution and get back to you.
Thanks for your repeated help!

No worries and good luck – hope this is what you were looking for!

I was able to use this and it was working perfectly. Thanks for your help!

2 Likes