4 /* TODO delete or document this
7 static PyObject* py_combine(PyObject* self, PyObject* args)
9 const unsigned char *im_bg;
10 const unsigned char *im_fg;
17 if (!PyArg_ParseTuple(args, "s#s#", &im_bg, &size, &im_fg, &size)) return NULL;
21 for (i=0; i < size; i++) {
28 return Py_BuildValue("d", ((double) sum) / area);
33 * Takes dimentions of the image, the image, initial angle and TODO dt is what?.
34 * Computes Hough transform of the image. TODO size etc.
37 static PyObject* py_hough(PyObject* self, PyObject* args)
39 const unsigned char *image;
56 unsigned char *n_image;
59 if (!PyArg_ParseTuple(args, "(ii)s#dd", &x, &y, &image, &size, &init_angle, &dt)) return NULL;
60 // x and y are width and height of the image as ints
61 // Python sends image as (byte)string and since it is not null-terminated, must send its size
62 // init_angle and dt are doubles
65 matrix = (int*) malloc(size * sizeof(int));
66 for (i=0; i < x * y; i++) {
72 for (i=0; i < x; i++) {
73 for (j=0; j < y; j++) {
74 if (image[j * x + i]){
75 for (a=0; a < y; a++){
76 distance = (((i - x / 2) * sin((dt * a) + init_angle)) +
77 ((j - y / 2) * -cos((dt * a) + init_angle)) +
79 column = (int) round(distance);
80 if ((0 <= column) && (column < x)){
81 matrix[a * x + column]++;
91 n_image = (char*) malloc(size * sizeof(char));
94 for (i=1; i < x * y; i++){
95 if (matrix[i] < minimum) minimum = matrix[i];
96 if (matrix[i] > maximum) maximum = matrix[i];
98 maximum = maximum - minimum + 1;
99 for (i=0; i < x * y; i++){
100 n_image[i] = (char) ((((float) (matrix[i] - minimum)) / maximum) * 256);
105 result = Py_BuildValue("s#", n_image, size);
112 * Takes image size, the image, and the size of TODO what?
114 static PyObject* py_edge(PyObject* self, PyObject* args)
116 const unsigned char *image;
125 unsigned char *n_image;
128 if (!PyArg_ParseTuple(args, "(ii)s#", &x, &y, &image, &size)) return NULL;
129 // x and y are width and height of the image as ints
130 // Python sends image as (byte)string and since it is not null-terminated, must send its size
132 n_image = (char*) malloc(size);
133 for (i=0; i < 2 * x; i++) {
135 n_image[(y - 2) * x + i] = 0;
137 for (i=0; i < y; i++) {
139 n_image[x * i + 1] = 0;
140 n_image[x * i + x - 2] = 0;
141 n_image[x * i + x - 1] = 0;
146 for (i=2; i < x - 2; i++) {
147 for (j=2; j < y - 2; j++) {
148 sum = image[x * j + i - 2] + image[x * j + i - 1] + image[x * j + i + 1] + image[x * j + i + 2] +
149 image[x * (j - 2) + i - 2] + image[x * (j - 2) + i - 1] + image[x * (j - 2) + i] +
150 image[x * (j - 2) + i + 1] + image[x * (j - 2) + i + 2] +
151 image[x * (j - 1) + i - 2] + image[x * (j - 1) + i - 1] + image[x * (j - 1) + i] +
152 image[x * (j - 1) + i + 1] + image[x * (j - 1) + i + 2] +
153 image[x * (j + 2) + i - 2] + image[x * (j + 2) + i - 1] + image[x * (j + 2) + i] +
154 image[x * (j + 2) + i + 1] + image[x * (j + 2) + i + 2] +
155 image[x * (j + 1) + i - 2] + image[x * (j + 1) + i - 1] + image[x * (j + 1) + i] +
156 image[x * (j + 1) + i + 1] + image[x * (j + 1) + i + 2]
157 - (24 * image[x * j + i]);
158 if (sum < 0) sum = 0;
159 if (sum > 255) sum = 255;
160 n_image[x * j + i] = sum;
166 result = Py_BuildValue("s#", n_image, size);
172 static PyMethodDef myModule_methods[] = {
173 {"combine", py_combine, METH_VARARGS},
174 {"edge", py_edge, METH_VARARGS},
175 {"hough", py_hough, METH_VARARGS},
181 (void) Py_InitModule("pcf", myModule_methods);