/
imageOneChannel.R
129 lines (117 loc) · 2.62 KB
/
imageOneChannel.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#' Title
#'
#' @slot image matrix.
#'
#' @return
#' @export
#'
#' @examples
imageOneChannel <- setClass("imageOneChannel", slots=list(imageMatrix="matrix"))
#' Is It A One Channel Image
#'
#' @description Checks if an object is of type \code{\link[Raspository]{imageOneChannel}}.
#'
#' @export
#'
#' @param x any R object.
#'
#' @return \code{TRUE}, if the object is of type \code{\link[Raspository]{imageOneChannel}}.
#' \code{FALSE} otherwise.
#'
#'
#' @examples
is.imageOneChannel <-function(x){
return(inherits(x, "imageOneChannel"))
}
#' Title
#'
#' @param pathToJpeg
#'
#' @return
#' @export
#' @importFrom jpeg readJPEG
#' @importFrom methods new
#'
#' @examples
imageOneChannelFromJpeg <-function(pathToJpeg){
image<- readJPEG(pathToJpeg)
imageOneChannel <- image[,,1]
return(new("imageOneChannel", imageMatrix = imageOneChannel))
}
#' Title
#'
#' @param object
#'
#' @return
#' @export
#' @importFrom grDevices as.raster
#' @importFrom graphics plot
#' @importFrom methods slot
#'
#' @examples
plot.imageOneChannel <- function(object){
plot(as.raster(object@imageMatrix))
}
#' Title
#'
#' @importFrom jpeg writeJPEG
#'
#' @param target
#' @param object
#'
#' @return
#' @export
#'
#' @examples
writeJPEG.imageOneChannel <- function(object, target){
writeJPEG(image = object@imageMatrix, target = target)
}
#' Title
#'
#' @importFrom png writePNG
#'
#' @param target
#' @param object
#'
#' @return
#' @export
#'
#' @examples
writePNG.imageOneChannel <- function(object, target){
writePNG(image = object@imageMatrix, target = target)
}
#' Title
#'
#' @export
#'
#' @import data.table
#'
#' @param img
#' @param log.scale
#'
#' @references \insertRef{Weickert2019}{Raspository}
#'
#' @return
#'
#'
#' @examples
calculateFourierSpectrum <- function(img, log.scale = TRUE, shift = TRUE){
if(shift){
## generate checkerboard sign imageMatrix to shift it in the fourier domain
DT <- CJ(1:nrow(img@imageMatrix), 1:ncol(img@imageMatrix))
DT[, sign := (-1)^(V1 + V2)]
shiftMatrix <- new("imageOneChannel", imageMatrix = as.matrix(dcast(DT, V1 ~ V2, value.var = "sign")[,-1]))
shiftedImg <- img * shiftMatrix
imgFTrans <- fft(shiftedImg@imageMatrix)
}else{
imgFTrans <- fft(img@imageMatrix)
}
## calculate the eiclidean norm
fourierSpectrum <- new("imageOneChannel",
imageMatrix = sqrt(Re(imgFTrans)^2 + Im(imgFTrans)^2))
## perform logarithmic rescaling
if(log.scale){
fourierSpectrum <- logarithmicDynamicCompression(fourierSpectrum)
}
return(fourierSpectrum)
}