functions.R 7.16 KB
Newer Older
luroth's avatar
luroth committed
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
calc_sensor_ratio <- function(sensor_size_x, sensor_size_y) {
  ratio <- sensor_size_x / sensor_size_y
  return(ratio)
}


calc_lens_angle_of_view <- function(sensor_size, lens_focal_length) {
  angle_of_view <- 2 * atan(sensor_size / (2 * lens_focal_length))
  return(angle_of_view)
}

calc_IFOV <- function(flight_height, size, sensor_recorded_pixels, focal_length) {
  return((size/sensor_recorded_pixels) * flight_height / focal_length)
}

calc_IFOV_mm <- function(flight_height, size, sensor_recorded_pixels, focal_length) {
  return(1000 *calc_IFOV(flight_height, size, sensor_recorded_pixels, focal_length))
}

calc_FOV <- function(flight_height, size, focal_length) {
  return((size) * flight_height / focal_length)
}

calc_flight_height <- function(ifov, size, sensor_recorded_pixels, focal_length) {
  return(ifov / (size/sensor_recorded_pixels) *focal_length)
}

28
29
30
31
calc_flight_height_mm <- function(ifov_mm, size, sensor_recorded_pixels, focal_length) {
  return((ifov_mm/1000) / (size/sensor_recorded_pixels) *focal_length)
}

luroth's avatar
luroth committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
calc_hyperfocal_distance <- function(focal_length, aperture, circle_of_confusion) {
  return((focal_length^2)/(aperture*circle_of_confusion) + focal_length)
}

calc_focal_dist <- function(focal_length, aperture, circle_of_confusion, flight_height) {
  hyperfocal_distance <- calc_hyperfocal_distance(focal_length, aperture, circle_of_confusion)
  
  focus_dist <- (hyperfocal_distance * sqrt(flight_height))/(sqrt(hyperfocal_distance + flight_height))
  
  focus_dist <- (-hyperfocal_distance^2 + sqrt(hyperfocal_distance^4 + 4 * hyperfocal_distance^2 * flight_height^2))/(2 * flight_height)
  
  focus_dist <- ifelse(focus_dist > hyperfocal_distance, hyperfocal_distance, focus_dist)
  return(focus_dist)
}

calc_depth_of_field_near <- function(focal_length, aperture, circle_of_confusion, object_distance) {
  hyperfocal_distance <- calc_hyperfocal_distance(focal_length, aperture, circle_of_confusion)
  return(object_distance / ( (object_distance - focal_length) / (hyperfocal_distance - focal_length) + 1))
}

calc_depth_of_field_far <- function(focal_length, aperture, circle_of_confusion, object_distance) {
  hyperfocal_distance <- calc_hyperfocal_distance(focal_length, aperture, circle_of_confusion)
54
  return(ifelse(object_distance <= hyperfocal_distance, (object_distance / ( (focal_length - object_distance) / (hyperfocal_distance - focal_length) + 1)), Inf))
luroth's avatar
luroth committed
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
}

calc_exposure_value <- function(aperture, shutter_speed, iso=100) {
  return(log2((100/iso)*(aperture^2)/(1/shutter_speed)))
}

calc_aperture <- function(exposure_value, shutter_speed, iso=100) {
  return((1/10) * sqrt(iso) * sqrt(1/shutter_speed) * sqrt(2^exposure_value))
}

calc_shutter_speed <- function(exposure_value, aperture, iso=100) {
  return(1/((25 * aperture^2 * 2^(2-exposure_value))/(iso)))
}

calc_iso <- function(exposure_value, aperture, shutter_speed) {
  return((25 * aperture^2 * 2^(2-exposure_value))/(1/shutter_speed))
}

calc_flight_speed <- function(IFOV, blur, shutter_speed) {
  return(((IFOV)*blur) / (1/shutter_speed))
}

calc_distance_exposure_station <- function(end_lap, FOV_x) {
  return(FOV_x - ((end_lap/100) * FOV_x))
}

calc_spacing_flight_lines <- function(side_lap, FOV_y) {
  return(FOV_y - (side_lap/100) * FOV_y)
}

calc_percent_exposure_station <- function(end_lap_size, FOV_x) {
  return(100 - (end_lap_size/ FOV_x)*100)
}

calc_percent_flight_lines <- function(side_lap_size, FOV_y) {
  return(100 - (side_lap_size/ FOV_y)*100)
}

calc_image_recording_speed <- function(flight_speed, distance_exposure_stations) {
  return(flight_speed / distance_exposure_stations)
}

calc_flight_duration <- function(distance_area_x, FOV_x, distance_area_y, FOV_y, spacing_flight_lines, flight_speed) {
  distance <- 1 * (distance_area_x + ceiling(FOV_x / (2 * spacing_flight_lines))) * 2 * spacing_flight_lines + (distance_area_x/spacing_flight_lines) * (distance_area_y+ FOV_y)
  duration <- (distance / flight_speed) / 60
  return(duration)
}

calc_min_flight_height_coded_gcp <- function(diameter_coded, size_x, sensor_recorded_pixels_x, focal_length) {
  ifov <- diameter_coded / 80
  height <- calc_flight_height(ifov, size_x, sensor_recorded_pixels_x, focal_length)
  return(height)
}

calc_max_flight_height_coded_gcp <- function(diameter_coded, size_x, sensor_recorded_pixels_x, focal_length) {
  ifov <- diameter_coded / 15
  height <- calc_flight_height(ifov, size_x, sensor_recorded_pixels_x, focal_length)
  return(height)
}

calc_min_flight_height_noncoded_gcp <- function(diameter_coded, size_x, sensor_recorded_pixels_x, focal_length) {
  ifov <- diameter_coded / 110
  height <- calc_flight_height(ifov, size_x, sensor_recorded_pixels_x, focal_length)
  return(height)
}

calc_max_flight_height_noncoded_gcp <- function(diameter_coded, size_x, sensor_recorded_pixels_x, focal_length) {
  ifov <- diameter_coded / 24
  height <- calc_flight_height(ifov, size_x, sensor_recorded_pixels_x, focal_length)
  return(height)
}

calc_pixel_freq <- function(plot_size, spacing, no_of_lanes, position_precision_sd, sensor_res, gsd, use_uniform=FALSE) {
  
  pixels <- 1:sensor_res
  # Positions of plots, pixel position as index, TRUE as value if plot present, else FALSE
  plot_positions <- ifelse(seq(1, sensor_res + no_of_lanes*spacing/gsd) %% round(plot_size/gsd) + round(plot_size/(gsd*2))  == round(plot_size/(gsd)), TRUE, FALSE)
  
  # Get camera views on plots
  sensor_views <- rollapply(data=plot_positions, width=sensor_res, by=round(spacing/gsd), FUN = function(x) x)
  
  # Calculate frequency of plots at specific sensor position
  sensor_position_freq <- apply(sensor_views, MARGIN=c(2), sum)
  # Normalize
  sensor_position_freq <- sensor_position_freq / sum(sensor_position_freq)
  
  # Normal distribution for each sensor pixel
  if(!use_uniform) {
    pixel_distribution <- mapply(pixels, FUN=function(z) {dnorm(x=pixels, sd=position_precision_sd/gsd, mean=z)})
  } else {
    pixel_distribution <- mapply(pixels, FUN=function(z) {dunif(x=pixels, min=z-(position_precision_sd/gsd), max=z+(position_precision_sd/gsd))})
  }
  # Sweep over columns and multiply with occurance frequency of sensor at this position
  pixel_distribution_sens <- sweep(pixel_distribution, MARGIN=2, sensor_position_freq, '*')
  # Sum up result
  pixel_frequency <- rowSums(pixel_distribution_sens)
  # Normalize
  pixel_frequency <- pixel_frequency / sum(pixel_frequency)

  pixel_frequency_data <- data.frame(x=1:sensor_res, freq=pixel_frequency)
  
  return(pixel_frequency_data)
}

calc_pixel_freq_xy <- function(plot_size_x, plot_size_y, spacing_x, spacing_y, no_of_lanes_x, no_of_lanes_y, sensor_res_x, sensor_res_y, position_precision_sd, gsd) {
  
  pixel_frequency_data_x <- calc_pixel_freq(plot_size_x, spacing_x, no_of_lanes_x, position_precision_sd, sensor_res_x, gsd)
  pixel_frequency_data_y <- calc_pixel_freq(plot_size_y, spacing_y, no_of_lanes_y, position_precision_sd, sensor_res_y, gsd, use_uniform = TRUE)
  
  pixel_frequency_data_xy <- unlist(pixel_frequency_data_y[,'freq']) %o% unlist(pixel_frequency_data_x[,'freq'])
  pixel_frequency_data_xy <- (pixel_frequency_data_xy / max(pixel_frequency_data_xy))
  return(list(pixel_frequency_data_xy, pixel_frequency_data_x, pixel_frequency_data_y))
}


long2UTM <- function(long) {
  (floor((long + 180)/6) %% 60) + 1
}