-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrast.R
More file actions
106 lines (97 loc) · 2.94 KB
/
contrast.R
File metadata and controls
106 lines (97 loc) · 2.94 KB
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
#' @include utils.R
NULL
#' Image contrast
#'
#' \code{img_contrast} returns the RMS contrast of an image \code{img}. A higher
#' value indicates higher contrast.
#'
#' @details The function returns the RMS contrast of an image \code{img}. The
#' RMS contrast is defined as the standard deviation of the normalized pixel
#' intensity values. A higher value indicates higher contrast. The image is
#' automatically normalized if necessary (i.e., normalization into range [0,
#' 1]).
#'
#' For color images, the weighted average between each color channel's values
#' is computed.
#'
#'
#' @param img An image in form of a matrix or array of numeric values. Use e.g.
#' \code{\link{img_read}()} to read an image file into \code{R}.
#'
#' @return a numeric value (RMS contrast)
#' @export
#'
#' @examples
#' # Example image with relatively high contrast: berries
#' berries <- img_read(system.file("example_images", "berries.jpg", package = "imagefluency"))
#' #
#' # display image
#' grid::grid.raster(berries)
#' #
#' # get contrast
#' img_contrast(berries)
#'
#' # Example image with relatively low contrast: bike
#' bike <- img_read(system.file("example_images", "bike.jpg", package = "imagefluency"))
#' #
#' # display image
#' grid::grid.raster(bike)
#' #
#' # get contrast
#' img_contrast(bike)
#'
#' @references Peli, E. (1990). Contrast in complex images. \emph{Journal of the
#' Optical Society of America A}, \emph{7}, 2032--2040.
#' \doi{10.1364/JOSAA.7.002032}
#'
#'
#' @seealso \code{\link{img_read}}, \code{\link{img_complexity}},
#' \code{\link{img_self_similarity}}, \code{\link{img_simplicity}},
#' \code{\link{img_symmetry}}, \code{\link{img_typicality}},
#'
#'
img_contrast <- function(img){
# check input
imgtype <- .check_input(img, f_call = "contrast")
# compute and return contrast
if (imgtype == "rgb") {
# split image into channels
redChannel <- img[, , 1]
greenChannel <- img[, , 2]
blueChannel <- img[, , 3]
#
out <- 0.2989 * .contr(redChannel) + 0.5870 * .contr(greenChannel) + 0.1140 * .contr(blueChannel)
return(out)
#
} else return(.contr(img))
}
#' .contr
#'
#' Returns the RMS contrast of an image matrix.
#'
#' @param img A matrix of numeric values or integer values.
#'
#' @return a numeric value (RMS contrast)
#' @keywords internal
.contr <- function(img) {
## -----------------------
## rms contrast
## -----------------------
# vectorize matrix
pixAll <- as.vector(img)
# check range of input values
if (min(pixAll) < 0) {
warning("Negative pixel intensity values in your image. Corresponding pixels set to 0.", call. = FALSE)
pixAll[pixAll < 0] <- 0
}
if (max(pixAll) > 255) {
warning("Pixel intensity values > 255 in your image. Corresponding pixels set to 255.", call. = FALSE)
pixAll[pixAll > 255] <- 255
}
# normalize image if necessary
if (max(pixAll) > 1) {
pixAll <- pixAll / 255
}
# RMS via built-in sd function
return(stats::sd(pixAll))
}