-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_color.R
151 lines (131 loc) · 4.34 KB
/
find_color.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env Rscript
# -*- encoding: utf-8 -*-
# ./find_color.R
#
# (c) 2010 Konstantin Sering, Nora Umbach, Dominik Wabersich
# <colorlab[at]psycho.uni-tuebingen.de>
#
# GPL 3.0+ or (cc) by-sa (http://creativecommons.org/licenses/by-sa/3.0/)
#
# Find voltages that adjust tubes so that xyY for tubes has a minimum
# distance from monitor xyY; "brute force method" to find correct calibration.
#
# content: (1) read data for tubes and monitor
# (2) find best fit
# (3) diagnostic plots
#
# input: measure_tubes_20120210_2151.txt
# depth_monitor20120210_1949.txt
# output: --
#
# created
# last mod 2012-16-02, NU
###### (1) read data for tubes and monitor ######
tub0 <- read.table("measure_tubes_20120210_2151.txt", header=T, sep=",")
# Careful! This thing is huge!
tub <- aggregate(as.matrix(tub0) ~ volR + volG + volB, tub0, mean)
for (i in 1:3) tub[,4] <- NULL
names(tub) <- c("volR","volG","volB","x","y","Y",paste("l", 1:36, sep=""))
mon0 <- read.table("depth_monitor20120210_1949.txt", sep=",")
names(mon0) <- c("R","G","B","x","y","Y",paste("l", 1:36, sep=""))
mon <- aggregate(as.matrix(mon0) ~ R + G + B, mon0, mean)
for (i in 1:3) mon[,4] <- NULL
names(mon) <- c("R","G","B","x","y","Y",paste("l", 1:36, sep=""))
###### (2) find best fit ######
res_x <- NULL
for (i in 1:10){
x <- tub[which.min(abs(tub$x - mon$x[i])),]
res_x <- rbind(res_x, x)
gc() # free all memory R doesn't need anymore
}
res_y <- NULL
for (i in 1:10){
y <- tub[which.min(abs(tub$y - mon$y[i])),]
res_y <- rbind(res_y, y)
gc() # free all memory R doesn't need anymore
}
res_Y <- NULL
for (i in 1:10){
Y <- tub[which.min(abs(tub$Y - mon$Y[i])),]
res_Y <- rbind(res_Y, Y)
gc() # free all memory R doesn't need anymore
}
## first for x and y coordinates
for (i in 1:10){
tub[paste("r", i, sep="")] <- sqrt((tub$x - mon$x[i])^2 + (tub$y -
mon$y[i])^2)
}
epsilon <- 0.0035
## then for Y
reslist <- NULL
for (i in 1:10){
tmp <- tub[tub[paste("r", i, sep="")] < epsilon,]
print(paste("tmp dim for grey", i, "is", dim(tmp)))
reslist <- rbind(reslist, tmp[which.min(abs(tmp$Y - mon$Y[i])),])
}
###### (3) diagnostic plots ######
# Did we measure the correct area?
par(mfrow=c(1,2))
# diagnostic plot xy
plot(tub$x, tub$y, pch=".", xlim=c(0.296, 0.306))
points(mon$x, mon$y, pch="x", col="red")
# and Y
plot(jitter(rep(1, length(tub$Y))), tub$Y, pch=".", ylim=c(19,21.5))
abline(h=mon$Y, col="red")
# How does the color influence direction?
par(mfrow=c(1,3))
# red
plot(tub$x, tub$y, type="n", xlim=c(0.296, 0.306))
points(mon$x, mon$y, pch="x", col="red")
lcolor <- colorRampPalette(c("black","red"))(length(unique(tub$volR)))
for (i in 1:length(unique(tub$volR))) {
tmp <- tub[tub$volR==unique(tub$volR)[i],]
points(tmp$x, tmp$y, col=lcolor[i], pch=".")
}
# green
plot(tub$x, tub$y, type="n", xlim=c(0.296, 0.306))
points(mon$x, mon$y, pch="x", col="red")
lcolor <- colorRampPalette(c("black","green"))(length(unique(tub$volG)))
for (i in 1:length(unique(tub$volG))) {
tmp <- tub[tub$volG==unique(tub$volG)[i],]
points(tmp$x, tmp$y, col=lcolor[i], pch=".")
}
# blue
plot(tub$x, tub$y, type="n", xlim=c(0.296, 0.306))
points(mon$x, mon$y, pch="x", col="red")
lcolor <- colorRampPalette(c("black","blue"))(length(unique(tub$volB)))
for (i in 1:length(unique(tub$volB))) {
tmp <- tub[tub$volB==unique(tub$volB)[i],]
points(tmp$x, tmp$y, col=lcolor[i], pch=".")
}
# Look at deviance of our measurements:
# ellipse function
ell <- function(t, xc=0, yc=0, a=1, b=1/5, phi=0){
phi <- ifelse(phi < pi/2, phi, pi - phi)
cbind(x = xc + a*cos(t)*cos(phi) - b*sin(t)*sin(phi),
y = yc + a*cos(t)*sin(phi) + b*sin(t)*cos(phi))
}
steps <- 1
tub.tmp <- tub
tub <- tub[tub$volR==1270 & tub$volG==1670,]
tub <- tub[order(tub$volB),]
print(dim(tub))
idx <- seq(1,46,steps)
plot(tub$x, tub$y, type="n")
lcolor <- colorRampPalette(c("green","red"))(length(idx))
for (j in 1:length(idx)) {
i <- idx[j]
points(y ~ x, data=tub[i,], pch="x", col=lcolor[j])
volR <- tub$volR[i]
volG <- tub$volG[i]
volB <- tub$volB[i]
tmp <- tub0[tub0$volR==volR & tub0$volG==volG & tub0$volB==volB,]
points(y ~ x, data=tmp, col=lcolor[j])
xsd <- sd(tmp$x)
ysd <- sd(tmp$y)
lines(ell(seq(0,2*pi,length.out=100), tub$x[i], tub$y[i], xsd, ysd),
col=lcolor[j])
}
lines(tub$x, tub$y)
tub <- tub.tmp
rm(tub.tmp)