summaryrefslogtreecommitdiff
path: root/im/src/process/im_houghline.cpp
blob: 6ead98224fa3ed022dbf64e90073e2299ebfa3bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/** \file
 * \brief Hough Transform
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_houghline.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $
 */

#include <im.h>
#include <im_util.h>
#include <im_complex.h>
#include <im_convert.h>
#include <im_counter.h>

#include "im_process_glo.h"

#include <stdlib.h>
#include <memory.h>


#ifndef M_PI
#define M_PI    3.14159265358979323846
#endif

static double *costab=NULL, *sintab=NULL;

static int hgAbs(int x)
{
  return x < 0? -x: x;
}

typedef struct _point
{
  int rho, theta, count;
} point;

typedef struct _listnode
{
  struct _listnode *next;
  point pt;
} listnode;

static listnode* listnew(point *pt)
{
  listnode* node = (listnode*)malloc(sizeof(listnode));
  node->next = NULL;
  node->pt = *pt;
  return node;
}

static listnode* listadd(listnode* node, point *pt)
{
  node->next = listnew(pt);
  return node->next;
}

/* minimum angle to match similar angles */
#define THETA_DELTA1   0.05  /* radians */
#define THETA_DELTA2   3     /* degrees */

static int ptNear(point* pt1, point* pt2, int rho_delta)
{
  int theta_diff = hgAbs(pt1->theta - pt2->theta);
  if ((hgAbs(pt1->rho - pt2->rho) < rho_delta && theta_diff < THETA_DELTA2) ||
      (hgAbs(pt1->rho + pt2->rho) < rho_delta && 180-theta_diff < THETA_DELTA2))
  {
    if (pt2->count > pt1->count)
      return 2;   /* replace the line */
    else
      return 1;
  }
  else
    return 0;
}

static listnode* listadd_filtered(listnode* list, listnode* cur_node, point *pt, int rho_delta)
{
  int ret;
  listnode* lt = list;
  while (lt)
  {
    ret = ptNear(&lt->pt, pt, rho_delta);
    if (ret)
    {
      if (ret == 2)
        lt->pt = *pt;  /* replace the line */
      return cur_node;
    }
    lt = lt->next;
  }

  cur_node->next = listnew(pt);
  return cur_node->next;
}

/*C* Initial version from XITE

        houghLine
        $Id: im_houghline.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $
        Copyright 1990, Blab, UiO
        Image processing lab, Department of Informatics
        University of Oslo
        E-mail: blab@ifi.uio.no
________________________________________________________________

  houghLine - Hough transform for line detection

  Description:
    Performs a Hough transform to detect lines. Every band in the
    input image 'inimage' is transformed to a two dimensional
    Hough space, a (theta, rho) space.

		After creating the transform, the Hough space may be searched
		for local maxima. Within each band, only the largest local
		maximum (maxima) within a 'ws'x'ws' area is registered.
		Besides, only maxima with number of updates above a limit
		given by the ul option are used.

		updateLimit determines the minimum number of updates for a maximum
		to be used. The minimum number is determined from 'updateLimit'
		and the size of the hough space image:
		| updateLimit * MAX(horizontal size, vertical size)
		Default: 0.1.

    All pixels above zero in the 'input' band are
		transformed to (theta,rho) space in the 'output'
		band. The 'input' band may have any size, while
		the 'output' band currently must be at least
		| xsize: 180
		| ysize: 2 * sqrt(inputXsize*inputXsize +
		|             inputYsize*inputYsize) + 1

		Notice that band x coordinates 1..180 correspond
		to angles theta = 0 .. 179, and y coordinates
		1..YSIZE correspond to rho = -(ysize/2) .. ysize/2.

  Restrictions:
    'input' must have pixel type imbyte.
    'output' must have pixel type int.

  Author:		Tor Lønnestad, BLAB, Ifi, UiO
*/


static int houghLine(const imImage* input, imImage* output, int counter)
{
  int ixsize, iysize, ixhalf, iyhalf, thetamax, x, y, rho, theta, rhomax;
  imbyte *input_map = (imbyte*)input->data[0];
  int *output_map = (int*)output->data[0];

  ixsize = input->width;
  iysize = input->height;
  ixhalf = ixsize/2;
  iyhalf = iysize/2;

  thetamax = output->width;   /* theta max = 180 */
  rhomax = output->height/2;  /* rho shift to 0, -rmax <= r <= +rmax */

  costab = (double*)malloc(thetamax*sizeof(double));
  sintab = (double*)malloc(thetamax*sizeof(double));

  for (theta=0; theta < thetamax; theta++)
  {
    double th = (M_PI*theta)/thetamax;
    costab[theta] = cos(th);
    sintab[theta] = sin(th);
  }

  for (y=0; y < iysize; y++)
  {
    for (x=0; x < ixsize; x++)
    {
      if (input_map[y*ixsize + x])
      {
        for (theta=0; theta < thetamax; theta++)
        {
          rho = imRound((x-ixhalf)*costab[theta] + (y-iyhalf)*sintab[theta]);
          if (rho > rhomax) continue;
          if (rho < -rhomax) continue;
          output_map[(rho+rhomax)*thetamax + theta]++;
	      }
      }
    }

    if (!imCounterInc(counter))
    {
      free(costab); costab = NULL;
      free(sintab); sintab = NULL;
      return 0;
    }
  }

  free(costab); costab = NULL;
  free(sintab); sintab = NULL;

  return 1;
}

static listnode* findMaxima(const imImage* hough_points, int *line_count, const imImage* hough)
{
  int x, y, xsize, ysize, rhomax, offset, rho_delta = 0;
  listnode* maxima = NULL, *cur_node = NULL;
  point pt;
  imbyte *map = (imbyte*)hough_points->data[0];
  int *hough_map = NULL;

  xsize = hough_points->width;   /* X = theta */
  ysize = hough_points->height;  /* Y = rho   */
  rhomax = ysize/2;
  
  if (hough)
  {
    hough_map = (int*)hough->data[0];
    rho_delta = (int)(rhomax*tan(THETA_DELTA1));
  }

  for (y=0; y < ysize; y++)
  {
    for (x=0; x < xsize; x++)
    {
      offset = y*xsize + x;

      if (map[offset])
      {
        pt.theta = x;
        pt.rho = y-rhomax;

        if (!maxima)
        {
          cur_node = maxima = listnew(&pt);
          (*line_count)++;
        }
        else
        {
          if (hough_map)
          {
            listnode* old_node = cur_node;
            pt.count = hough_map[offset];
            cur_node = listadd_filtered(maxima, cur_node, &pt, rho_delta);
            if (cur_node != old_node)
              (*line_count)++;
          }
          else
          {
            cur_node = listadd(cur_node, &pt);
            (*line_count)++;
          }
        }
	    }
    }
  }

  return maxima;
}

#define SWAPINT(a, b) {int t = a; a = b; b = t; }

static void drawLine(imImage* image, int theta, int rho)
{
  int xsize, ysize, xstart, xstop, ystart, ystop, xhalf, yhalf;
  float a, b;
  imbyte *map = (imbyte*)image->data[0];

  xsize = image->width;
  ysize = image->height;
  xhalf = xsize/2;
  yhalf = ysize/2;

  if (theta == 0)  /* vertical line */
  {
    int y;
    if (rho+xhalf < 0 || rho+xhalf > xsize-1) return;
    for (y=0; y < ysize; y++)
      map[y*xsize + rho+xhalf]=254;

    return;
  }

  if (theta == 90)  /* horizontal line */
  {
    int x;
    if (rho+yhalf < 0 || rho+yhalf > ysize-1) return;
    for (x=0; x < xsize; x++)
      map[(rho+yhalf)*xsize + x]=254;

    return;
  }

  a = (float)(-costab[theta]/sintab[theta]);
  b = (float)((rho + xhalf*costab[theta] + yhalf*sintab[theta])/sintab[theta]);

  {
    int x[2];
    int y[2];
    int c = 0;
    int y1 = imRound(b);              /* x = 0 */
    int y2 = imRound(a*(xsize-1)+b);  /* x = xsize-1 */

    int x1 = imRound(-b/a);           /* y = 0 */
    int x2 = imRound((ysize-1-b)/a);  /* y = ysize-1 */

    if (y1 >= 0 && y1 < ysize)
    {
      y[c] = y1;
      x[c] = 0;
      c++;
    }

    if (y2 >= 0 && y2 < ysize)
    {
      y[c] = y2;
      x[c] = xsize-1;
      c++;
    }

    if (c < 2 && x1 >= 0 && x1 < xsize)
    {
      x[c] = x1;
      y[c] = 0;
      c++;
    }

    if (c < 2 && x2 >= 0 && x2 < xsize)
    {
      x[c] = x2;
      y[c] = ysize-1;
      c++;
    }

    if (c < 2) return;

    ystart = y[0];
    xstart = x[0];
    ystop = y[1];
    xstop = x[1];
  }

  {
    int x, y;
    if (45 <= theta && theta <= 135)
    {
      if (xstart > xstop)
        SWAPINT(xstart, xstop);

      for (x=xstart; x <= xstop; x++)
      {
        y = imRound(a*x + b);
        if (y < 0) continue;
        if (y > ysize-1) continue;
        map[y*xsize + x]=254;
      }
    }
    else
    {
      if (ystart > ystop)
        SWAPINT(ystart, ystop);

      for (y=ystart; y <= ystop; y++)
      {
        x = imRound((y-b)/a);
        if (x < 0) continue;
        if (x > xsize-1) continue;
        map[y*xsize + x]=254;
      }
    }
  }
}

int imProcessHoughLines(const imImage* image, imImage *NewImage)
{
  int counter = imCounterBegin("Hough Line Transform");
  imCounterTotal(counter, image->height, "Processing...");

  int ret = houghLine(image, NewImage, counter);

  imCounterEnd(counter);

  return ret;
}

static void DrawPoints(imImage *image, listnode* maxima)
{
  listnode* cur_node;
  while (maxima)
  {
    cur_node = maxima;
    drawLine(image, cur_node->pt.theta, cur_node->pt.rho);
    maxima = cur_node->next;
    free(cur_node);
  }
}

static void ReplaceColor(imImage* NewImage)
{
  int i;
  imbyte* map = (imbyte*)NewImage->data[0];

  NewImage->color_space = IM_MAP;
  NewImage->palette[254] = imColorEncode(255, 0, 0);

  for (i = 0; i < NewImage->count; i++)
  {
    if (map[i] == 254)
      map[i] = 255;
  }
}

int imProcessHoughLinesDraw(const imImage* original_image, const imImage *hough, const imImage *hough_points, imImage *NewImage)
{
  int theta, line_count = 0;

  if (original_image != NewImage)
    imImageCopyData(original_image, NewImage);

  listnode* maxima = findMaxima(hough_points, &line_count, hough);

  ReplaceColor(NewImage);

  costab = (double*)malloc(180*sizeof(double));
  sintab = (double*)malloc(180*sizeof(double));

  for (theta=0; theta < 180; theta++)
  {
    double th = (M_PI*theta)/180.;
    costab[theta] = cos(th);
    sintab[theta] = sin(th);
  }

  DrawPoints(NewImage, maxima);

  free(costab); costab = NULL;
  free(sintab); sintab = NULL;

  return line_count;
}