diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-02-18 19:18:32 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-02-18 19:18:32 +0100 |
commit | 1942f603803a92b0c2f078975a04167a6ee2f7bf (patch) | |
tree | d9d63dcce55c3f05fa94c94f4abf5f394aca2057 | |
parent | ab0dfb9eb5c0e1d39bb2e87bb3692171585a6716 (diff) |
i&m: add operators
-rw-r--r-- | Year_2/IandM/bitplane/bitplane.pde | 39 | ||||
-rw-r--r-- | Year_2/IandM/bitplane/data/lena.png | bin | 0 -> 473831 bytes | |||
-rw-r--r-- | Year_2/IandM/convolution/convolution.pde | 73 | ||||
-rw-r--r-- | Year_2/IandM/convolution/data/lena.png | bin | 0 -> 473831 bytes | |||
-rw-r--r-- | Year_2/IandM/maximum/data/lena.png | bin | 0 -> 473831 bytes | |||
-rw-r--r-- | Year_2/IandM/maximum/maximum.pde | 31 | ||||
-rw-r--r-- | Year_2/IandM/median/data/lena.png | bin | 0 -> 473831 bytes | |||
-rw-r--r-- | Year_2/IandM/median/median.pde | 37 | ||||
-rw-r--r-- | Year_2/IandM/minimum/data/lena.png | bin | 0 -> 473831 bytes | |||
-rw-r--r-- | Year_2/IandM/minimum/minimum.pde | 31 |
10 files changed, 211 insertions, 0 deletions
diff --git a/Year_2/IandM/bitplane/bitplane.pde b/Year_2/IandM/bitplane/bitplane.pde new file mode 100644 index 0000000..33cda9c --- /dev/null +++ b/Year_2/IandM/bitplane/bitplane.pde @@ -0,0 +1,39 @@ +int nb = 0; + +void setup() { + size(512, 256); +} + +void draw() { + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(bitplane(img, nb), 256, 0); +} + +void keyPressed() { + if(key == '+' && nb < 7) { + nb++; + } else if(key == '-' && nb > 1) { + nb--; + } +} + +PImage bitplane(PImage I, int nb) { + PImage out = I.copy(); + out.loadPixels(); + + int x, r; + + for (int i = 0; i < out.pixels.length; ++i) { + x = int(red(out.pixels[i])); + r = (x >> nb) & 1; + out.pixels[i] = color(255*r); + } + + out.updatePixels(); + + return out; +} diff --git a/Year_2/IandM/bitplane/data/lena.png b/Year_2/IandM/bitplane/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/bitplane/data/lena.png diff --git a/Year_2/IandM/convolution/convolution.pde b/Year_2/IandM/convolution/convolution.pde new file mode 100644 index 0000000..cb2acf5 --- /dev/null +++ b/Year_2/IandM/convolution/convolution.pde @@ -0,0 +1,73 @@ +float n3 = 1/9.0; +float[][] nbox3 = {{n3, n3, n3}, + {n3, n3, n3}, + {n3, n3, n3}}; + +float n5 = 1.0/25; +float[][] nbox5 = {{n5, n5, n5, n5, n5}, + {n5, n5, n5, n5, n5}, + {n5, n5, n5, n5, n5}, + {n5, n5, n5, n5, n5}, + {n5, n5, n5, n5, n5}}; + +float[][] lapl = {{-1, 0, -1}, + {0, 4, 0}, + {-1, 0, -1}}; + +float[][] sobx = {{-1, -2, -1}, + {0, 0, 0}, + {1, 2, 1}}; + +float[][] sharp = {{-1, 0, -1}, + {0, 5, 0}, + {-1, 0, -1}}; + +void setup() { + size(768, 512); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(convert(convolution(img, nbox3)), 256, 0); + image(convert(convolution(img, nbox5)), 512, 0); + image(convert(convolution(img, lapl)), 0, 256); + image(convert(convolution(img, sobx)), 256, 256); + image(convert(convolution(img, sharp)), 512, 256); +} + + +PImage convert(float[][] mat) { + PImage out = createImage(mat[0].length, mat.length, RGB); + float res = 0; + + for (int x = 0; x < out.width; ++x) { + for (int y = 0; y < out.height; ++y) { + res = constrain(abs(mat[y][x]), 0, 255); + out.set(x, y, color(res)); + } + } + + return out; +} + +float[][] convolution(PImage I, float[][] kernel) { + float[][] out = new float[I.height][I.width]; + int n = kernel[0].length; + int m = kernel.length; + PImage tmp; + + for (int x = 0; x < I.width; ++x) { + for (int y = 0; y < I.height; ++y) { + tmp = I.get(x-n/2, y-m/2, n, m); + float res = 0; + for (int p = 0; p < n; ++p) + for (int q = 0; q < m; ++q) + res+=red(tmp.get(p, q)) * kernel[q][p]; + + out[y][x] = res; + } + } + + return out; +} diff --git a/Year_2/IandM/convolution/data/lena.png b/Year_2/IandM/convolution/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/convolution/data/lena.png diff --git a/Year_2/IandM/maximum/data/lena.png b/Year_2/IandM/maximum/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/maximum/data/lena.png diff --git a/Year_2/IandM/maximum/maximum.pde b/Year_2/IandM/maximum/maximum.pde new file mode 100644 index 0000000..4f0f54f --- /dev/null +++ b/Year_2/IandM/maximum/maximum.pde @@ -0,0 +1,31 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(maximum(img, 11), 256, 0); +} + +PImage maximum(PImage I, int n) { + PImage out = createImage(I.width, I.height, RGB); + + int off = n/2; + + for (int x = 0; x < I.width; ++x) { + for (int y = 0; y < I.height; ++y) { + PImage t = I.get(x-off, y-off, n, n); + float[] tarr = new float[n*n]; + t.loadPixels(); + + for(int i = 0; i < t.pixels.length; ++i) { + tarr[i] = red(t.pixels[i]); + } + + out.set(x, y, color(max(tarr))); + } + } + + return out; +} diff --git a/Year_2/IandM/median/data/lena.png b/Year_2/IandM/median/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/median/data/lena.png diff --git a/Year_2/IandM/median/median.pde b/Year_2/IandM/median/median.pde new file mode 100644 index 0000000..856e50e --- /dev/null +++ b/Year_2/IandM/median/median.pde @@ -0,0 +1,37 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(median(img, 11), 256, 0); +} + +PImage median(PImage I, int n) { + PImage out = createImage(I.width, I.height, RGB); + + int off = n/2; + + for (int x = 0; x < I.width; ++x) { + for (int y = 0; y < I.height; ++y) { + PImage t = I.get(x-off, y-off, n, n); + float[] tarr = new float[n*n]; + t.loadPixels(); + + for(int i = 0; i < t.pixels.length; ++i) { + tarr[i] = red(t.pixels[i]); + } + + tarr = sort(tarr); + float med = tarr[(n*n)/2]; + if((n*n)%2 == 0) { + med = (med+tarr[(n*n)/2-1])/2; + } + + out.set(x, y, color(med)); + } + } + + return out; +} diff --git a/Year_2/IandM/minimum/data/lena.png b/Year_2/IandM/minimum/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/minimum/data/lena.png diff --git a/Year_2/IandM/minimum/minimum.pde b/Year_2/IandM/minimum/minimum.pde new file mode 100644 index 0000000..b960a24 --- /dev/null +++ b/Year_2/IandM/minimum/minimum.pde @@ -0,0 +1,31 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(minimum(img, 11), 256, 0); +} + +PImage minimum(PImage I, int n) { + PImage out = createImage(I.width, I.height, RGB); + + int off = n/2; + + for (int x = 0; x < I.width; ++x) { + for (int y = 0; y < I.height; ++y) { + PImage t = I.get(x-off, y-off, n, n); + float[] tarr = new float[n*n]; + t.loadPixels(); + + for(int i = 0; i < t.pixels.length; ++i) { + tarr[i] = red(t.pixels[i]); + } + + out.set(x, y, color(min(tarr))); + } + } + + return out; +} |