hough transform in C
[imago.git] / pcf.c
diff --git a/pcf.c b/pcf.c
index 4a14681..8cb8a4b 100644 (file)
--- a/pcf.c
+++ b/pcf.c
@@ -1,4 +1,75 @@
 #include <Python.h>
 #include <Python.h>
+#include <math.h>
+
+static PyObject* py_hough(PyObject* self, PyObject* args)
+{
+       const unsigned char *image;
+       int x;
+       int y;
+       int size;
+       double init_angle;
+       double dt;
+
+       int i;
+       int j;
+       int a;
+
+       double distance;
+       int column;
+       int minimum;
+       int maximum;
+
+       int *matrix;
+       unsigned char *n_image;
+       PyObject *result;
+
+       if (!PyArg_ParseTuple(args, "(ii)s#dd", &x, &y, &image, &size, &init_angle, &dt)) return NULL;
+       
+
+       matrix = (int*) malloc(size * sizeof(int));
+       for (i=0; i < x * y; i++) {
+               matrix[i] = 0;
+       }
+
+
+
+       for (i=0; i < x; i++) {
+               for (j=0; j < y; j++) {
+                       if (image[j * x + i]){
+                               for (a=0; a < y; a++){
+                                       distance = (((i - x / 2) * sin((dt * a) + init_angle)) +
+                                                       ((j - y / 2) * -cos((dt * a) + init_angle)) +
+                                                       x / 2);
+                                       column = (int) round(distance);
+                                       if ((0 <= column) && (column < x)){
+                                               matrix[a * x + column]++;
+                                       }
+                               }
+                       }
+               }
+       }
+       
+
+
+
+       n_image = (char*) malloc(size * sizeof(char));
+       minimum = matrix[0];
+       maximum = matrix[0];
+       for (i=1; i < x * y; i++){
+               if (matrix[i] < minimum) minimum = matrix[i];
+               if (matrix[i] > maximum) maximum = matrix[i];
+       }
+       maximum = maximum - minimum + 1;
+       for (i=0; i < x * y; i++){
+               n_image[i] = (char) ((((float) (matrix[i] - minimum)) / maximum) * 256);
+       }
+
+       free(matrix);
+
+       result = Py_BuildValue("s#", n_image, size);
+       free(n_image);
+       return result;
+}
 
 static PyObject* py_edge(PyObject* self, PyObject* args)
 {
 
 static PyObject* py_edge(PyObject* self, PyObject* args)
 {
@@ -58,6 +129,7 @@ static PyObject* py_edge(PyObject* self, PyObject* args)
 
 static PyMethodDef myModule_methods[] = {
        {"edge", py_edge, METH_VARARGS},
 
 static PyMethodDef myModule_methods[] = {
        {"edge", py_edge, METH_VARARGS},
+       {"hough", py_hough, METH_VARARGS},
        {NULL, NULL}
 };
 
        {NULL, NULL}
 };