summaryrefslogtreecommitdiff
path: root/Year_2/IandM/psnr_mse
diff options
context:
space:
mode:
Diffstat (limited to 'Year_2/IandM/psnr_mse')
-rw-r--r--Year_2/IandM/psnr_mse/data/lena.pngbin0 -> 473831 bytes
-rw-r--r--Year_2/IandM/psnr_mse/psnr_mse.pde50
-rw-r--r--Year_2/IandM/psnr_mse/psnr_mse8365406430453607560.autosave25
3 files changed, 75 insertions, 0 deletions
diff --git a/Year_2/IandM/psnr_mse/data/lena.png b/Year_2/IandM/psnr_mse/data/lena.png
new file mode 100644
index 0000000..59ef68a
--- /dev/null
+++ b/Year_2/IandM/psnr_mse/data/lena.png
Binary files differ
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;
+}