-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboxBlur.R
executable file
·118 lines (108 loc) · 3.98 KB
/
boxBlur.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
# Last night you partied a little too hard. Now there's a black and white photo of you that's about to go viral! You can't let this ruin your reputation, so you want to apply the box blur algorithm to the photo to hide its content.
#
# The pixels in the input image are represented as integers. The algorithm distorts the input image in the following way: Every pixel x in the output image has a value equal to the average value of the pixel values from the 3 × 3 square that has its center at x, including x itself. All the pixels on the border of x are then removed.
#
# Return the blurred image as an integer, with the fractions rounded down.
#
# Example
#
# For
#
# image = [[1, 1, 1],
# [1, 7, 1],
# [1, 1, 1]]
#
# the output should be boxBlur(image) = [[1]].
#
# To get the value of the middle pixel in the input 3 × 3 square: (1 + 1 + 1 + 1 + 7 + 1 + 1 + 1 + 1) = 15 / 9 = 1.66666 = 1. The border pixels are cropped from the final result.
#
# For
#
# image = [[7, 4, 0, 1],
# [5, 6, 2, 2],
# [6, 10, 7, 8],
# [1, 4, 2, 0]]
#
# the output should be
#
# boxBlur(image) = [[5, 4],
# [4, 4]]
#
# There are four 3 × 3 squares in the input image, so there should be four integers in the blurred output. To get the first value: (7 + 4 + 0 + 5 + 6 + 2 + 6 + 10 + 7) = 47 / 9 = 5.2222 = 5. The other three integers are obtained the same way, then the surrounding integers are cropped from the final result.
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] array.array.integer image
#
# An image, stored as a rectangular matrix of non-negative integers.
#
# Guaranteed constraints:
# 3 ≤ image.length ≤ 100,
# 3 ≤ image[0].length ≤ 100,
# 0 ≤ image[i][j] ≤ 255.
#
# [output] array.array.integer
# A blurred image represented as integers, obtained through the process in the description.
#notes: took me the longest to resolve this. some 2.5hours.
library(data.table)
# image <- list(list(7, 4, 0, 1),
# list(5, 6, 2, 2),
# list(6, 10, 7, 8),
# list(1, 4, 2, 0))
#
# image = list(list(1, 1, 1),
# list(1, 7, 1),
# list(1, 1, 1))
# image <- list(list(36,0,18,9),
# list(27,54,9,0),
# list(81,63,72,45))
boxBlur <- function(image) {
#setDT(image) was the issue. :(
image <- matrix(unlist(image),nrow = length(image),byrow=T)
outputImage <- matrix(nrow = (nrow(image) - 2),ncol = (ncol(image) - 2))
for (rowIndex in 2:(nrow(image) - 1)) {
for (colIndex in 2:(ncol(image) - 1)) {
outputImage[rowIndex-1,colIndex-1] <- floor(mean(unlist(image[(rowIndex-1):(rowIndex+1),(colIndex-1):(colIndex+1)])))
}
}
#convert into list of list.
#every row is a list
#then make list of each of the rows
outputImage <- apply(outputImage,2,as.list)
return(outputImage)
}
# #this worked but failed last 2 tests for speed.
# boxBlur <- function(image) {
# # initial <- image
# setDT(image)
# outputImage <- matrix(nrow = (nrow(image) - 2),ncol = (ncol(image) - 2))
#
#
# for (rowIndex in 2:(nrow(image) - 1)) {
# for (colIndex in 2:(ncol(image) - 1)) {
# outputImage[rowIndex-1,colIndex-1] <- floor(mean(unlist(image[(rowIndex-1):(rowIndex+1),(colIndex-1):(colIndex+1)])))
# }
# }
# #convert into list of list.
# #every row is a list
# #then make list of each of the rows
# outputImage <- apply(outputImage,2,as.list)
# return(outputImage)
# }
#this solution has some type issues. Same codesignal array to R (list of list issues)
# boxBlur <- function(image) {
# # initial <- image
# setDT(image)
# outputImage <- matrix(nrow = (nrow(image) - 2),ncol = (ncol(image) - 2))
# for (rowIndex in 2:(nrow(image) - 1)) {
# for (colIndex in 2:(ncol(image) - 1)) {
# outputImage[rowIndex-1,colIndex-1] <- floor(mean(unlist(image[(rowIndex-1):(rowIndex+1),(colIndex-1):(colIndex+1)])))
# }
# }
# #convert into list of list.
# #every row is a list
#
# return(as.list(unname(as.data.table(t(outputImage)))))
# }