-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhough.c
More file actions
1702 lines (1353 loc) · 49.8 KB
/
Copy pathhough.c
File metadata and controls
1702 lines (1353 loc) · 49.8 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Programmed by: Robert Ainsley McLaughlin
* E-mail: ram@ee.uwa.edu.au
* Date: 18 August, 1996
* Organization: The Center For Intelligent Information Processing Systems, Dept. Electrical & Electronic Engineering, The University of Western Australia,
* Nedlands W.A. 6907, Australia
*
* The original code was refactored. It serves to detect ellipses over binary images and PBM format.
* Modified by: Edwin Delgado Huaynalaya
* E-mail: edychrist@gmail.com
* Last modified: February, 2012
* Organization: Institute of Mathematics and Statistics, University of São Paulo,
* São Paulo, Brazil
*
* Main function: double* ellipse_hough_transform(char filename[],int window_size, double sample_proportion_for_local_max, int min_local_max_value,
* double min_ellipse_proportion, int max_ellipse, int min_ellipse, double min_dist)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <string.h>
#include <values.h> /* MAXINT */
#include <strings.h>
#include <stdarg.h>
#include <time.h>
extern double sqrarg;
#define SQR(a) ((sqrarg=(double )(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)
extern double absarg;
#define ABS(a) ((absarg=(a)) > 0 ? absarg : -absarg)
double sqrarg; /* Used in the macro SQR() */
double absarg; /* Used in the macro ABS() */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define WHITE 63
#define BLACK 0
#define NO_ANGLE -100
#define A_SMALL_NEIGHBOURHOOD (2*8)
#define alloc_vector_MACRO(dim) ((double *)malloc(( (unsigned int)(dim+1) ) * sizeof(double)) )
typedef int BOOLEAN;
typedef struct hough_point_type {
int x, y;
double theta, slope;
BOOLEAN valid_angle; /* is the value in theta, slope valid */
struct hough_point_type *next;
} HOUGH_POINT;
typedef struct hough_point_list_type {
HOUGH_POINT *head;
HOUGH_POINT *tail;
int num_elements; /* number of elements in linked list. */
} HOUGH_POINT_LIST;
typedef struct point_type {
int dim;
double *x; /* indexed [1..dim] */
BOOLEAN flag; /* generic flag - to be used for whatever
* seems important at the time.
*/
struct point_type *next;
struct point_type *next_list;
} POINT;
typedef struct point_list_type {
POINT *head;
POINT *tail;
int num_elements; /* number of elements in linked list. */
struct point_list_type *next_list;
} POINT_LIST;
typedef struct ellipse_type {
/* An ellipse centred on (x,y)
* with major axis of length a at angle theta
* and minor axis of length b.
*
* If the co-ordinate system were translated
* by (x, y) and then rotated by theta,
* so as to centre the ellipse on the origin and
* bring the major axis in line with the x-axis,
* then the ellipse would be defined by the points:
* { (x,y) : (x/a)^2 + (y/b)^2 = 1}
*
* note: When I refer to the major axis, I mean
* the largest radius of the ellipse.
* The minor axis refers to the smallest
* radius of the ellipse.
*/
double x, y;
double a, b, theta;
struct ellipse_type *next;
} ELLIPSE;
typedef struct image_type {
unsigned int x, y; /* dimensions of image
* Allowable values for x are [0, x)
* y are [0, y)
*/
unsigned char **data; /* The pixel values for the image.
* All values are in the range [0,63].
*/
} IMAGE;
int
get_pixel_value(IMAGE *aImage, int x, int y)
{
/* The lower 6 bits specify the value of the pixel.
* The upper 2 bits are used for tagging a pixel.
*/
return((int )(aImage->data[x][y] & (unsigned char )0x3F));
} /* end of get_pixel_value() */
HOUGH_POINT *
alloc_hough_point(void)
{
HOUGH_POINT *hp;
hp = (HOUGH_POINT *)malloc(sizeof(HOUGH_POINT));
if (hp == NULL)
{
fprintf(stderr, "malloc failed in alloc_hough_point() in sht_ellipse.c\n");
exit(1);
}
hp->next = NULL;
return(hp);
} /* end of alloc_hough_point() */
HOUGH_POINT_LIST *
alloc_hough_point_list(void)
{
HOUGH_POINT_LIST *hough_point_list;
hough_point_list = (HOUGH_POINT_LIST *)malloc( sizeof(HOUGH_POINT_LIST) );
if (hough_point_list == NULL)
{
fprintf(stderr, "malloc failed in alloc_hough_point_list() in sht_ellipse.c\n");
exit(1);
}
hough_point_list->head = NULL;
hough_point_list->tail = NULL;
hough_point_list->num_elements = 0;
return(hough_point_list);
} /* end of alloc_hough_point_list() */
POINT *
alloc_point(int dim)
{
POINT *p;
p = (POINT *)malloc(sizeof(POINT));
if (p == NULL)
{
fprintf(stderr, "malloc failed in alloc_point() in point.c\n");
exit(1);
}
p->dim = dim;
p->x = alloc_vector_MACRO(dim);
if (p->x == NULL)
{
fprintf(stderr, "malloc failed in alloc_point() in point.c\n");
exit(1);
}
p->flag = FALSE;
p->next = NULL;
p->next_list = NULL;
return(p);
} /* end of alloc_point() */
POINT_LIST *
put_in_linked_list_of_point(POINT_LIST *point_list, POINT *p)
{
POINT_LIST *alloc_point_list(void);
if (point_list == NULL)
point_list = alloc_point_list();
if (point_list->head == NULL)
{
point_list->head = p;
point_list->tail = p;
point_list->num_elements = 1;
}
else
{
/* Add p to the list.
*/
point_list->tail->next = p;
/* Update the tail. */
point_list->tail = p;
/* Update the number of elements. */
(point_list->num_elements)++;
}
p->next = NULL;
return(point_list);
} /* end of put_in_linked_list_of_point() */
POINT_LIST *
alloc_point_list(void)
{
POINT_LIST *point_list;
point_list = (POINT_LIST *)malloc( sizeof(POINT_LIST) );
if (point_list == NULL)
{
fprintf(stderr, "malloc failed in alloc_point_list() in point.c\n");
exit(1);
}
point_list->head = NULL;
point_list->tail = NULL;
point_list->num_elements = 0;
point_list->next_list = NULL;
return(point_list);
} /* end of alloc_point_list() */
ELLIPSE *
alloc_ellipse(void)
{
ELLIPSE *e;
e = (ELLIPSE *)malloc(sizeof(ELLIPSE));
if (e == NULL)
{
fprintf(stderr, "malloc failed in alloc_ellipse() in sht_ellipse.c\n");
exit(1);
}
e->next = NULL;
return(e);
} /* end of alloc_ellipse() */
ELLIPSE *
find_other_three_ellipse_parameters(POINT_LIST *centre_point_list, IMAGE *aImage, double min_ellipse_proportion, int max_ellipse, int min_ellipse, double min_dist)
{
double a_start, a_end, b_start, b_end, theta_start, theta_end;
double da, db, dtheta;
int a_index, b_index, theta_index;
double a_max, b_max, theta_max;
int i;
ELLIPSE *ellipse, *ellipse_list;
int ***histogram;
POINT *centre_point;
double proportion;
int *** alloc_3D_histogram(int x_max, int y_max, int z_max);
void other_three_parameters_SHT_hough_transform(int ***histogram, POINT *centre_point, double a_start, double a_end, double b_start, double b_end, double theta_start, double theta_end, IMAGE *aImage);
void find_max_of_3D_histogram(int ***histogram, int *x_val, int *y_val, int *z_val, int x_max, int y_max, int z_max);
void free_3D_histogram(int ***histogram, int x_max, int y_max);
double find_proportion_of_ellipse_that_exists(double x_e, double y_e, double a, double b, double theta, IMAGE *aImage, double min_dist);
ELLIPSE *alloc_ellipse(void);
ELLIPSE *put_in_linked_list_of_ellipse(ELLIPSE *head, ELLIPSE *e);
double a_over_b;
if ((centre_point_list == (POINT_LIST *)NULL) || (centre_point_list->head == (POINT *)NULL))
return( (ELLIPSE *)NULL );
ellipse_list = NULL;
histogram = alloc_3D_histogram(20, 20, 20);
for (centre_point=centre_point_list->head; centre_point != NULL;
centre_point=centre_point->next)
{
a_start = b_start = 0.0;
/* a_end, b_end define the size of the
* largest ellipse that we can find.
*
* We have set this largest ellipse to have a
* major radius 1/2 as large as the diagonal of the image.
*/
a_end = b_end = max_ellipse / 2.0; //sqrt(SQR(aImage->x) + SQR(aImage->y)) / 2.0;
theta_start = 0.0; theta_end = M_PI;
for (i=0; i < 5; i++)
{
other_three_parameters_SHT_hough_transform(histogram, centre_point, a_start, a_end, b_start, b_end, theta_start, theta_end, aImage);
find_max_of_3D_histogram(histogram, &a_index, &b_index, &theta_index, 20, 20, 20);
da = ((double )(a_end-a_start)) / 20;
db = ((double )(b_end-b_start)) / 20;
dtheta = ((double )(theta_end-theta_start)) / 20;
a_max = a_start + da*(double )a_index;
b_max = b_start + db*(double )b_index;
theta_max = theta_start + dtheta*(double )theta_index;
a_start = a_max - da;
if (a_start < 0)
a_start = 0;
a_end = a_max + da;
b_start = b_max - db;
if (b_start < 0)
b_start = 0;
b_end = b_max + db;
theta_start = theta_max - dtheta;
theta_end = theta_max + dtheta;
#ifdef COMPUTATION_TIME
check_parity();
#endif
}
/* Having found the parameters for an ellipse,
* we need to check how much of the ellipse exists.
* If only a small bit of it exists, then the ellipse
* probably does not exist.
* We are not talking about occluded ellipses here.
* We are trying to detect when the hough transform
* has taken a few black pixels from around the image
* and interpretted them as bits of an ellipse.
*/
proportion = find_proportion_of_ellipse_that_exists(centre_point->x[1], centre_point->x[2], a_max, b_max, theta_max, aImage, min_dist);
if(a_max != 0 && b_max != 0){
if(a_max > b_max){
a_over_b = a_max / b_max;
} else {
a_over_b = b_max / a_max;
}
} else {
a_over_b = 0;
}
if ( (proportion > min_ellipse_proportion) && (a_max >= min_ellipse/2.0 && b_max >= min_ellipse/2.0 ) && a_over_b >= 1.50 )
{
ellipse = alloc_ellipse();
ellipse->x = centre_point->x[1];
ellipse->y = centre_point->x[2];
ellipse->a = a_max;
ellipse->b = b_max;
ellipse->theta = theta_max;
ellipse_list = put_in_linked_list_of_ellipse(ellipse_list, ellipse);
}
#ifdef COMPUTATION_TIME
check_parity();
#endif
}
free_3D_histogram(histogram, 20, 20);
return(ellipse_list);
} /* end of find_other_three_ellipse_parameters */
void
free_3D_histogram(int ***histogram, int x_max, int y_max)
{
int x, y;
for (x=0; x < x_max; x++)
{
for (y=0; y < y_max; y++)
free(histogram[x][y]);
free(histogram[x]);
}
free(histogram);
} /* end of free_3D_histogram() */
int ***
alloc_3D_histogram(int x_max, int y_max, int z_max)
{
int x, y;
int ***histogram;
/* Allocate space for a 3D histogram
* histogram is an array of pointers,
* each of which points to an array of pointers,
* each of which points to an array of data.
* So, histogram is a (int ***)
* histogram[] is a (int **)
* histogram[][] is a (int *)
* histogram[][][] is a (int )
*/
histogram = (int ***)malloc((int )x_max * sizeof(int **));
if (histogram == NULL)
{
fprintf(stderr, "first malloc failed in alloc_3D_histogram() in sht_ellipse.c\n");
exit(1);
}
for (x=0; x < x_max; x++)
{
histogram[x] = (int **)malloc((int )y_max * sizeof(int *));
if (histogram[x] == NULL)
{
fprintf(stderr, "second malloc failed in alloc_3D_histogram() in sht_ellipse.c\n");
exit(1);
}
for (y=0; y < y_max; y++)
{
histogram[x][y] = (int *)malloc((int )z_max * sizeof(int ));
if (histogram[x][y] == NULL)
{
fprintf(stderr, "third malloc failed in alloc_3D_histogram() in sht_ellipse.c\n");
exit(1);
}
}
}
return(histogram);
} /* end of alloc_3D_histogram() */
void
other_three_parameters_SHT_hough_transform(int ***histogram, POINT *centre_point, double a_start, double a_end, double b_start, double b_end, double theta_start, double theta_end, IMAGE *aImage)
{
int x, y;
double x1, y1, x2, y2;
double a, b, theta, da, db, dtheta;
int a_index, b_index, theta_index;
int get_pixel_value(IMAGE *aImage, int x, int y);
/* Initialise all histogram values
* to zero.
*/
for (a_index=0; a_index < 20; a_index++)
for (b_index=0; b_index < 20; b_index++)
for (theta_index=0; theta_index < 20; theta_index++)
histogram[a_index][b_index][theta_index] = 0;
da = ((double )(a_end-a_start)) / 20;
db = ((double )(b_end-b_start)) / 20;
dtheta = ((double )(theta_end-theta_start)) / 20;
for (x=0; x < aImage->x; x++)
for (y=0; y < aImage->y; y++)
if (get_pixel_value(aImage,x,y) == BLACK)
{
/* Translate (x,y) relative to centre of ellipse.
*/
x1= ((double )x)-centre_point->x[1];
y1 = ((double )y)-centre_point->x[2];
for (a_index=0, a=a_start; a_index < 20; a_index++, a+=da)
for (theta_index=0, theta=theta_start; theta_index < 20; theta_index++, theta+=dtheta)
{
/* Given a and theta, calculate b
*/
/* Rotate (x1,y1) relative to theta.
*/
x2 = x1*cos(theta) + y1*sin(theta);
y2 = -x1*sin(theta) + y1*cos(theta);
if (SQR(x2/a) < 1.0) /* avoid divide by zero */
{
b = sqrt( SQR(y2) / (1.0 - SQR(x2/a)) );
b_index = (int )((b-b_start) / db);
if ((b_index >= 0) && (b_index < 20))
histogram[a_index][b_index][theta_index]++;
}
}
}
} /* end of other_three_parameters_SHT_hough_transform() */
void
find_max_of_3D_histogram(int ***histogram, int *x_val, int *y_val, int *z_val, int x_max, int y_max, int z_max)
{
int x, y, z;
int max_val;
*x_val = 0; *y_val = 0; *z_val = 0;
max_val = 0;
for (x=0; x < x_max; x++)
for (y=0; y < y_max; y++)
for (z=0; z < z_max; z++)
if (histogram[x][y][z] > max_val)
{
max_val = histogram[x][y][z];
*x_val = x;
*y_val = y;
*z_val = z;
}
} /* end of find_max_of_3D_histogram() */
double
find_proportion_of_ellipse_that_exists(double x_e, double y_e, double a, double b, double theta, IMAGE *aImage, double min_dist)
{
int x, y;
double x1, y1, x2, y2;
double count = 0.0;
double perimeter;
int get_pixel_value(IMAGE *aImage, int x, int y);
for (x=0; x < aImage->x; x++)
for (y=0; y < aImage->y; y++)
if (get_pixel_value(aImage,x,y) == BLACK)
{
x1 = (double )x - x_e; /* translate pixel relative */
y1 = (double )y - y_e; /* to ellipse centre. */
/* Rotate (x1,y1) relative to theta.
*/
x2 = x1*cos(theta) + y1*sin(theta);
y2 = -x1*sin(theta) + y1*cos(theta);
/* Scale it relative to a, b
*/
x2 /= a;
y2 /= b;
/* If (x,y) lies on or near the ellipse,
* then (x2,y2) will lie on or near
* the unit circle.
*/
if (ABS(1.0 - (SQR(x2) + SQR(y2))) < min_dist)
{
count++;
}
}
/* Note: we multiply the ellipse perimeter by the current
* line thickness.
*/
perimeter = M_PI * ( 3*(a+b) - sqrt((a+3*b)*(3*a+b)) ) * 2;
return(count/perimeter);
} /* end of find_proportion_of ellipse_that_exists() */
ELLIPSE *
put_in_linked_list_of_ellipse(ELLIPSE *head, ELLIPSE *e)
{
ELLIPSE *ptr;
if (head == NULL)
head = e;
else
{
for (ptr=head; ptr->next != NULL; ptr=ptr->next)
;
ptr->next = e;
}
e->next = NULL;
return(head);
} /* end of put_in_linked_list_of_ellipse() */
void
free_linked_list_of_point(POINT_LIST *point_list)
{
POINT *p1, *p2;
void free_point(POINT *p);
if (point_list != NULL)
{
p1 = point_list->head;
while (p1 != NULL)
{
p2 = p1;
p1 = p1->next;
free_point(p2);
}
free(point_list);
}
} /* end of free_linked_list_of_point() */
void
free_point(POINT *p)
{
if (p != NULL)
{
if (p->x != NULL)
free(p->x);
free(p);
}
} /* end of free_point() */
void
average_histogram(int **histogram, int x_max, int y_max)
{
int **temp_histogram;
int x, y, i, j;
int sum;
int **alloc_histogram(int x_max, int y_max);
temp_histogram = alloc_histogram(x_max, y_max);
for (x=0; x < x_max; x++)
for (y=0; y < y_max; y++)
temp_histogram[x][y] = histogram[x][y];
for (x=1; x < x_max-1; x++)
for (y=1; y < y_max-1; y++)
{
sum = 0;
for (i=x-1; i <= x+1; i++)
for (j=y-1; j <= y+1; j++)
sum += temp_histogram[i][j];
histogram[x][y] = sum / 9;
}
} /* end of average_histogram() */
void
free_hough_point(HOUGH_POINT *hp)
{
/* Admittedly, this function is a bit of a waste of time,
* but I put it in since I have a free function for
* my other data structures, e.g.
* free_gaus(), free_histogram(), free_point(), free_basis(), free_image()
*/
free(hp);
} /* end of free_hough_point() */
void
free_linked_list_of_hough_point(HOUGH_POINT_LIST *hough_point_list)
{
HOUGH_POINT *hp1, *hp2;
void free_hough_point(HOUGH_POINT *p);
if (hough_point_list != NULL)
{
hp1 = hough_point_list->head;
while (hp1 != NULL)
{
hp2 = hp1;
hp1 = hp1->next;
free_hough_point(hp2);
}
free(hough_point_list);
}
} /* end of free_linked_list_of_hough_point() */
double
find_tangent_of_edge(IMAGE *aImage, int x_p, int y_p)
{
int num_pixels;
int x, y, x_min, x_max, y_min, y_max;
int sum1, sum2;
double slope, theta;
int get_pixel_value(IMAGE *aImage, int x, int y);
x_min = x_p - A_SMALL_NEIGHBOURHOOD;
if (x_min < 0)
x_min = 0;
x_max = x_p + A_SMALL_NEIGHBOURHOOD;
if (x_max >= aImage->x)
x_max = aImage->x-1;
y_min = y_p - A_SMALL_NEIGHBOURHOOD;
if (y_min < 0)
y_min = 0;
y_max = y_p + A_SMALL_NEIGHBOURHOOD;
if (y_max >= aImage->y)
y_max = aImage->y-1;
num_pixels = 0;
sum1 = sum2 = 0;
for (x=x_min; x <= x_max; x++)
for (y=y_min; y <= y_max; y++)
if (get_pixel_value(aImage, x, y) == BLACK)
{
num_pixels++;
sum1 += (x-x_p)*(y-y_p);
sum2 += (x-x_p)*(x-x_p);
}
/* The slope of the best line through the
* black pixels in this neighbourhood
* is sum1 / sum2.
* We will convert this to an angle.
*/
if (num_pixels == 1)
{
/* If there was only one black pixel
* i.e. at co-ord (x_p, y_p)
* In that case, we can't tell what the
* angle is at that point, so we will
* return the value NO_ANGLE
*/
theta = NO_ANGLE;
}
else if (sum2 == 0)
{
/* If all points were on a vertical line.
* In this case, set the angle to M_PI_2 radians.
*/
theta = M_PI_2;
}
else
{
slope = ((double )sum1) / (double )sum2;
theta = atan(slope);
}
return(theta);
} /* end of find_tangent_of_edge() */
HOUGH_POINT_LIST *
put_in_linked_list_of_hough_point(HOUGH_POINT_LIST *hough_point_list,
HOUGH_POINT *hp)
{
HOUGH_POINT_LIST *alloc_hough_point_list(void);
if (hough_point_list == NULL)
hough_point_list = alloc_hough_point_list();
if (hough_point_list->head == NULL)
{
hough_point_list->head = hp;
hough_point_list->tail = hp;
hough_point_list->num_elements = 1;
}
else
{
/* Add hp to the list.
*/
hough_point_list->tail->next = hp;
/* Update the tail. */
hough_point_list->tail = hp;
/* Update the number of elements. */
(hough_point_list->num_elements)++;
}
hp->next = NULL;
return(hough_point_list);
} /* end of put_in_linked_list_of_hough_point() */
IMAGE *
load_pbm_image(char filename[])
{
IMAGE *aImage;
FILE *infile;
char magic_num[10];
int width, height, x, y, data, i;
unsigned char data_raw, value;
int check_for_comment(FILE *infile);
IMAGE *alloc_IMAGE(int x, int y);
void set_IMAGE_pixel_value(IMAGE *aImage, int x, int y, int val);
infile = fopen(filename, "r");
if (infile == NULL)
{
fprintf(stderr, "Error opening file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if (check_for_comment(infile) == EOF)
{
fprintf(stderr, "Error reading file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if (fscanf(infile, "%[P14]", magic_num) != 1)
{
fprintf(stderr, "Error reading magic number of file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if ((strlen(magic_num) != 2) ||
((magic_num[1] != '1') && (magic_num[1] != '4')))
{
fprintf(stderr, "Error reading magic number of file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if (check_for_comment(infile) == EOF)
{
fprintf(stderr, "Error reading file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if (fscanf(infile, "%d", &width) != 1)
{
fprintf(stderr, "Error reading width in file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if (check_for_comment(infile) == EOF)
{
fprintf(stderr, "Error reading file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if (fscanf(infile, "%d", &height) != 1)
{
fprintf(stderr, "Error reading height in file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
aImage = alloc_IMAGE(width, height);
/* Read in the data
*/
if (magic_num[1] == '1')
{
if (check_for_comment(infile) == EOF)
return((IMAGE *)NULL);
for (y=0; y < height; y++)
{
/* Read in a line of data.
*/
for (x=0; x < width; x++)
{
if (fscanf(infile, "%d", &data) != 1)
{
fprintf(stderr, "Error reading data from file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
if (data == 1)
set_IMAGE_pixel_value(aImage, x, y, BLACK);
else if (data == 0)
set_IMAGE_pixel_value(aImage, x, y, WHITE);
check_for_comment(infile);
}
}
}
else if (magic_num[1] == '4')
{
/* Discard white space after height.
*/
fscanf(infile, "%*c");
for (y=0; y < height; y++)
{
/* Read in a line of data.
*/
for (x=0; x < width; x+=8)
{
if (fscanf(infile, "%c", &data_raw) != 1)
{
fprintf(stderr, "Error reading data from file %s in load_pbm_image() in image.c\n", filename);
exit(1);
}
/* Extract each bit as the value for a pixel.
*/
for (i=0; i < 8; i++)
{
value = data_raw >> (7-i);
value = value & 0x01; /* Mask 00000001 */
if (value == (unsigned char )1)
set_IMAGE_pixel_value(aImage, x+i, y, BLACK);
else if (value == (unsigned char )0)
set_IMAGE_pixel_value(aImage, x+i, y, WHITE);
}
}
}
}
return(aImage);
} /* end of load_pbm_image() */
int
check_for_comment(FILE *infile)
{
unsigned char c;
/* Remove trailing white characters.
*/
do
c = getc(infile);
while ((c == '\n') || (c == ' '));
/* If line begins with #
* discard it - it is a comment.
*/
while (c == '#')
{
/* Read in the rest of the line as comment
*/
do
{
c = (unsigned char )getc(infile);
}
while ((c != (unsigned char )'\n') && (c != (unsigned char )EOF));
c = getc(infile); /* to check if next line begins with # */
}
/* When testing to see if the next line began with a #,
* we read one too many characters.