using NtToolBox using PyPlot using Images #using Autoreload #arequire("NtToolBox") n = 256 name = "NtToolBox/src/data/hibiscus.png" f = load_image(name, n, 1) imageplot(f); slex = 19:23 sely = 62:66 floor(255*f[19:24,62:66]); sub = (l, k) -> l[collect(i for i in 1:length(l[:, 1]) if (i - 1 )%k == 0), collect(i for i in 1:length(l[1, :]) if (i - 1 )%k == 0)] imageplot(sub(f, 4)) klist = [2, 4, 8, 16] clf for i in 1:length(klist) k = klist[i] imageplot( sub(f,k), string("1 ligne/ colonne sur ", string(k)), [2, 2, i]) end quant = (l,q) -> (round(q*rescale(f, 1e-3, 1 - 1e-3) - 1/2) + 1/2)/q imageplot(quant(f,4), "4 niveaux de gris") qlist = [16, 4, 3, 2] for i in 1:length(qlist) q = qlist[i] f1 = quant(f,q) f1[1] = 0 f1[2] = 1 imageplot(f1, string(string(q), " niveaux de gris"), [2, 2, i]) end name = "NtToolBox/src/data/boat.png" f = load_image(name, n) sigma = .08 f = f + randn(n,n)*sigma imageplot(clamP(f)) #We wrote clamP instead of clamp, because there is already a function clamp in module Base. roll2d = (f, i, j) -> circshift(f, (i, j)) function filt_moy(f, k) n = size(f)[1] g = zeros(n, n) for i in -k:k for j in -k:k g = g + roll2d(f, i, j) end end return g/((2*k + 1)^2) end imageplot(clamP(f), "Image bruitée", [1, 2, 1]) imageplot(clamP(filt_moy(f, 1)), "Image moyennée", [1, 2, 2]) klist = [1, 2, 3, 4] for i in 1:length(klist) k = klist[i] f1 = filt_moy(f, k) imageplot(clamP(f1), string("Moyenne de ", string((2*k+1)^2), " pixels"), [2, 2, i]) end function filt_med(f, k) n = size(f)[1] g = zeros(n, n, (2*k+1)^2) s = 1 for i in -k:k for j in -k:k g[:,:,s] = roll2d(f, i, j) s = s+1 end end return median(g, 3)[:, :] end imageplot(clamP(filt_moy(f, 1)), "Moyenne de 9 nombres", [1, 2, 1]) imageplot(clamP(filt_med(f, 1)), "Médiane de 9 nombres", [1, 2, 2]) klist = [1, 2, 3, 4] for i in 1:length(klist) k = klist[i] f1 = filt_med(f, k) imageplot(clamP(f1), string("Médiane de ", string((2*k + 1)^2), " pixels"), [2, 2, i]) end n = 256 name = "NtToolBox/src/data/hibiscus.png" f = load_image(name, n); edge = f -> sqrt( (roll2d(f, +1, 0) - roll2d(f, -1, 0)).^2 + (roll2d(f, 0, +1) - roll2d(f, 0, -1)).^2 ) imageplot(f, "Image", [1, 2, 1]) imageplot(edge(f), L"Carte de $\ell$", [1, 2, 2]) On peut voir que dans l'image de droite, les contours des objets ressortent en blanc, car ils correspondent aux grandes valeurs de $\ell$. n = 256 name = "NtToolBox/src/data/hibiscus.png" f = load_image(name, n, 0, 1); f1 = copy(f) f1[:,:, [2, 3]] = 0 f2 = copy(f) f2[:,:, [1, 3]] = 0 f3 = copy(f) f3[:,:, [1, 2]] = 0; imageplot(f, "Image couleur", [2, 2, 1]) imageplot(f1, "Canal rouge", [2, 2, 2]) imageplot(f2, "Canal vert", [2, 2, 3]) imageplot(f3, "Canal bleu", [2, 2, 4]) imageplot(f, "Couleur", [1, 2, 1]) imageplot(sum(f, 3)[:, :], "Luminance", [1, 2, 2]) f1 = copy(f) f1[:, :, [2,3]] = 1 f2 = copy(f) f2[:, :, [1,3]] = 1 f3 = copy(f) f3[:, :, [1,2]] = 1; imageplot(f, "Image couleur", [2, 2, 1]) imageplot(f1, "Canal cyan", [2, 2, 2]) imageplot(f2, "Canal magenta", [2, 2, 3]) imageplot(f3, "Canal jaune", [2, 2, 4]) n = 256 name = "NtToolBox/src/data/hibiscus.png" f = load_image(name, n); imageplot(-f) imageplot(f.^2, "Carré") imageplot(sqrt(f), L"Remplacement de a par $\sqrt{a}$") name = "NtToolBox/src/data/hibiscus.png" f = load_image(name, n, 0, 1); function repeat3(x, k) if (ndims(x) == 3) & (size(x)[3] == 3) x1 = Images.imresize( repeat(x[:, :, 1], inner = [1, k]), (n, n) ) x2 = Images.imresize( repeat(x[:, :, 2], inner = [1, k]), (n, n) ) x3 = Images.imresize( repeat(x[:, :, 3], inner = [1, k]), (n, n) ) l = zeros(n, n, 3) l[:, :, 1] = x1 l[:, :, 2] = x2 l[:, :, 3] = x3 return l else return Images.imresize( repeat(x[:, :], inner = [1, k]), (n, n) ) end end m = f -> repeat3( mean(f, 3), 3) function contrast_image(f, gamma) f1 = clamP(m(f).^gamma + f[:, :, 1] - m(f)) f2 = clamP(m(f).^gamma + f[:, :, 2] - m(f)) f3 = clamP(m(f).^gamma + f[:, :, 3] - m(f)) l = zeros(length(f[:, 1, 1]), length(f[:, 1, 1]), 3) l[:, :, 1] = f1 l[:, :, 2] = f2 l[:, :, 3] = f3 return l end gamma_list = [.5, .75, 1, 1.5, 2, 3] for i in 1:length(gamma_list) gamma = gamma_list[i] subplot(2, 3, i) imageplot(contrast_image(f, gamma)); title(string(L"$\gamma$ = ", string(gamma))) end name = "NtToolBox/src/data/flowers.png" n = 502 A = load_image(name, n, 0, 1) B1 = A[:, :, 1]'; B2 = A[:, :, 2]'; B3 = A[:, :, 3]' B = zeros(n, n, 3) B[:, :, 1] = B1; B[:, :, 2] = B2; B[:, :, 3] = B3 imageplot(A, "Image A", [1,2,1]) imageplot(B, "Image B", [1,2,2]) C = A C = C[end:-1:1, :, :] C1 = C[:, :, 1]'; C2 = C[:, :, 2]'; C3 = C[:, :, 3]' C = zeros(n, n, 3) C[:, :, 1] = C1; C[:, :, 2] = C2; C[:, :, 3] = C3 imageplot(A, "Image A", [1,2,1]) imageplot(C, "Image C", [1,2,2]) name1 = "NtToolBox/src/data/flowers.png" name2 = "NtToolBox/src/data/hibiscus.png" n = 502 A = load_image(name1, n, 0, 1) B = load_image(name2, n, 0, 1) imageplot(A, "Image A", [1,2,1]) imageplot(B, "Image B", [1,2,2]) p = 6 t = linspace(0, 1, p) for i in 1:p imageplot(t[i]*A + (1-t[i])*B, string("t = ", string(t[i])), [2, p/2, i]) end