Reputation: 5
I have the following code in R:
img = read.csv(file="img119.csv")
img is loaded like this:
X Y R G B
1 0 0 0.48046875 0.65234375 0.83593750
2 0 1 0.40234375 0.57031250 0.73828125
3 0 2 0.24609375 0.39453125 0.54687500
4 0 3 0.07031250 0.19921875 0.32031250
5 0 4 0.00000000 0.07812500 0.17187500
width = img[length(img[,1]),1]+1;
height = img[length(img[,2]),2]+1;
x = 1:width;
y = 1:height;
img[,3:5] = img[,3:5]/256;
rgb_colors = rgb(img[,3:5])
imgfunction <-function(xx,yy){
idx = xx*width + height - yy;
rgb_color = rgb_colors[idx];
return ( rgb_color )
}
rgb_colors_xy = outer(x,y,imgfunction)
image(x,y,rgb_colors)
I want to draw that csv file like a regular image. However, the last line gives me error as following:
Error in image.default(x, y, rgb_colors) : 'z' must be a matrix
I am not sure what I am doing wrong and appreciate if you help me fix this code.
Thanks,
Upvotes: 0
Views: 1605
Reputation: 21502
graphics::image
will not create a color image from 3-D data. You need to use raster::image
or rasterImage
. Read the help files carefully to understand how your RGB data must be converted to arrays. The "long" way is to create three x
by y
matrices and fill each one with something along the lines of
for(i in 1:max(x)) {
for(j in 1:max(y)) redmat[i,j]<- img[i,j,3]
}
There are faster tools which someone more awake than I will probably post. Note also that R indexes from 1, not zero, so you'll need to offset the counts appropriately.
Upvotes: 1