dahtah / imager

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

Threshold function #97

Open Balamur7 opened 5 years ago

Balamur7 commented 5 years ago

First of all, very nice package! Regeratablly I din't find other contact details, so I have to use the forum.

Is it possible to see the value of the threshold when using the threshold function? Normally the threshold is only applied to the image, without any information which threshold is choosen.

More over, is there any possibility to see the code of threshold function?

Best wishes and thanks in advance, Norman

ShotaOchi commented 5 years ago

You can get the threshold value. The example code is shown below.

library(imager)
im <- grayscale(boats) # im is an image
b <- threshold(im)
thr <- max(im * (1 - b)) # thr is the threshold value

You can see the code of threshold function. https://github.com/dahtah/imager/blob/b8f3724f024bb9af993bd01f37e0080d0f35ecf0/R/utils.R#L418-L453

Balamur7 commented 5 years ago

Dear ShotaOchi,

thanks for your fast and good reply.

Regrettably this only works with classical images and not for raster images. The main reason for using the imager package was that it also can handle raster images.

Applying your code to a raster resulting in this error:

thr <- max(Raster * (1 - b)) Warning message: In max(new("RasterLayer", file = new(".RasterFile", name = "", datanotation = "FLT4S", : Nothing to summarize if you provide a single RasterLayer; see cellStats

ShotaOchi commented 5 years ago

Just coerce raster objects into cimg objects.

ras <- raster(as.matrix(im)) # ras is a raster object
b <- threshold(ras)
thr <- max(as.cimg(ras) * (1 - as.cimg(b))) # thr is the threshold value
Balamur7 commented 5 years ago

Hey ShotaOchi,

Sorry for border you again, but there are still problems in this code.

First, when you need the adjust function for the threshold code, you will not get this value from the code above.

Moreover, the thr value (thr) is all the time 0 by applying your code, independent from the input.

Thanks in advance for your kind support, Norman

ShotaOchi commented 5 years ago

I don't understand what is the adjust function for the threshold code.

The above code worked fine on my machine when I input a raster object shown below.

ras1 <- raster(as.matrix(grayscale(boats)))

The above code didn't work on my machine when I input a raster object shown below. threshold(ras2) returned the error message 'Error in storage.mode(x) <- "double" :'.

ras2 <- raster(matrix(1:400,20,20))

I can't reproduce your problem.

Moreover, the thr value (thr) is all the time 0 by applying your code, independent from the input.

What raster object did you input?

dahtah commented 5 years ago

The adjust argument works similarly to the adjust argument in R's density function: it adjusts the automatic threshold up or down, i.e. if the auto threshold is 3.1 and adj=2 the threshold will actually be 6.2. It's just for convenience.

Balamur7 commented 5 years ago

Yes, the first part is completly correct and I also know.

So if understand your post correctly, you have to multiply the threshold value with the adjust value? as Example:

Adjust= 0.5 thr * 0.5

adjust = 1.5 thr * 1.5

ShotaOchi commented 5 years ago

thr in the above code is same as below. https://github.com/dahtah/imager/blob/b8f3724f024bb9af993bd01f37e0080d0f35ecf0/R/utils.R#L433 So, We don't have to myltiply the threshold value with the adjust value.