Author: Hakan Ardo <[email protected]>
Branch: extradoc
Changeset: r3633:dcfb63160aeb
Date: 2011-06-10 16:46 +0200
http://bitbucket.org/pypy/extradoc/changeset/dcfb63160aeb/
Log: some morphology
diff --git a/talk/iwtc11/benchmarks/benchmark.sh
b/talk/iwtc11/benchmarks/benchmark.sh
--- a/talk/iwtc11/benchmarks/benchmark.sh
+++ b/talk/iwtc11/benchmarks/benchmark.sh
@@ -14,6 +14,7 @@
$* convolution/conv5.c -lm; /usr/bin/time -f %e ./a.out 1000 > /dev/null
$* convolution/conv3x3.cc -lstdc++; /usr/bin/time -f %e ./a.out 1000000 3
> /dev/null
$* convolution/conv3x3.cc -lstdc++; /usr/bin/time -f %e ./a.out 1000 1000
> /dev/null
+ $* convolution/dilate3x3.cc -lstdc++; /usr/bin/time -f %e ./a.out 1000
1000 > /dev/null
rm a.out
else
$* sqrt/time_sqrt.py float
@@ -22,4 +23,5 @@
$* convolution/time_conv.py 1
$* convolution/time_conv.py 100
$* convolution/time_conv.py 1000
+ $* convolution/time_conv2d.py
fi
diff --git a/talk/iwtc11/benchmarks/convolution/convolution.py
b/talk/iwtc11/benchmarks/convolution/convolution.py
--- a/talk/iwtc11/benchmarks/convolution/convolution.py
+++ b/talk/iwtc11/benchmarks/convolution/convolution.py
@@ -53,3 +53,19 @@
k[2,1]*a[x-1, y] + k[1,1]*a[x, y] + k[0,1]*a[x+1, y]
+ \
k[2,0]*a[x-1, y+1] + k[1,0]*a[x, y+1] + k[0,0]*a[x+1,
y+1]
return b
+
+def morphology3x3(a, k, func):
+ assert k.width == k.height == 3
+ b = Array2D(a.width, a.height)
+ for y in xrange(1, a.height-1):
+ for x in xrange(1, a.width-1):
+ b[x, y] = func(k[2,2]*a[x-1, y-1], k[1,2]*a[x, y-1], k[0,2]*a[x+1,
y-1], \
+ k[2,1]*a[x-1, y] , k[1,1]*a[x, y] , k[0,1]*a[x+1,
y] , \
+ k[2,0]*a[x-1, y+1], k[1,0]*a[x, y+1], k[0,0]*a[x+1,
y+1])
+ return b
+
+def dilate3x3(a, k):
+ return morphology3x3(a, k, max)
+
+def erode3x3(a, k):
+ return morphology3x3(a, k, min)
diff --git a/talk/iwtc11/benchmarks/convolution/dilate3x3.cc
b/talk/iwtc11/benchmarks/convolution/dilate3x3.cc
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/convolution/dilate3x3.cc
@@ -0,0 +1,54 @@
+// A safe array example.
+#include <stdio.h>
+#include <stdlib.h>
+
+class Array2D {
+ double *data;
+public:
+ int width, height;
+ Array2D(int w, int h) {
+ width = w;
+ height = h;
+ data = (double *) malloc(w*h*sizeof(double));
+ }
+ double &operator()(int x, int y) {
+ if (x >= 0 && x < width && y >= 0 && y < height) {
+ return data[y*width + x];
+ }
+ printf("IndexError\n");
+ exit(1);
+ }
+};
+
+#define max(x,y) ((x) > (y) ? (x) : (y))
+
+void dilate3x3(Array2D &a, Array2D &k, Array2D &b) {
+ int x, y;
+ for (y=1; y<a.height-1; y++) {
+ for (x=1; x<a.width-1; x++) {
+ double v = k(2,2)*a(x-1, y-1);
+ v = max(v, k(1,2)*a(x, y-1));
+ v = max(v, k(0,2)*a(x+1, y-1));
+ v = max(v, k(2,1)*a(x-1, y));
+ v = max(v, k(1,1)*a(x, y));
+ v = max(v, k(0,1)*a(x+1, y));
+ v = max(v, k(2,0)*a(x-1, y+1));
+ v = max(v, k(1,0)*a(x, y+1));
+ v = max(v, k(0,0)*a(x+1, y+1));
+ b(x, y) = v;
+ }
+ }
+}
+
+int main(int ac, char **av) {
+ int w = atoi(av[1]), h = atoi(av[2]);
+ int i;
+
+ for (i=0; i<10; i++) {
+ Array2D a(w, h), b(w, h), k(3, 3);
+ dilate3x3(a, k, b);
+ printf("%f\n", b(1,1));
+ }
+ fprintf(stderr, "dilate3x3(%d): ", h);
+ return 0;
+}
diff --git a/talk/iwtc11/benchmarks/convolution/time_conv2d.py
b/talk/iwtc11/benchmarks/convolution/time_conv2d.py
--- a/talk/iwtc11/benchmarks/convolution/time_conv2d.py
+++ b/talk/iwtc11/benchmarks/convolution/time_conv2d.py
@@ -1,4 +1,4 @@
-from convolution import conv3x3, Array2D
+from convolution import conv3x3, Array2D, dilate3x3, erode3x3
from array import array
import sys, time
@@ -8,7 +8,9 @@
except ImportError:
pass
-conv3x3(Array2D(1001, 1001), Array2D(3,3)) # Warmup
+# Warmup
+conv3x3(Array2D(1010, 1010), Array2D(3,3))
+dilate3x3(Array2D(1010, 1010), Array2D(3,3))
a = time.time()
for i in range(10):
@@ -22,4 +24,8 @@
b = time.time()
print 'conv3x3(1000):', b - a
-
+a = time.time()
+for i in range(10):
+ dilate3x3(Array2D(1000, 1000), Array2D(3,3))
+b = time.time()
+print 'dilate3x3(1000):', b - a
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit