Skip to content

Instantly share code, notes, and snippets.

Avatar

Oscar de Lama oscardelama

View GitHub Profile
@oscardelama
oscardelama / xval-noise-models-p3.r
Last active Aug 29, 2015
xval-noise-models: Test the models
View xval-noise-models-p3.r
# Get the weighted errors `w.mean` and `w.var`
test.error.weighted.var <- function(sel.channel, coeffs) {
tests.pics <- read.csv('test-pics.csv', stringsAsFactors = FALSE, row.names=NULL)
tests.pics <- as.vector(tests.pics[,1])
data.df <- subset(data.frame(vvm.all$var.df),
channel == sel.channel &
pict %in% tests.pics)
predicted.var <- (data.df$mean*coeffs[3] + coeffs[2])*data.df$mean + coeffs[1]
error.var <- mean((data.df$var - predicted.var)^2/predicted.var)
@oscardelama
oscardelama / xval-noise-models-p4.r
Last active Aug 29, 2015
xval-noise-models: The main routine
View xval-noise-models-p4.r
execute.tests <- function(n) {
# A place-holder for the test results
result.df <- data.frame()
# Initial values for snr model optimization
param.init <- c(2, 0.4, 5e-06)
# Iterate n times
for (n in 1:n) {
split.data()
# Model each channel per iteration
@oscardelama
oscardelama / xval-noise-models-p1.r
Last active Aug 29, 2015
xval-noise-models: Cross validation of "var ~ mean + mean^2" models testing a SNR optimization model versus the regular VVM model.
View xval-noise-models-p1.r
library(imgnoiser)
library(dplyr)
library(robustbase)
# Acquire the data
vvm.all <- vvm$new(has.RGGB.pattern = TRUE)
vvm.all$digest(
file.name.from = '_ODL0387s4',
file.name.to = '_ODL1671s6',
file.name.ext = '.pgm',
@oscardelama
oscardelama / xval-noise-models-p2.r
Last active Aug 29, 2015
xval-noise-models: Fit the models.
View xval-noise-models-p2.r
# Compute the SNR.
# Returns "high" values when the model variance is negative to
# hint the optimizer to get out of there.
snr <- function(mean, var) {
negatives <- which(var < 0)
result <- 20*log10(mean/sqrt(abs(var)))
result[negatives] <- result[negatives]*8
result;
}
@oscardelama
oscardelama / loss-of-snr.r
Last active Aug 29, 2015
Compute the loss of SNR we converting a raw image to a RGB color space.
View loss-of-snr.r
avg.loss.of.snr <- function(color.conv.matrix, white.bal.scales) {
sqr.conv.mtx <- color.conv.matrix^2
wb.conv.mtx <- t(apply(sqr.conv.mtx, 1, function(x) x*white.bal.scales ))
mc <- apply(wb.conv.mtx, 1, sum)
loss <- mean(log10(mc)) - mean(log(white.bal.scales) )
# Result
10*loss;
}
# Helper function to build the color matrix
matrix_3x3 <- function(x) matrix(x, nrow=3L, ncol=3L, byrow=TRUE);
@oscardelama
oscardelama / rgb-noise:process-LR-samples.r
Last active Aug 29, 2015
rgb-noise: Process Lightroom samples, with zero settings
View rgb-noise:process-LR-samples.r
# Read the TIFF files
vvm.tif <- vvm$new(has.RGGB.pattern = TRUE)
vvm.tif$digest.from.rgb(
file.name.from = '_ODL0415',
file.name.to = '_ODL1668',
file.path = 'ISO100/Selection/LR-zero'
)
# VVM plot
vvm.tif$plot(tlab = "VVM: sRGB images from Lightroom",
slab = 'With zero settings',
@oscardelama
oscardelama / gist:7f0093f8c5e9176b9ec7
Last active Aug 29, 2015
rgb-noise: Draw SNR quality reference limits for 18% gray
View gist:7f0093f8c5e9176b9ec7
# Draw SNR quality reference limits for 18% gray
add.snr.ref.limits <- function(p) {
p +
geom_hline(aes(yintercept=20), colour='red', linetype = 6, alpha = 0.4) +
geom_hline(aes(yintercept=32), colour='cyan', linetype = 6, alpha = 0.8) +
geom_hline(aes(yintercept=38), colour='green', linetype = 6, alpha = 0.8)
}
@oscardelama
oscardelama / gist:d85a5fe11db274aa1103
Last active Aug 29, 2015
rgb-noise: Condense the three raw noise models in one
View gist:d85a5fe11db274aa1103
library(imgnoiser)
library(ggplot2)
# Y coordinate of sRGB primaries. From http://en.wikipedia.org/wiki/SRGB
weights.sRGB <- c(0.2126, 0.7152, 0.0722)
# D65 white point in XYZ. From ASTM E308-01 in http://www.brucelindbloom.com/
D65.XYZ <- c(0.95047, 1.00000, 1.08883)
# With "D65 color matrix" in the camera color data, we can convert
# the D65 XYZ white point to the camera raw color space:
D65.raw <- nikon.d7000.ISO100.colmap$cam.matrices.2$color.matrix %*% D65.XYZ
#> [,1]
@oscardelama
oscardelama / gist:bd2f949fd9295ca2a9b5
Created Feb 6, 2015
rgb-noise: sRGB linear - Initial analysis
View gist:bd2f949fd9295ca2a9b5
vvm.rgb <- convert.to.rgb(target.space = 'sRGB', use.camera.tc = FALSE, target.tc = 'linear')
x11()
vvm.rgb$plot(tlab = "VVM Selected samples in linear sRGB",
slab = 'Without camera tone curve')
vvm.rgb$plot(x = log2(mean), y = 20*log10(mean/sqrt(var)),
tlab = "SNR Selected samples in linear sRGB",
slab = 'Without camera tone curve',
xlab = "Signal (stops)", xlim=c(-1.5,8),
ylab = "SNR (dB)")
@oscardelama
oscardelama / rgb-noise: Get spline approx to LR camera TC.r
Last active Aug 29, 2015
rgb-noise: Build a spline approximation to the Lightroom camera tone curve
View rgb-noise: Get spline approx to LR camera TC.r
# Plot log-log of LR and our camera tone curves
ggplot(lin.rgb.and.tif) +
geom_point(aes(x = (mean)/255, y = tif.mean.cam/255,
group = channel, colour = channel),
size = 1.5) +
ggtitle('LR Camera tone curve\nFor Nikon D7000') +
xlab('linear sRGB gray (log10)') + ylab('LR camera TC sRGB gray (log10)') +
scale_x_log10() +
scale_y_log10() +
geom_line(aes(x=x, y=y), data=my.cam.tc, color='darkblue', linetype='dashed')
You can’t perform that action at this time.