Question

I have written a function in R which reads a text file and generates a color map using the coordinate information and intensity value for each co-ordiante within that file.

Now, I want to apply a specific threshold value for the intensities such that the intensity values below the specific threshold are suppressed (or are in black) and the intensities above the threshold are represented in the defined color scheme. I basically want to high light areas on the image with high intensity.

I also have another question in this context: Rather than specific values for threshold, can I define a percent? Because, for every image, I have different intensity values and hence different thresholds will have to be applied.

I have attached the image I am working with. enter image description here

Here is a sample of the data with three columns (xCoordinate, yCoordinate, intensity values)

0 0 4.169080e+000
1 0 6.391669e+000
2 0 6.391669e+000
.
.
.
511 0 9.922547e+000
0 1 5.268026e+000
1 1 5.268026e+000
.
.
.
511 511 9.922547e+000

And, to generate the plot, I read all data from the file and pass it to a function which generates the color map (spplot). Here is the function:

generatePlot <- function (data, plotName) {
  rv <- raster(nrows=data$xPixels, ncols=data$xPixels)
  rv1 <- raster(matrix(data$rawData$V3, nrow = data$xPixels, byrow = T), xmn=0, ymn=0,     xmx=1, ymx=1)
plotData <- spplot(rv1, scales = list(draw = TRUE), col.regions=rainbow(100, start = 1/6, end = 1), main=plotName)

Here is the original image: enter image description here

Here is the thresholded image: enter image description here

Was it helpful?

Solution

Key is to assign NA to all values below the threshold:

library(sp)
library(raster)

# create data
x = 1:511
y = 1:511
z = runif(n=511*511, min=0, max=10)

# turn into matrix
m = matrix(z, nrow=length(x), ncol=length(y))

# your function
generatePlot <- function (data, plotName) {

  # which values are below 0.5*max?
  below = which(data<0.5*max(data,na.rm=T))

  # change these values to NA
  data[below] <- NA

  # make a raster of the matrix
  rv1 <- raster(data)

  # create the plot
  plotData <- spplot(rv1, scales = list(draw = TRUE), col.regions=rainbow(100, start = 1/6, end = 1), main=plotName)

  return(plotData)
}

plotData = generatePlot(m,'nice plot')
print(plotData)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top