# Imref2d alternative in Julia

**URL:** <https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 10, 2022, 8:15pm UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581 "2022-06-10T20:15:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 10, 2022, 8:15pm UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/1 "2022-06-10T20:15:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 10, 2022, 8:25pm UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/2 "2022-06-10T20:25:04Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 10, 2022, 8:46pm UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/3 "2022-06-10T20:46:01Z")

</div>

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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/2/b257dd8d80b607e06e1050555406a06ad26cab52.png)  
  
  

This is the respective Julia code I have written:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/7596b7a64a30a7344193782422679c22e985633e.png)  
  
  
And the respective image output:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/3/730f7235545c3d07a0d7df15804b9b8ec59da414.jpeg)  
  
  

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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c682b02d64e6e614c96b2f31a98e62b3e8fada0.png)  
  

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

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [June 10, 2022, 9:19pm UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/4 "2022-06-10T21:19:20Z")

</div>

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](https://octave.sourceforge.io/image/function/@imref2d/imref2d/m.html)

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 11, 2022, 1:31am UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/5 "2022-06-11T01:31:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 11, 2022, 1:38am UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/6 "2022-06-11T01:38:30Z")

</div>

For example:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 11, 2022, 1:40am UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/7 "2022-06-11T01:40:23Z")

</div>

Result:

 ![Screen Shot 2022-06-10 at 9.38.09 PM](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed4bb27a8de8f11fcc4bdccaac240eba97cd70b0.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 12, 2022, 2:55am UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/8 "2022-06-12T02:55:20Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![brenhinkeller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brenhinkeller/32/36648_2.png) [@brenhinkeller](https://discourse.julialang.org/u/brenhinkeller)\
**Post date:** [June 12, 2022, 4:40am UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/9 "2022-06-12T04:40:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 13, 2022, 2:47pm UTC](https://discourse.julialang.org/t/imref2d-alternative-in-julia/82581/10 "2022-06-13T14:47:01Z")

</div>

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