library(imager) library(png) library(pracma) # library(SynchWave) # Importing the libraries for (f in list.files(path="nt_toolbox/toolbox_general/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_general/", f, sep="")) } for (f in list.files(path="nt_toolbox/toolbox_signal/", pattern="*.R")) { source(paste("nt_toolbox/toolbox_signal/", f, sep="")) } n = 256 name = 'nt_toolbox/data/hibiscus.png' options(repr.plot.width=4, repr.plot.height=4) f = load_image(name, n, 1) imageplot(f) selx = c(20:25) sely = c(63:68) print(floor(255 * f[selx,sely])) sub = function(f,k){f[seq(1, nrow(f), k), seq(1, ncol(f), k)]} options(repr.plot.width=4, repr.plot.height=4) imageplot(sub(f[,],4)) klist = c(2, 4, 8, 16) for (i in c(1:length(klist))) { options(repr.plot.width=5, repr.plot.height=3) k = klist[i] imageplot( sub(f[,], k), paste('1 ligne/colonne sur ', toString(k)), c(1, 2, i)) } quant = function(f,q){round(q * rescale(f, 1e-3, 1 - 1e-3)) / q} imageplot(quant(f[,], 4), '4 niveaux de gris') qlist = c(16, 4, 3, 2) for (i in c(1:length(qlist))) { q = qlist[i] f1 = quant(f[,],q) f1[0] = 0 f1[1] = 1 options(repr.plot.width=5, repr.plot.height=3) imageplot(f1[,], paste(toString(q), 'niveaux de gris'), c(1, 2, i)) } name = 'nt_toolbox/data/boat.png' f = load_image(name, n) sigma = .08 f = f[,] + randn(n,n) * sigma imageplot(clamp(f)) roll2d = function(f,i,j){roll(roll(f,i,axis=0),j,axis=1)} filt_moy = function(f,k) { n = dim(f)[1] g = matrix(0, n,n) for (i in c(-k :k)) { for (j in c(-k : k)) { g = g + roll2d(f,i,j) } } return(g/( (2 * k + 1)**2 )) } imageplot(clamp(f[,]), 'Image bruitée', c(1, 2, 1)) imageplot(clamp(filt_moy(f[,], 1)), 'Image moyennée', c(1, 2, 2)) klist = c(1, 2, 3, 4) for (i in c(1 : length(klist))) { k = klist[i] f1 = filt_moy(f,k) imageplot(clamp(f1), paste('Moyenne de ', toString((2*k+1)**2), ' pixels'), c(1, 2, i)) } filt_med = function(f,k) { n = dim(f)[1] g = array(0, dim=c(n, n, (2 * k + 1)**2)) s = 1 for (i in c(-k : k)) { for (j in c(-k : k)) { g[,,s] = roll2d(f, i, j) s = s + 1 } } return(apply(g, 1:2, median)) } imageplot(clamp(filt_moy(f[,], 1)), 'Moyenne de 9 nombres', c(1, 2, 1)) imageplot(clamp(filt_med(f[,], 1)), 'Médiane de 9 nombres', c(1, 2, 2)) klist = c(1, 2, 3, 4) for (i in c(1 : length(klist))) { k = klist[i] f1 = filt_med(f,k) imageplot(clamp(f1), paste('Médiane de ', toString((2*k+1)**2), ' pixels'), c(1, 2, i)) } n = 256 name = 'nt_toolbox/data/hibiscus.png' f = load_image(name, n) edge = function(f){sqrt((roll2d(f,1,0) - roll2d(f, -1,0))**2 + (roll2d(f, 0, 1) - roll2d(f,0, -1))**2)} imageplot(f[,], 'Image', c(1, 2, 1)) imageplot(edge(f[,]), 'Carte de l', c(1, 2, 2)) name = "nt_toolbox/data/hibiscus.png" f = load_image(name, flatten=0, grayscale=0) f1 = f f1[,,,c(2, 3)] = 0 f2 = f f2[,,,c(1, 3)] = 0 f3 = f f3[,,,c(1, 2)] = 0 par(mfrow=c(1,2)) imageplot(f, 'Image couleur') imageplot(f1, 'Canal rouge') par(mfrow=c(1,2)) imageplot(f2, 'Canal vert') imageplot(f3, 'Canal bleu') par(mfrow=c(1,2)) imageplot(f, 'Couleur') imageplot(apply(f,1:2, sum), "Gris") f1 = f f1[,,,c(2, 3)] = 0 f2 = f f2[,,,c(1, 3)] = 1 f3 = f f3[,,,c(1, 2)] = 1 par(mfrow=c(1,2)) imageplot(f, 'Image couleur') imageplot(f1, 'Canal cyan') par(mfrow=c(1,2)) imageplot(f2, 'Canal magenta') imageplot(f3, 'Canal jaune') name = 'nt_toolbox/data/hibiscus.png' f = load_image(name, n) imageplot(-f) imageplot(f**2, 'Carré') imageplot(sqrt(f), 'Remplacement de a par sqrt(a)') name = 'nt_toolbox/data/hibiscus.png' f = load_image(name, n, 0, 1) m = function(f) { m = array(0, dim=dim(f)) for (i in 1:dim(m)[4]) { m[,,1,i] = apply(f, 1:2, mean) } return(m) } contrast = function(f,gamma){clamp(m(f) ** gamma + f - m(f))} gamma_list = c(.5, .75, 1, 1.5, 2, 3) options(repr.plot.width=5, repr.plot.height=5) for (i in 1:length(gamma_list)) { if (i %% 3 == 1) { par(mfrow=c(1,3)) } gamma = gamma_list[i] title = paste('gamma = ', toString(gamma)) imageplot(contrast(f, gamma), title) } name = 'nt_toolbox/data/flowers.png' n = 512 options(repr.plot.width=5, repr.plot.height=5) A = load_image(name, n, 0, 1) B = A B[,,,1] = t(A[,,1,1]) B[,,,2] = t(A[,,1,2]) B[,,,3] = t(A[,,1,3]) par(mfrow=c(1,2)) imageplot(A, 'Image A', c(1,2,1)) imageplot(B, 'Image B', c(1,2,2)) C = A C[,,,] = C[,dim(A)[1]:1,,] #C[,,,1] = t(C[,,,1]) #C[,,,2] = t(C[,,1,2]) #C[,,,3] = t(C[,,1,3]) C = as.cimg(aperm(C, c(2, 1, 3, 4))) options(repr.plot.width=5, repr.plot.height=5) par(mfrow=c(1,2)) imageplot(A, 'Image A') imageplot(C, 'Image C') name1 = 'nt_toolbox/data/flowers.png' name2 = 'nt_toolbox/data/hibiscus.png' n = 512 A = load_image(name1, n, 0, 1) B = load_image(name2, n, 0, 1) par(mfrow=c(1,2)) imageplot(A, 'Image A') imageplot(B, 'Image B') p = 6 t = linspace(0, 1, p) for (i in c(1:p)) { if (i %% 3 == 1) { par(mfrow=c(1,3)) } title = paste('t = ', toString(t[i])) imageplot(t[i] * A + (1 - t[i]) * as.cimg(B[,,,1:3]), title, c(2, p/2, i)) }