-
Notifications
You must be signed in to change notification settings - Fork 0
/
susan2l.c
2122 lines (1788 loc) · 58.4 KB
/
susan2l.c
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
/* {{{ Copyright etc. */
/**********************************************************************\
SUSAN Version 2l by Stephen Smith
Oxford Centre for Functional Magnetic Resonance Imaging of the Brain,
Department of Clinical Neurology, Oxford University, Oxford, UK
(Previously in Computer Vision and Image Processing Group - now
Computer Vision and Electro Optics Group - DERA Chertsey, UK)
Email: steve@fmrib.ox.ac.uk
WWW: http://www.fmrib.ox.ac.uk/~steve
(C) Crown Copyright (1995-1999), Defence Evaluation and Research Agency,
Farnborough, Hampshire, GU14 6TD, UK
DERA WWW site:
http://www.dera.gov.uk/
DERA Computer Vision and Electro Optics Group WWW site:
http://www.dera.gov.uk/imageprocessing/dera/group_home.html
DERA Computer Vision and Electro Optics Group point of contact:
Dr. John Savage, jtsavage@dera.gov.uk, +44 1344 633203
A UK patent has been granted: "Method for digitally processing
images to determine the position of edges and/or corners therein for
guidance of unmanned vehicle", UK Patent 2272285. Proprietor:
Secretary of State for Defence, UK. 15 January 1997
This code is issued for research purposes only and remains the
property of the UK Secretary of State for Defence. This code must
not be passed on without this header information being kept
intact. This code must not be sold.
\**********************************************************************/
/* }}} */
/* {{{ Readme First */
/**********************************************************************\
SUSAN Version 2l
SUSAN = Smallest Univalue Segment Assimilating Nucleus
Email: steve@fmrib.ox.ac.uk
WWW: http://www.fmrib.ox.ac.uk/~steve
Related paper:
@article{Smith97,
author = "Smith, S.M. and Brady, J.M.",
title = "{SUSAN} - A New Approach to Low Level Image Processing",
journal = "Int. Journal of Computer Vision",
pages = "45--78",
volume = "23",
number = "1",
month = "May",
year = 1997}
To be registered for automatic (bug) updates of SUSAN, send an email.
Compile with:
gcc -O4 -o susan susan2l.c -lm
See following section for different machine information. Please
report any bugs (and fixes). There are a few optional changes that
can be made in the "defines" section which follows shortly.
Usage: type "susan" to get usage. Only PGM format files can be input
and output. Utilities such as the netpbm package and XV can be used
to convert to and from other formats. Any size of image can be
processed.
This code is written using an emacs folding mode, making moving
around the different sections very easy. This is why there are
various marks within comments and why comments are indented.
SUSAN QUICK:
This version of the SUSAN corner finder does not do all the
false-corner suppression and thus is faster and produced some false
positives, particularly on strong edges. However, because there are
less stages involving thresholds etc., the corners that are
correctly reported are usually more stable than those reported with
the full algorithm. Thus I recommend at least TRYING this algorithm
for applications where stability is important, e.g., tracking.
THRESHOLDS:
There are two thresholds which can be set at run-time. These are the
brightness threshold (t) and the distance threshold (d).
SPATIAL CONTROL: d
In SUSAN smoothing d controls the size of the Gaussian mask; its
default is 4.0. Increasing d gives more smoothing. In edge finding,
a fixed flat mask is used, either 37 pixels arranged in a "circle"
(default), or a 3 by 3 mask which gives finer detail. In corner
finding, only the larger 37 pixel mask is used; d is not
variable. In smoothing, the flat 3 by 3 mask can be used instead of
a larger Gaussian mask; this gives low smoothing and fast operation.
BRIGHTNESS CONTROL: t
In all three algorithms, t can be varied (default=20); this is the
main threshold to be varied. It determines the maximum difference in
greylevels between two pixels which allows them to be considered
part of the same "region" in the image. Thus it can be reduced to
give more edges or corners, i.e. to be more sensitive, and vice
versa. In smoothing, reducing t gives less smoothing, and vice
versa. Set t=10 for the test image available from the SUSAN web
page.
ITERATIONS:
With SUSAN smoothing, more smoothing can also be obtained by
iterating the algorithm several times. This has a different effect
from varying d or t.
FIXED MASKS:
37 pixel mask: ooo 3 by 3 mask: ooo
ooooo ooo
ooooooo ooo
ooooooo
ooooooo
ooooo
ooo
CORNER ATTRIBUTES dx, dy and I
(Only read this if you are interested in the C implementation or in
using corner attributes, e.g., for corner matching)
Corners reported in the corner list have attributes associated with
them as well as positions. This is useful, for example, when
attempting to match corners from one image to another, as these
attributes can often be fairly unchanged between images. The
attributes are dx, dy and I. I is the value of image brightness at
the position of the corner. In the case of susan_corners_quick, dx
and dy are the first order derivatives (differentials) of the image
brightness in the x and y directions respectively, at the position
of the corner. In the case of normal susan corner finding, dx and dy
are scaled versions of the position of the centre of gravity of the
USAN with respect to the centre pixel (nucleus).
BRIGHTNESS FUNCTION LUT IMPLEMENTATION:
(Only read this if you are interested in the C implementation)
The SUSAN brightness function is implemented as a LUT
(Look-Up-Table) for speed. The resulting pointer-based code is a
little hard to follow, so here is a brief explanation. In
setup_brightness_lut() the LUT is setup. This mallocs enough space
for *bp and then repositions the pointer to the centre of the
malloced space. The SUSAN function e^-(x^6) or e^-(x^2) is
calculated and converted to a uchar in the range 0-100, for all
possible image brightness differences (including negative
ones). Thus bp[23] is the output for a brightness difference of 23
greylevels. In the SUSAN algorithms this LUT is used as follows:
p=in + (i-3)*x_size + j - 1;
p points to the first image pixel in the circular mask surrounding
point (x,y).
cp=bp + in[i*x_size+j];
cp points to a position in the LUT corresponding to the brightness
of the centre pixel (x,y).
now for every pixel within the mask surrounding (x,y),
n+=*(cp-*p++);
the brightness difference function is found by moving the cp pointer
down by an amount equal to the value of the pixel pointed to by p,
thus subtracting the two brightness values and performing the
exponential function. This value is added to n, the running USAN
area.
in SUSAN smoothing, the variable height mask is implemented by
multiplying the above by the moving mask pointer, reset for each new
centre pixel.
tmp = *dpt++ * *(cp-brightness);
\**********************************************************************/
/* }}} */
/* {{{ Machine Information */
/**********************************************************************\
Success has been reported with the following:
MACHINE OS COMPILER
Sun 4.1.4 bundled C, gcc
Next
SGI IRIX SGI cc
DEC Unix V3.2+
IBM RISC AIX gcc
PC Borland 5.0
PC Linux gcc-2.6.3
PC Win32 Visual C++ 4.0 (Console Application)
PC Win95 Visual C++ 5.0 (Console Application)
Thanks to Niu Yongsheng <niuysbit@163.net>:
Use the FOPENB option below
PC DOS djgpp gnu C
Thanks to Mark Pettovello <mpettove@umdsun2.umd.umich.edu>:
Use the FOPENB option below
HP HP-UX bundled cc
Thanks to Brian Dixon <briand@hpcvsgen.cv.hp.com>:
in ksh:
export CCOPTS="-Aa -D_HPUX_SOURCE | -lM"
cc -O3 -o susan susan2l.c
\**********************************************************************/
/* }}} */
/* {{{ History */
/**********************************************************************\
SUSAN Version 2l, 12/2/99
Changed GNUDOS option to FOPENB.
(Thanks to Niu Yongsheng <niuysbit@163.net>.)
Took out redundant "sq=sq/2;".
SUSAN Version 2k, 19/8/98:
In corner finding:
Changed if(yy<sq) {...} else if(xx<sq) {...} to
if(yy<xx) {...} else {...}
(Thanks to adq@cim.mcgill.edu - Alain Domercq.)
SUSAN Version 2j, 22/10/97:
Fixed (mask_size>x_size) etc. tests in smoothing.
Added a couple of free() calls for cgx and cgy.
(Thanks to geoffb@ucs.ed.ac.uk - Geoff Browitt.)
SUSAN Version 2i, 21/7/97:
Added information about corner attributes.
SUSAN Version 2h, 16/12/96:
Added principle (initial enhancement) option.
SUSAN Version 2g, 2/7/96:
Minor superficial changes to code.
SUSAN Version 2f, 16/1/96:
Added GNUDOS option (now called FOPENB; see options below).
SUSAN Version 2e, 9/1/96:
Added -b option.
Fixed 1 pixel horizontal offset error for drawing edges.
SUSAN Version 2d, 27/11/95:
Fixed loading of certain PGM files in get_image (again!)
SUSAN Version 2c, 22/11/95:
Fixed loading of certain PGM files in get_image.
(Thanks to qu@San-Jose.ate.slb.com - Gongyuan Qu.)
SUSAN Version 2b, 9/11/95:
removed "z==" error in edges routines.
SUSAN Version 2a, 6/11/95:
Removed a few unnecessary variable declarations.
Added different machine information.
Changed "header" in get_image to char.
SUSAN Version 2, 1/11/95: first combined version able to take any
image sizes.
SUSAN "Versions 1", circa 1992: the various SUSAN algorithms were
developed during my doctorate within different programs and for
fixed image sizes. The algorithms themselves are virtually unaltered
between "versions 1" and the combined program, version 2.
\**********************************************************************/
/* }}} */
/* {{{ defines, includes and typedefs */
/* ********** Optional settings */
#ifndef PPC
typedef int TOTAL_TYPE; /* this is faster for "int" but should be "float" for large d masks */
#else
typedef float TOTAL_TYPE; /* for my PowerPC accelerator only */
#endif
/*#define FOPENB*/ /* uncomment if using djgpp gnu C for DOS or certain Win95 compilers */
#define SEVEN_SUPP /* size for non-max corner suppression; SEVEN_SUPP or FIVE_SUPP */
#define MAX_CORNERS 15000 /* max corners per frame */
/* ********** Leave the rest - but you may need to remove one or both of sys/file.h and malloc.h lines */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/file.h> /* may want to remove this line */
#include <malloc.h> /* may want to remove this line */
#define exit_error(IFB,IFC) { fprintf(stderr,IFB,IFC); exit(0); }
#define FTOI(a) ( (a) < 0 ? ((int)(a-0.5)) : ((int)(a+0.5)) )
typedef unsigned char uchar;
typedef struct {int x,y,info, dx, dy, I;} CORNER_LIST[MAX_CORNERS];
/* }}} */
/* {{{ usage() */
usage()
{
printf("Usage: susan <in.pgm> <out.pgm> [options]\n\n");
printf("-s : Smoothing mode (default)\n");
printf("-e : Edges mode\n");
printf("-c : Corners mode\n\n");
printf("See source code for more information about setting the thresholds\n");
printf("-t <thresh> : Brightness threshold, all modes (default=20)\n");
printf("-d <thresh> : Distance threshold, smoothing mode, (default=4) (use next option instead for flat 3x3 mask)\n");
printf("-3 : Use flat 3x3 mask, edges or smoothing mode\n");
printf("-n : No post-processing on the binary edge map (runs much faster); edges mode\n");
printf("-q : Use faster (and usually stabler) corner mode; edge-like corner suppression not carried out; corners mode\n");
printf("-b : Mark corners/edges with single black points instead of black with white border; corners or edges mode\n");
printf("-p : Output initial enhancement image only; corners or edges mode (default is edges mode)\n");
printf("\nSUSAN Version 2l (C) 1995-1997 Stephen Smith, DRA UK. steve@fmrib.ox.ac.uk\n");
exit(0);
}
/* }}} */
/* {{{ get_image(filename,in,x_size,y_size) */
/* {{{ int getint(fp) derived from XV */
int getint(fd)
FILE *fd;
{
int c, i;
char dummy[10000];
c = getc(fd);
while (1) /* find next integer */
{
if (c=='#') /* if we're at a comment, read to end of line */
fgets(dummy,9000,fd);
if (c==EOF)
exit_error("Image %s not binary PGM.\n","is");
if (c>='0' && c<='9')
break; /* found what we were looking for */
c = getc(fd);
}
/* we're at the start of a number, continue until we hit a non-number */
i = 0;
while (1) {
i = (i*10) + (c - '0');
c = getc(fd);
if (c==EOF) return (i);
if (c<'0' || c>'9') break;
}
return (i);
}
/* }}} */
#ifndef LIB
void get_image(filename,in,x_size,y_size)
char filename[200];
unsigned char **in;
int *x_size, *y_size;
{
FILE *fd;
char header [100];
int tmp;
#ifdef FOPENB
if ((fd=fopen(filename,"rb")) == NULL)
#else
if ((fd=fopen(filename,"r")) == NULL)
#endif
exit_error("Can't input image %s.\n",filename);
/* {{{ read header */
header[0]=fgetc(fd);
header[1]=fgetc(fd);
if(!(header[0]=='P' && header[1]=='5'))
exit_error("Image %s does not have binary PGM header.\n",filename);
*x_size = getint(fd);
*y_size = getint(fd);
tmp = getint(fd);
/* }}} */
*in = (uchar *) malloc(*x_size * *y_size);
if (fread(*in,1,*x_size * *y_size,fd) == 0)
exit_error("Image %s is wrong size.\n",filename);
fclose(fd);
}
/* }}} */
/* {{{ put_image(filename,in,x_size,y_size) */
put_image(filename,in,x_size,y_size)
char filename [100],
*in;
int x_size,
y_size;
{
FILE *fd;
#ifdef FOPENB
if ((fd=fopen(filename,"wb")) == NULL)
#else
if ((fd=fopen(filename,"w")) == NULL)
#endif
exit_error("Can't output image%s.\n",filename);
fprintf(fd,"P5\n");
fprintf(fd,"%d %d\n",x_size,y_size);
fprintf(fd,"255\n");
if (fwrite(in,x_size*y_size,1,fd) != 1)
exit_error("Can't write image %s.\n",filename);
fclose(fd);
}
#endif
/* }}} */
/* {{{ int_to_uchar(r,in,size) */
int_to_uchar(r,in,size)
uchar *in;
int *r, size;
{
int i,
max_r=r[0],
min_r=r[0];
for (i=0; i<size; i++)
{
if ( r[i] > max_r )
max_r=r[i];
if ( r[i] < min_r )
min_r=r[i];
}
/*printf("min=%d max=%d\n",min_r,max_r);*/
max_r-=min_r;
for (i=0; i<size; i++)
in[i] = (uchar)((int)((int)(r[i]-min_r)*255)/max_r);
}
/* }}} */
/* {{{ setup_brightness_lut(bp,thresh,form) */
void setup_brightness_lut(bp,thresh,form)
uchar **bp;
int thresh, form;
{
int k;
float temp;
*bp=(unsigned char *)malloc(516);
*bp=*bp+258;
for(k=-256;k<257;k++)
{
temp=((float)k)/((float)thresh);
temp=temp*temp;
if (form==6)
temp=temp*temp*temp;
temp=100.0*exp(-temp);
*(*bp+k)= (uchar)temp;
}
}
/* }}} */
/* {{{ susan principle */
/* {{{ susan_principle(in,r,bp,max_no,x_size,y_size) */
susan_principle(in,r,bp,max_no,x_size,y_size)
uchar *in, *bp;
int *r, max_no, x_size, y_size;
{
int i, j, n;
uchar *p,*cp;
memset (r,0,x_size * y_size * sizeof(int));
for (i=3;i<y_size-3;i++)
for (j=3;j<x_size-3;j++)
{
n=100;
p=in + (i-3)*x_size + j - 1;
cp=bp + in[i*x_size+j];
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=x_size-3;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=x_size-5;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=x_size-6;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=2;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=x_size-6;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=x_size-5;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=x_size-3;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
if (n<=max_no)
r[i*x_size+j] = max_no - n;
}
}
/* }}} */
/* {{{ susan_principle_small(in,r,bp,max_no,x_size,y_size) */
susan_principle_small(in,r,bp,max_no,x_size,y_size)
uchar *in, *bp;
int *r, max_no, x_size, y_size;
{
int i, j, n;
uchar *p,*cp;
memset (r,0,x_size * y_size * sizeof(int));
max_no = 730; /* ho hum ;) */
for (i=1;i<y_size-1;i++)
for (j=1;j<x_size-1;j++)
{
n=100;
p=in + (i-1)*x_size + j - 1;
cp=bp + in[i*x_size+j];
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
p+=x_size-2;
n+=*(cp-*p);
p+=2;
n+=*(cp-*p);
p+=x_size-2;
n+=*(cp-*p++);
n+=*(cp-*p++);
n+=*(cp-*p);
if (n<=max_no)
r[i*x_size+j] = max_no - n;
}
}
/* }}} */
/* }}} */
/* {{{ smoothing */
/* {{{ median(in,i,j,x_size) */
uchar median(in,i,j,x_size)
uchar *in;
int i, j, x_size;
{
int p[8],k,l,tmp;
p[0]=in[(i-1)*x_size+j-1];
p[1]=in[(i-1)*x_size+j ];
p[2]=in[(i-1)*x_size+j+1];
p[3]=in[(i )*x_size+j-1];
p[4]=in[(i )*x_size+j+1];
p[5]=in[(i+1)*x_size+j-1];
p[6]=in[(i+1)*x_size+j ];
p[7]=in[(i+1)*x_size+j+1];
for(k=0; k<7; k++)
for(l=0; l<(7-k); l++)
if (p[l]>p[l+1])
{
tmp=p[l]; p[l]=p[l+1]; p[l+1]=tmp;
}
return( (p[3]+p[4]) / 2 );
}
/* }}} */
/* {{{ enlarge(in,tmp_image,x_size,y_size,border) */
/* this enlarges "in" so that borders can be dealt with easily */
enlarge(in,tmp_image,x_size,y_size,border)
uchar **in;
uchar *tmp_image;
int *x_size, *y_size, border;
{
int i, j;
for(i=0; i<*y_size; i++) /* copy *in into tmp_image */
memcpy(tmp_image+(i+border)*(*x_size+2*border)+border, *in+i* *x_size, *x_size);
for(i=0; i<border; i++) /* copy top and bottom rows; invert as many as necessary */
{
memcpy(tmp_image+(border-1-i)*(*x_size+2*border)+border,*in+i* *x_size,*x_size);
memcpy(tmp_image+(*y_size+border+i)*(*x_size+2*border)+border,*in+(*y_size-i-1)* *x_size,*x_size);
}
for(i=0; i<border; i++) /* copy left and right columns */
for(j=0; j<*y_size+2*border; j++)
{
tmp_image[j*(*x_size+2*border)+border-1-i]=tmp_image[j*(*x_size+2*border)+border+i];
tmp_image[j*(*x_size+2*border)+ *x_size+border+i]=tmp_image[j*(*x_size+2*border)+ *x_size+border-1-i];
}
*x_size+=2*border; /* alter image size */
*y_size+=2*border;
*in=tmp_image; /* repoint in */
}
/* }}} */
/* {{{ void susan_smoothing(three_by_three,in,dt,x_size,y_size,bp) */
void susan_smoothing(three_by_three,in,dt,x_size,y_size,bp)
int three_by_three, x_size, y_size;
uchar *in, *bp;
float dt;
{
/* {{{ vars */
float temp;
int n_max, increment, mask_size,
i,j,x,y,area,brightness,tmp,centre;
uchar *ip, *dp, *dpt, *cp, *out=in,
*tmp_image;
TOTAL_TYPE total;
/* }}} */
/* {{{ setup larger image and border sizes */
if (three_by_three==0)
mask_size = ((int)(1.5 * dt)) + 1;
else
mask_size = 1;
total=0.1; /* test for total's type */
if ( (dt>15) && (total==0) )
{
printf("Distance_thresh (%f) too big for integer arithmetic.\n",dt);
printf("Either reduce it to <=15 or recompile with variable \"total\"\n");
printf("as a float: see top \"defines\" section.\n");
exit(0);
}
if ( (2*mask_size+1>x_size) || (2*mask_size+1>y_size) )
{
printf("Mask size (1.5*distance_thresh+1=%d) too big for image (%dx%d).\n",mask_size,x_size,y_size);
exit(0);
}
tmp_image = (uchar *) malloc( (x_size+mask_size*2) * (y_size+mask_size*2) );
enlarge(&in,tmp_image,&x_size,&y_size,mask_size);
/* }}} */
if (three_by_three==0)
{ /* large Gaussian masks */
/* {{{ setup distance lut */
n_max = (mask_size*2) + 1;
increment = x_size - n_max;
dp = (unsigned char *)malloc(n_max*n_max);
dpt = dp;
temp = -(dt*dt);
for(i=-mask_size; i<=mask_size; i++)
for(j=-mask_size; j<=mask_size; j++)
{
x = (int) (100.0 * exp( ((float)((i*i)+(j*j))) / temp ));
*dpt++ = (unsigned char)x;
}
/* }}} */
/* {{{ main section */
for (i=mask_size;i<y_size-mask_size;i++)
{
for (j=mask_size;j<x_size-mask_size;j++)
{
area = 0;
total = 0;
dpt = dp;
ip = in + ((i-mask_size)*x_size) + j - mask_size;
centre = in[i*x_size+j];
cp = bp + centre;
for(y=-mask_size; y<=mask_size; y++)
{
for(x=-mask_size; x<=mask_size; x++)
{
brightness = *ip++;
tmp = *dpt++ * *(cp-brightness);
area += tmp;
total += tmp * brightness;
}
ip += increment;
}
tmp = area-10000;
if (tmp==0)
*out++=median(in,i,j,x_size);
else
*out++=((total-(centre*10000))/tmp);
}
}
/* }}} */
}
else
{ /* 3x3 constant mask */
/* {{{ main section */
for (i=1;i<y_size-1;i++)
{
for (j=1;j<x_size-1;j++)
{
area = 0;
total = 0;
ip = in + ((i-1)*x_size) + j - 1;
centre = in[i*x_size+j];
cp = bp + centre;
brightness=*ip++; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
brightness=*ip++; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
brightness=*ip; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
ip += x_size-2;
brightness=*ip++; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
brightness=*ip++; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
brightness=*ip; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
ip += x_size-2;
brightness=*ip++; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
brightness=*ip++; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
brightness=*ip; tmp=*(cp-brightness); area += tmp; total += tmp * brightness;
tmp = area-100;
if (tmp==0)
*out++=median(in,i,j,x_size);
else
*out++=(total-(centre*100))/tmp;
}
}
/* }}} */
}
}
/* }}} */
/* }}} */
/* {{{ edges */
/* {{{ edge_draw(in,corner_list,drawing_mode) */
edge_draw(in,mid,x_size,y_size,drawing_mode)
uchar *in, *mid;
int x_size, y_size, drawing_mode;
{
int i;
uchar *inp, *midp;
if (drawing_mode==0)
{
/* mark 3x3 white block around each edge point */
midp=mid;
for (i=0; i<x_size*y_size; i++)
{
if (*midp<8)
{
inp = in + (midp - mid) - x_size - 1;
*inp++=255; *inp++=255; *inp=255; inp+=x_size-2;
*inp++=255; *inp++; *inp=255; inp+=x_size-2;
*inp++=255; *inp++=255; *inp=255;
}
midp++;
}
}
/* now mark 1 black pixel at each edge point */
midp=mid;
for (i=0; i<x_size*y_size; i++)
{
if (*midp<8)
*(in + (midp - mid)) = 0;
midp++;
}
}
/* }}} */
/* {{{ susan_thin(r,mid,x_size,y_size) */
/* only one pass is needed as i,j are decremented if necessary to go
back and do bits again */
susan_thin(r,mid,x_size,y_size)
uchar *mid;
int *r, x_size, y_size;
{
int l[9], centre, nlinks, npieces,
b01, b12, b21, b10,
p1, p2, p3, p4,
b00, b02, b20, b22,
m, n, a, b, x, y, i, j;
uchar *mp;
for (i=4;i<y_size-4;i++)
for (j=4;j<x_size-4;j++)
if (mid[i*x_size+j]<8)
{
centre = r[i*x_size+j];
/* {{{ count number of neighbours */
mp=mid + (i-1)*x_size + j-1;
n = (*mp<8) +
(*(mp+1)<8) +
(*(mp+2)<8) +
(*(mp+x_size)<8) +
(*(mp+x_size+2)<8) +
(*(mp+x_size+x_size)<8) +
(*(mp+x_size+x_size+1)<8) +
(*(mp+x_size+x_size+2)<8);
/* }}} */
/* {{{ n==0 no neighbours - remove point */
if (n==0)
mid[i*x_size+j]=100;
/* }}} */
/* {{{ n==1 - extend line if I can */
/* extension is only allowed a few times - the value of mid is used to control this */
if ( (n==1) && (mid[i*x_size+j]<6) )
{
/* find maximum neighbour weighted in direction opposite the
neighbour already present. e.g.
have: O O O weight r by 0 2 3
X X O 0 0 4
O O O 0 2 3 */
l[0]=r[(i-1)*x_size+j-1]; l[1]=r[(i-1)*x_size+j]; l[2]=r[(i-1)*x_size+j+1];
l[3]=r[(i )*x_size+j-1]; l[4]=0; l[5]=r[(i )*x_size+j+1];
l[6]=r[(i+1)*x_size+j-1]; l[7]=r[(i+1)*x_size+j]; l[8]=r[(i+1)*x_size+j+1];
if (mid[(i-1)*x_size+j-1]<8) { l[0]=0; l[1]=0; l[3]=0; l[2]*=2;
l[6]*=2; l[5]*=3; l[7]*=3; l[8]*=4; }
else { if (mid[(i-1)*x_size+j]<8) { l[1]=0; l[0]=0; l[2]=0; l[3]*=2;
l[5]*=2; l[6]*=3; l[8]*=3; l[7]*=4; }
else { if (mid[(i-1)*x_size+j+1]<8) { l[2]=0; l[1]=0; l[5]=0; l[0]*=2;
l[8]*=2; l[3]*=3; l[7]*=3; l[6]*=4; }
else { if (mid[(i)*x_size+j-1]<8) { l[3]=0; l[0]=0; l[6]=0; l[1]*=2;
l[7]*=2; l[2]*=3; l[8]*=3; l[5]*=4; }
else { if (mid[(i)*x_size+j+1]<8) { l[5]=0; l[2]=0; l[8]=0; l[1]*=2;
l[7]*=2; l[0]*=3; l[6]*=3; l[3]*=4; }
else { if (mid[(i+1)*x_size+j-1]<8) { l[6]=0; l[3]=0; l[7]=0; l[0]*=2;
l[8]*=2; l[1]*=3; l[5]*=3; l[2]*=4; }
else { if (mid[(i+1)*x_size+j]<8) { l[7]=0; l[6]=0; l[8]=0; l[3]*=2;
l[5]*=2; l[0]*=3; l[2]*=3; l[1]*=4; }
else { if (mid[(i+1)*x_size+j+1]<8) { l[8]=0; l[5]=0; l[7]=0; l[6]*=2;
l[2]*=2; l[1]*=3; l[3]*=3; l[0]*=4; } }}}}}}}
m=0; /* find the highest point */
for(y=0; y<3; y++)
for(x=0; x<3; x++)
if (l[y+y+y+x]>m) { m=l[y+y+y+x]; a=y; b=x; }
if (m>0)
{
if (mid[i*x_size+j]<4)
mid[(i+a-1)*x_size+j+b-1] = 4;
else
mid[(i+a-1)*x_size+j+b-1] = mid[i*x_size+j]+1;
if ( (a+a+b) < 3 ) /* need to jump back in image */
{
i+=a-1;
j+=b-2;
if (i<4) i=4;
if (j<4) j=4;
}
}
}
/* }}} */
/* {{{ n==2 */
if (n==2)
{
/* put in a bit here to straighten edges */
b00 = mid[(i-1)*x_size+j-1]<8; /* corners of 3x3 */
b02 = mid[(i-1)*x_size+j+1]<8;
b20 = mid[(i+1)*x_size+j-1]<8;
b22 = mid[(i+1)*x_size+j+1]<8;
if ( ((b00+b02+b20+b22)==2) && ((b00|b22)&(b02|b20)))
{ /* case: move a point back into line.
e.g. X O X CAN become X X X
O X O O O O
O O O O O O */
if (b00)
{
if (b02) { x=0; y=-1; }
else { x=-1; y=0; }
}
else
{
if (b02) { x=1; y=0; }
else { x=0; y=1; }
}
if (((float)r[(i+y)*x_size+j+x]/(float)centre) > 0.7)
{
if ( ( (x==0) && (mid[(i+(2*y))*x_size+j]>7) && (mid[(i+(2*y))*x_size+j-1]>7) && (mid[(i+(2*y))*x_size+j+1]>7) ) ||
( (y==0) && (mid[(i)*x_size+j+(2*x)]>7) && (mid[(i+1)*x_size+j+(2*x)]>7) && (mid[(i-1)*x_size+j+(2*x)]>7) ) )
{
mid[(i)*x_size+j]=100;
mid[(i+y)*x_size+j+x]=3; /* no jumping needed */
}
}
}
else
{
b01 = mid[(i-1)*x_size+j ]<8;
b12 = mid[(i )*x_size+j+1]<8;
b21 = mid[(i+1)*x_size+j ]<8;
b10 = mid[(i )*x_size+j-1]<8;
/* {{{ right angle ends - not currently used */
#ifdef IGNORETHIS
if ( (b00&b01)|(b00&b10)|(b02&b01)|(b02&b12)|(b20&b10)|(b20&b21)|(b22&b21)|(b22&b12) )
{ /* case; right angle ends. clean up.
e.g.; X X O CAN become X X O
O X O O O O
O O O O O O */
if ( ((b01)&(mid[(i-2)*x_size+j-1]>7)&(mid[(i-2)*x_size+j]>7)&(mid[(i-2)*x_size+j+1]>7)&
((b00&((2*r[(i-1)*x_size+j+1])>centre))|(b02&((2*r[(i-1)*x_size+j-1])>centre)))) |
((b10)&(mid[(i-1)*x_size+j-2]>7)&(mid[(i)*x_size+j-2]>7)&(mid[(i+1)*x_size+j-2]>7)&
((b00&((2*r[(i+1)*x_size+j-1])>centre))|(b20&((2*r[(i-1)*x_size+j-1])>centre)))) |
((b12)&(mid[(i-1)*x_size+j+2]>7)&(mid[(i)*x_size+j+2]>7)&(mid[(i+1)*x_size+j+2]>7)&
((b02&((2*r[(i+1)*x_size+j+1])>centre))|(b22&((2*r[(i-1)*x_size+j+1])>centre)))) |
((b21)&(mid[(i+2)*x_size+j-1]>7)&(mid[(i+2)*x_size+j]>7)&(mid[(i+2)*x_size+j+1]>7)&
((b20&((2*r[(i+1)*x_size+j+1])>centre))|(b22&((2*r[(i+1)*x_size+j-1])>centre)))) )