-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDenoising Dirt Image.R
59 lines (52 loc) · 1.48 KB
/
Denoising Dirt Image.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
# libraries
library("pacman")
pacman::p_load(png, raster, data.table, reshape)
dirtyFolder = "../input/train"
cleanFolder = "../input/train_cleaned"
dat = NULL
filenames = list.files(dirtyFolder)
for (f in filenames)
{
print(f)
imgX = readPNG(file.path(dirtyFolder, f))
imgY = readPNG(file.path(cleanFolder, f))
# turn the images into vectors
x = matrix(imgX, nrow(imgX) * ncol(imgX), 1)
y = matrix(imgY, nrow(imgY) * ncol(imgY), 1)
if (f == filenames[1])
{
dat = cbind(y, x)
names(dat) = NULL
} else
{
dat = rbind(cbind(y, x))
}
}
dat = data.frame(dat)
names(dat) = c("y", "x")
lm.mod.1 = lm(y ~ x, data = dat[y > 0.05 & y < 0.95,])
dirtyFolder = "../input/test"
filenames = list.files(dirtyFolder)
for (f in filenames)
{
print(f)
imgX = readPNG(file.path(dirtyFolder, f))
x = matrix(imgX, nrow(imgX) * ncol(imgX), 1)
y = coef(lm.mod.1)[1] + coef(lm.mod.1)[2] * x
y[y < 0] = 0
y[y > 1] = 1
img = matrix(y, nrow(imgX), ncol(imgX))
img.dt=data.table(melt(img))
names.dt<-names(img.dt)
setnames(img.dt,names.dt[1],"X1")
setnames(img.dt,names.dt[2],"X2")
Numfile = gsub(".png", "", f, fixed=TRUE)
img.dt[,id:=paste(Numfile,X1,X2,sep="_")]
write.table(img.dt[,c("id","value"),with=FALSE], file = "submission.csv", sep = ",", col.names = (f == filenames[1]),row.names = FALSE,quote = FALSE,append=(f != filenames[1]))
# show a sample
if (f == "4.png")
{
writePNG(imgX, "train_101.png")
writePNG(img, "train_cleaned_101.png")
}
}