2
votes

I have following matrix of 10x10 pixel image, the third dimension indicates rgb values of the color that needs to come at that pixel.

> mymat3
, , 1

       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,] 0.010 0.010 0.010 0.000 0.000 0.000 0.033 0.033 0.033 0.010
 [2,] 0.948 0.948 0.948 0.948 0.929 0.929 0.929 0.910 0.910 0.910
 [3,] 0.986 0.986 1.000 1.000 1.000 0.976 0.976 0.976 0.986 0.986
 [4,] 0.010 0.010 0.010 0.010 0.010 0.010 0.014 0.014 0.014 0.014
 [5,] 0.957 0.943 0.943 0.943 0.919 0.919 0.919 0.895 0.895 0.895
 [6,] 0.981 0.981 0.981 0.981 0.981 0.986 0.986 0.986 0.967 0.967
 [7,] 0.038 0.038 0.038 0.014 0.014 0.014 0.024 0.024 0.024 0.033
 [8,] 0.948 0.919 0.919 0.919 0.905 0.905 0.905 0.895 0.895 0.895
 [9,] 0.981 0.981 0.995 0.995 0.995 0.971 0.971 0.971 0.981 0.981
[10,] 0.014 0.014 0.014 0.029 0.029 0.029 0.014 0.014 0.014 0.029

, , 2

       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,] 0.933 0.933 0.948 0.948 0.948 0.990 0.990 0.990 0.995 0.995
 [2,] 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443
 [3,] 0.233 0.233 0.233 0.233 0.233 0.233 0.233 0.229 0.229 0.229
 [4,] 0.929 0.929 0.929 0.929 0.929 0.957 0.957 0.957 0.981 0.981
 [5,] 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443
 [6,] 0.224 0.238 0.238 0.238 0.233 0.233 0.233 0.233 0.233 0.233
 [7,] 0.948 0.948 0.943 0.943 0.943 0.976 0.976 0.976 0.981 0.981
 [8,] 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443
 [9,] 0.229 0.238 0.238 0.238 0.248 0.248 0.248 0.233 0.233 0.233
[10,] 0.943 0.943 0.971 0.971 0.971 0.981 0.981 0.981 0.948 0.948

, , 3

       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,] 0.295 0.290 0.290 0.290 0.295 0.295 0.295 0.295 0.295 0.295
 [2,] 0.162 0.162 0.143 0.143 0.143 0.133 0.133 0.133 0.143 0.143
 [3,] 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443
 [4,] 0.295 0.290 0.290 0.290 0.295 0.295 0.295 0.295 0.295 0.295
 [5,] 0.148 0.148 0.162 0.162 0.162 0.143 0.143 0.143 0.152 0.152
 [6,] 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443
 [7,] 0.295 0.295 0.295 0.295 0.295 0.295 0.295 0.290 0.290 0.290
 [8,] 0.162 0.162 0.162 0.162 0.162 0.152 0.152 0.152 0.143 0.143
 [9,] 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443 0.443
[10,] 0.295 0.300 0.300 0.300 0.295 0.295 0.295 0.290 0.290 0.290

> dput(mymat3)
structure(c(0.01, 0.948, 0.986, 0.01, 0.957, 0.981, 0.038, 0.948, 
0.981, 0.014, 0.01, 0.948, 0.986, 0.01, 0.943, 0.981, 0.038, 
0.919, 0.981, 0.014, 0.01, 0.948, 1, 0.01, 0.943, 0.981, 0.038, 
0.919, 0.995, 0.014, 0, 0.948, 1, 0.01, 0.943, 0.981, 0.014, 
0.919, 0.995, 0.029, 0, 0.929, 1, 0.01, 0.919, 0.981, 0.014, 
0.905, 0.995, 0.029, 0, 0.929, 0.976, 0.01, 0.919, 0.986, 0.014, 
0.905, 0.971, 0.029, 0.033, 0.929, 0.976, 0.014, 0.919, 0.986, 
0.024, 0.905, 0.971, 0.014, 0.033, 0.91, 0.976, 0.014, 0.895, 
0.986, 0.024, 0.895, 0.971, 0.014, 0.033, 0.91, 0.986, 0.014, 
0.895, 0.967, 0.024, 0.895, 0.981, 0.014, 0.01, 0.91, 0.986, 
0.014, 0.895, 0.967, 0.033, 0.895, 0.981, 0.029, 0.933, 0.443, 
0.233, 0.929, 0.443, 0.224, 0.948, 0.443, 0.229, 0.943, 0.933, 
0.443, 0.233, 0.929, 0.443, 0.238, 0.948, 0.443, 0.238, 0.943, 
0.948, 0.443, 0.233, 0.929, 0.443, 0.238, 0.943, 0.443, 0.238, 
0.971, 0.948, 0.443, 0.233, 0.929, 0.443, 0.238, 0.943, 0.443, 
0.238, 0.971, 0.948, 0.443, 0.233, 0.929, 0.443, 0.233, 0.943, 
0.443, 0.248, 0.971, 0.99, 0.443, 0.233, 0.957, 0.443, 0.233, 
0.976, 0.443, 0.248, 0.981, 0.99, 0.443, 0.233, 0.957, 0.443, 
0.233, 0.976, 0.443, 0.248, 0.981, 0.99, 0.443, 0.229, 0.957, 
0.443, 0.233, 0.976, 0.443, 0.233, 0.981, 0.995, 0.443, 0.229, 
0.981, 0.443, 0.233, 0.981, 0.443, 0.233, 0.948, 0.995, 0.443, 
0.229, 0.981, 0.443, 0.233, 0.981, 0.443, 0.233, 0.948, 0.295, 
0.162, 0.443, 0.295, 0.148, 0.443, 0.295, 0.162, 0.443, 0.295, 
0.29, 0.162, 0.443, 0.29, 0.148, 0.443, 0.295, 0.162, 0.443, 
0.3, 0.29, 0.143, 0.443, 0.29, 0.162, 0.443, 0.295, 0.162, 0.443, 
0.3, 0.29, 0.143, 0.443, 0.29, 0.162, 0.443, 0.295, 0.162, 0.443, 
0.3, 0.295, 0.143, 0.443, 0.295, 0.162, 0.443, 0.295, 0.162, 
0.443, 0.295, 0.295, 0.133, 0.443, 0.295, 0.143, 0.443, 0.295, 
0.152, 0.443, 0.295, 0.295, 0.133, 0.443, 0.295, 0.143, 0.443, 
0.295, 0.152, 0.443, 0.295, 0.295, 0.133, 0.443, 0.295, 0.143, 
0.443, 0.29, 0.152, 0.443, 0.29, 0.295, 0.143, 0.443, 0.295, 
0.152, 0.443, 0.29, 0.143, 0.443, 0.29, 0.295, 0.143, 0.443, 
0.295, 0.152, 0.443, 0.29, 0.143, 0.443, 0.29), .Dim = c(10L, 
10L, 3L))

Hence (,,1) gives red values, (,,2) indicates green value and (,,3) indicates blue values at each pixel point.

I tried following but they give me errors:

> image(mymat3)
Error in image.default(mymat3) : 'z' must be a matrix
> 
> rasterImage(mymat3)
Error in rasterImage(mymat3) : 
  argument "xleft" is missing, with no default

How can I plot this matrix to see the image?

2
Why do you have negative values in your matrix? You need values in the range 0-1, as the error message explicitly states.thelatemail
You have negative values. It can't be RGB colorspace.user3710546
Thanks for pointing out. I changed to 0-1 values and edited the question above. Still errors are there.rnso

2 Answers

5
votes

If you just want to show an image, use grid::grid.raster

library(grid)
grid.raster(mymat3)

enter image description here

If you want to embed a raster in graph, use rasterImage with limits:

plot(c(0, 100), c(0, 100), type = "n")
rasterImage(mymat3, 10, 10, 90, 90)

enter image description here

1
votes

You can plot that with countcolors:plotArrayAsImage:

library(png)
img = readPNG("myimg.png")

library(countcolors)
plotArrayAsImage(img, main = "My Image")