dahtah / imager

R package for image processing
GNU Lesser General Public License v3.0
187 stars 43 forks source link

can't save image when NAN value appears. #117

Closed GreenEric closed 4 years ago

GreenEric commented 4 years ago

when manipulating images with {imager}, I subtract an image from the other, and shockedly found I can't save the image. After converting the image into data.frame, I conclude when NAN appears, the it can't be saved. pls find the toy example.

How can I save such image?

library(imager)
im1 <- load.image("imx1.jpg")
im2 <- load.image("imx1.jpg") %>% isoblur(5)

imx <- (im1-im2)^0.5

plot(imx)
# class(imx)
#[1] "cimg"         "imager_array" "numeric"     

save.image(imx, "imxxx.jpg") 
# Error in if (any(A > 1) | any(A < 0)) { : 
# missing value where TRUE/FALSE needed

imxdf <- as.data.frame(imx)
#> imxdf
#     x y cc       value
#1    1 1  1         NaN
#2    2 1  1         NaN
#3    3 1  1         NaN
#4    4 1  1         NaN

imx1

ShotaOchi commented 4 years ago

You can save such image as tiff file.

save.image(imx, "imxxx.tif")

However, I don't recommend to save such image because image viewers don't read such image in general.

The NaN appeared because square root of negative numbers is impossible and treated as NaN in R. I guess you wanted to estimate an error between two images. If so, you should use mean absolute error or root mean squared error or something.