# How to convert Matrix of Ints into image

**URL:** <https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273>\
**Category:** New to Julia\
**Tags:** question, images, plots\
**Created:** [August 4, 2022, 1:09am UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273 "2022-08-04T01:09:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [August 4, 2022, 1:09am UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/1 "2022-08-04T01:09:44Z")

</div>

I have a matrix of Ints. I’m using it for semantic classification so each pixel is a groundtruth label value, e.g.

> (10,20)=3 where at coordinate 10,20 there is a pixel value 3 which is the class.

This ground truth label will be used in UNet. I’m wondering how to convert this matrix into a ‘Target Image’ for UNet. I’ve looked at [Get an image from a matrix of signed integers](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701) `

> imadjustintensity(Float64.(gt), extrema(gt))

` but its very indistinguishable for different classes, they are all a shade of similar gray. Could/Should I enhance it? Is there a better way than converting to float?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 4, 2022, 9:17am UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/2 "2022-08-04T09:17:46Z")

</div>

With Plots.jl, see if the following heatmap example with 10 classes can help:

```julia
using Plots
n = 10
M = rand(1:n,20,20)
dc = distinguishable_colors(n)
heatmap(M, color=palette(dc, n), lims=(0.5,20.5), ratio=1, clims=(0.5,n+0.5))

```

 ![Plots_heatmap_distinguishable_colors](https://global.discourse-cdn.com/julialang/original/3X/d/0/d0396d8d7fc0165d3073375dbd25f2a7d54e49dd.png)

---

<div class="post-metadata">

**Author:** ![sefffal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sefffal/32/23640_2.png) [@sefffal](https://discourse.julialang.org/u/sefffal)\
**Post date:** [August 5, 2022, 3:13pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/3 "2022-08-05T15:13:23Z")

</div>

You can try the AstroImages.jl function `imview`. It takes any matrix and an optional Color scheme and converts directly to an image without going through Plots. It should have nice normalization by default or you can tweak contrast etc.

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [August 6, 2022, 5:52am UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/4 "2022-08-06T05:52:54Z")

</div>

You can also do this with only the standard `Images` package:

```julia
using Images
matrix = rand(1:10, 20, 20)
scheme = rand(RGB, 10) # specify your colouring here

```

 ![Screen Shot 2022-08-06 at 5.52.06 PM](https://global.discourse-cdn.com/julialang/original/3X/6/9/69d87cd988f71db269e9ef5a4c0746ec2d6ab7dc.png)

```julia
img = scheme[matrix]

```

![download](https://global.discourse-cdn.com/julialang/original/3X/6/f/6fd7bf2cfe0ccd0de7f659fa1800fd5888572754.png)

In a Jupyter or Pluto notebook you will see the image as the return value. You can save to a file with

```julia
save("output.png", img)

```

---

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [August 6, 2022, 12:29pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/5 "2022-08-06T12:29:38Z")

</div>

Thanks, I tried but I got an error ‘Segmentation fault’

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 6, 2022, 12:38pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/6 "2022-08-06T12:38:31Z")

</div>

Plots.jl version used and size of matrix?

---

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [August 6, 2022, 12:40pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/7 "2022-08-06T12:40:44Z")

</div>

Matrx is size

> (5041, 6001)

type `Matrix{Int64} (alias for Array{Int64, 2})`  
and Plots.jl v1.31.5

---

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [August 6, 2022, 12:47pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/8 "2022-08-06T12:47:33Z")

</div>

> [@Jollywatt](#):
>
> `scheme = rand(RGB, 10)`

I tried it, but got the following error? The maximum label is n=28

```julia
julia> img=scheme[gt_mat]
ERROR: BoundsError: attempt to access 28-element Array{RGB{Float64},1} with eltype RGB{Float64} at index [[0 0 … 0 0; 0 0 … 0 0; … ; 0 0 … 0 0; 0 0 … 0 0]]
Stacktrace:
 [1] throw_boundserror(A::Vector{RGB{Float64}}, I::Tuple{Matrix{Int64}})
   @ Base ./abstractarray.jl:651
 [2] checkbounds
   @ ./abstractarray.jl:616 [inlined]
 [3] _getindex
   @ ./multidimensional.jl:831 [inlined]
 [4] getindex(A::Vector{RGB{Float64}}, I::Matrix{Int64})
   @ Base ./abstractarray.jl:1170
 [5] top-level scope
   @ REPL[24]:1
 [6] top-level scope
   @ ~/.julia/packages/CUDA/DfvRa/src/initialization.jl:52

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [August 6, 2022, 12:57pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/9 "2022-08-06T12:57:41Z")

</div>

`gt_mat` should be an array of integers equal to or greater than 1 (and less than scheme’s length). Otherwise you’ll be indexing arrays with 0…

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [August 6, 2022, 1:06pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/11 "2022-08-06T13:06:09Z")

</div>

If you write a function `label_color(label)` which takes your labels — whether they start at zero or whatever else — you can convert your integer matrix to a colour matrix with:

```julia
img = [label_color(label) for label in gt_mat]

```

This array comprehension preserves the shape of the matrix, so produces a matrix of colours.

* * *

For example, you might use a dictionary:

```julia
julia> using Plots: distinguishable_colors

julia> scheme = Dict(0:28 .=> distinguishable_colors(29));

julia> img = [scheme[label] for label in gt_mat];

```

---

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [August 6, 2022, 1:08pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/12 "2022-08-06T13:08:15Z")

</div>

Yes, I missed that. Thank you big time

---

<div class="post-metadata">

**Author:** ![momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@momo1](https://discourse.julialang.org/u/momo1)\
**Post date:** [August 6, 2022, 1:23pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/13 "2022-08-06T13:23:29Z")

</div>

label\_color  
So this color label is fixed everytime a new image is colored ? e.g. from 0-28 each label has a fixed color or is it randomly generated?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 6, 2022, 3:04pm UTC](https://discourse.julialang.org/t/how-to-convert-matrix-of-ints-into-image/85273/14 "2022-08-06T15:04:51Z")

</div>

> [@momo1](#):
>
> Matrix is size = (5041, 6001)
> 
> type `Matrix{Int64} (alias for Array{Int64, 2})`  
> and Plots.jl v1.31.5

Using same Plots.jl version, Julia 1.7.3 on Win11, no problem to display such matrix with ~30 million cells:

```julia
using Plots; gr(dpi=300)
n = 10
M = rand(1:n, 5041, 6001)
dc = distinguishable_colors(n)
heatmap(M, color=palette(dc,n), framestyle=:none, ratio=1, clims=(0.5,n+0.5))

```

But with so many pixels it might be difficult to read:

 ![Plots_gr_heatmap_30_million_cells_distinguishable_colors](https://global.discourse-cdn.com/julialang/original/3X/c/c/cc22fcd7df2d3d83b5b97a76f22fbec9b78fc820.jpeg)
