Skip to content

Instantly share code, notes, and snippets.

@oscardelama
oscardelama / xval-noise-models-p2.r
Last active August 29, 2015 14:16
xval-noise-models: Fit the models.
# 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 / xval-noise-models-p1.r
Last active August 29, 2015 14:16
xval-noise-models: Cross validation of "var ~ mean + mean^2" models testing a SNR optimization model versus the regular VVM model.
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-p3.r
Last active August 29, 2015 14:16
xval-noise-models: Test the models
# 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 August 29, 2015 14:16
xval-noise-models: The main routine
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 / loss-of-snr.r
Last active August 29, 2015 14:15
Compute the loss of SNR we converting a raw image to a RGB color space.
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 August 29, 2015 14:15
rgb-noise: Process Lightroom samples, with zero settings
# 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 August 29, 2015 14:15
rgb-noise: Draw SNR quality reference limits for 18% gray
# 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 August 29, 2015 14:14
rgb-noise: Condense the three raw noise models in one
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 February 6, 2015 16:44
rgb-noise: sRGB linear - Initial analysis
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 / gist:9f1a52e7017666011be6
Last active August 29, 2015 14:14
rgb-noise: Scale function from linear sRGB to Lightroom sRGB
# Get the LR tifs var data
tif.var.zero <- vvm.tif$var.df
# Rename 'mean' and 'var' columns to avoid later name colision in merging
names(tif.var.zero)[3:4] <- paste0('tif.', names(tif.var.zero)[3:4])
# Remove sample file name extension to make possible the merge by picture file name
tif.var.zero$pict <- substr(tif.var.zero$pict, 1, 8)
# Compute the linear sRGB values
vvm.rgb.lin <- convert.to.rgb(target.space = 'sRGB', use.camera.tc = FALSE, target.tc = 'linear')
# Get the sRGB linear wide var data