diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-02-16 21:51:54 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-02-16 21:51:54 +0100 |
commit | 321b268bb74512289f9eb361560e8ceb9d30fe59 (patch) | |
tree | b5ddb9ab187a952891fc2f988fb47088fc03a5f9 | |
parent | 67438312a69f7b7aa1fdbc27610438897791ceef (diff) |
i&m: add operations
19 files changed, 313 insertions, 0 deletions
diff --git a/Year_2/IandM/equalization/data/lena.png b/Year_2/IandM/equalization/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/equalization/data/lena.png diff --git a/Year_2/IandM/equalization/equalization.pde b/Year_2/IandM/equalization/equalization.pde new file mode 100644 index 0000000..fa7b1e2 --- /dev/null +++ b/Year_2/IandM/equalization/equalization.pde @@ -0,0 +1,49 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(equalization(img), 256, 0); +} + +PImage equalization(PImage I) { + PImage out = I.copy(); + float[] h = histogram(out); + + for (int i = 1; i < 256; ++i) { + h[i] = h[i]+h[i-1]; + } + + out.loadPixels(); + + for (int i = 0; i < out.pixels.length; ++i) { + out.pixels[i] = color(255*h[int(red(out.pixels[i]))]); + } + + out.updatePixels(); + + return out; +} + +float[] histogram(PImage I) { + float[] h = new float[256]; + for (int i = 0; i< 256; ++i) { + h[i] = 0; + } + + I.loadPixels(); + + for (int i = 0; i < I.pixels.length; ++i) { + h[int(red(I.pixels[i]))]++; + } + + for (int i = 0; i < 256; ++i) { + h[i] = h[i] / I.pixels.length; + } + + I.updatePixels(); + + return h; +} diff --git a/Year_2/IandM/gamma/data/lena.png b/Year_2/IandM/gamma/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/gamma/data/lena.png diff --git a/Year_2/IandM/gamma/gamma.pde b/Year_2/IandM/gamma/gamma.pde new file mode 100644 index 0000000..8317e75 --- /dev/null +++ b/Year_2/IandM/gamma/gamma.pde @@ -0,0 +1,26 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + + image(img, 0, 0); + image(gamma(img, 2), 256, 0); +} + +PImage gamma(PImage I, float gm) { + PImage out = I.copy(); + out.loadPixels(); + float r, g, b; + float C = 1 / pow(255, gm-1); + + for (int i = 0; i < out.pixels.length; ++i) { + r= C*pow(red(out.pixels[i]), gm); + g= C*pow(green(out.pixels[i]), gm); + b= C*pow(blue(out.pixels[i]), gm); + + out.pixels[i] = color(r, g, b); + } + + out.updatePixels(); + return out; +} diff --git a/Year_2/IandM/histogram/data/lena.png b/Year_2/IandM/histogram/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/histogram/data/lena.png diff --git a/Year_2/IandM/histogram/histogram.pde b/Year_2/IandM/histogram/histogram.pde new file mode 100644 index 0000000..58f365a --- /dev/null +++ b/Year_2/IandM/histogram/histogram.pde @@ -0,0 +1,33 @@ +void setup() { + size(256, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + float[] hist = histogram(img); + for(int i = 0; i < 256; ++i) { + println(i, hist[i]); + } +} + +float[] histogram(PImage I) { + float[] h = new float[256]; + for (int i = 0; i< 256; ++i) { + h[i] = 0; + } + + I.loadPixels(); + + for(int i = 0; i < I.pixels.length; ++i) { + h[int(red(I.pixels[i]))]++; + } + + for(int i = 0; i < 256; ++i) { + h[i] = h[i] / I.pixels.length; + } + + I.updatePixels(); + + return h; +} diff --git a/Year_2/IandM/logarithm/data/lena.png b/Year_2/IandM/logarithm/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/logarithm/data/lena.png diff --git a/Year_2/IandM/logarithm/logarithm.pde b/Year_2/IandM/logarithm/logarithm.pde new file mode 100644 index 0000000..5fe02e7 --- /dev/null +++ b/Year_2/IandM/logarithm/logarithm.pde @@ -0,0 +1,26 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + + image(img, 0, 0); + image(logarithm(img, 100), 256, 0); +} + +PImage logarithm(PImage I, int k) { + PImage out = I.copy(); + out.loadPixels(); + float r, g, b; + float C = k/log(256); + + for (int i = 0; i < out.pixels.length; ++i) { + r= C*log(1+red(out.pixels[i])); + g= C*log(1+green(out.pixels[i])); + b= C*log(1+blue(out.pixels[i])); + + out.pixels[i] = color(r, g, b); + } + + out.updatePixels(); + return out; +} diff --git a/Year_2/IandM/negative/data/lena.png b/Year_2/IandM/negative/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/negative/data/lena.png diff --git a/Year_2/IandM/negative/negative.pde b/Year_2/IandM/negative/negative.pde new file mode 100644 index 0000000..c7c4475 --- /dev/null +++ b/Year_2/IandM/negative/negative.pde @@ -0,0 +1,25 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + + image(img, 0, 0); + image(negative(img), 256, 0); +} + +PImage negative(PImage I) { + PImage out = I.copy(); + out.loadPixels(); + float r, g, b; + + for (int i = 0; i < out.pixels.length; ++i) { + r= 255-red(out.pixels[i]); + g= 255-green(out.pixels[i]); + b= 255-blue(out.pixels[i]); + + out.pixels[i] = color(r, g, b); + } + + out.updatePixels(); + return out; +} diff --git a/Year_2/IandM/psnr_mse/data/lena.png b/Year_2/IandM/psnr_mse/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/psnr_mse/data/lena.png diff --git a/Year_2/IandM/psnr_mse/psnr_mse.pde b/Year_2/IandM/psnr_mse/psnr_mse.pde new file mode 100644 index 0000000..9aa0a8b --- /dev/null +++ b/Year_2/IandM/psnr_mse/psnr_mse.pde @@ -0,0 +1,50 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + PImage img2 = negative(img); + image(img, 0, 0); + image(img2, 256, 0); + + print("PSNR: ", psnr(img, img2)); +} + +float mse(PImage i1, PImage i2) { + float res = 0; + i1.loadPixels(); + i2.loadPixels(); + + for (int i = 0; i < i1.pixels.length; ++i) { + res+= pow(red(i1.pixels[i])-red(i2.pixels[i]), 2); + } + res = res/i1.pixels.length; + i1.updatePixels(); + i2.updatePixels(); + + return res; +} + +float psnr(PImage i1, PImage i2) { + float mse = mse(i1, i2); + float res = 10*log(255*255/mse)/log(10); + + return res; +} + +PImage negative(PImage I) { + PImage out = I.copy(); + out.loadPixels(); + float r, g, b; + + for (int i = 0; i < out.pixels.length; ++i) { + r= 255-red(out.pixels[i]); + g= 255-green(out.pixels[i]); + b= 255-blue(out.pixels[i]); + + out.pixels[i] = color(r, g, b); + } + + out.updatePixels(); + return out; +} diff --git a/Year_2/IandM/psnr_mse/psnr_mse8365406430453607560.autosave b/Year_2/IandM/psnr_mse/psnr_mse8365406430453607560.autosave new file mode 100644 index 0000000..cac2b99 --- /dev/null +++ b/Year_2/IandM/psnr_mse/psnr_mse8365406430453607560.autosave @@ -0,0 +1,25 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(quantization(img, 10), 256, 0); +} + +PImage quantization(PImage I, int k) { + PImage out = I.copy(); + out.loadPixels(); + int r; + + for (int i = 0; i < out.pixels.length; ++i) { + r = (int) floor((red(out.pixels[i])*k)/256); + r = int((float(r)/(k-1))*255); + + out.pixels[i] = color(r); + } + + out.updatePixels(); + return out; +} diff --git a/Year_2/IandM/quantization/data/lena.png b/Year_2/IandM/quantization/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/quantization/data/lena.png diff --git a/Year_2/IandM/quantization/quantization.pde b/Year_2/IandM/quantization/quantization.pde new file mode 100644 index 0000000..cac2b99 --- /dev/null +++ b/Year_2/IandM/quantization/quantization.pde @@ -0,0 +1,25 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(quantization(img, 10), 256, 0); +} + +PImage quantization(PImage I, int k) { + PImage out = I.copy(); + out.loadPixels(); + int r; + + for (int i = 0; i < out.pixels.length; ++i) { + r = (int) floor((red(out.pixels[i])*k)/256); + r = int((float(r)/(k-1))*255); + + out.pixels[i] = color(r); + } + + out.updatePixels(); + return out; +} diff --git a/Year_2/IandM/replication2x/data/lena.png b/Year_2/IandM/replication2x/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/replication2x/data/lena.png diff --git a/Year_2/IandM/replication2x/replication2x.pde b/Year_2/IandM/replication2x/replication2x.pde new file mode 100644 index 0000000..dc1d162 --- /dev/null +++ b/Year_2/IandM/replication2x/replication2x.pde @@ -0,0 +1,22 @@ +void setup() { + size(768, 512); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + image(img, 0, 0); + image(replication2x(img), 256, 0); +} + +PImage replication2x(PImage I) { + PImage out = createImage(I.width*2, I.height*2, RGB); + + for(int x = 0; x < I.width; ++x) { + for(int y = 0; y < I.height; ++y) { + out.set(2*x, 2*y, I.get(x, y)); + out.set(2*x+1, 2*y, I.get(x, y)); + out.set(2*x, 2*y+1, I.get(x, y)); + out.set(2*x+1, 2*y+1, I.get(x, y)); + } + } + + return out; +} diff --git a/Year_2/IandM/stretching/data/lena.png b/Year_2/IandM/stretching/data/lena.png Binary files differnew file mode 100644 index 0000000..59ef68a --- /dev/null +++ b/Year_2/IandM/stretching/data/lena.png diff --git a/Year_2/IandM/stretching/stretching.pde b/Year_2/IandM/stretching/stretching.pde new file mode 100644 index 0000000..0814190 --- /dev/null +++ b/Year_2/IandM/stretching/stretching.pde @@ -0,0 +1,32 @@ +void setup() { + size(512, 256); + PImage img = loadImage("lena.png"); + img.resize(256, 256); + img.filter(GRAY); + + image(img, 0, 0); + image(stretching(img), 256, 0); +} + +PImage stretching(PImage I) { + PImage out = I.copy(); + out.loadPixels(); + + float max = red(out.pixels[0]); + float min = max; + + for (int i = 0; i < out.pixels.length; ++i) { + float t = red(out.pixels[i]); + if (t < min) + min = t; + if (t > max) + max = t; + } + + for (int i = 0; i < out.pixels.length; ++i) { + out.pixels[i]= color(255*(red(out.pixels[i])-min)/(max-min)); + } + + out.updatePixels(); + return out; +} |