image_correlation.c
Go to the documentation of this file.
1 /****************************************************************/
2 /* image_correlation.c
3 /* rectangular window match: image-correlation pattern window ...
4 /* linear match: circular-correlation pattern target num_components
5 /* gcc -c -Di386 -DLinux -w -DGCC -fpic -I../c -O image_correlation.c;
6  ld -shared -o image_correlation.so image_correlation.o
7 /* cc -c -O -Dsun4 -Bstatic -I../../c cor.c
8 *****************************************************************/
9 
10 #include "eus.h"
11 #include "pix.h"
12 #define abs(x) ((x)<0?-(x):x)
13 #define imgvec(img,x,y) (&((img)->imgary[(y)*(img)->twidth + x]))
14 
15 #pragma init (init_object_module)
17 
18 static void init_object_module()
19  { add_module_initializer("image_correlation", image_correlation);}
20 
21 
22 
23 static void set_pixparams(piximg, eusary)
24 pixel_image *piximg;
25 pointer eusary;
26 { pointer imgp;
27  imgp=eusary->c.ary.entity;
28 /* printf("imgp=%x\n", imgp); */
29  piximg->imgary=
30  (elmtypeof(imgp)==ELM_FOREIGN)?
31  ((unsigned char *)imgp->c.ivec.iv[0]) :
32  (imgp->c.str.chars);
33 /* printf("imgary=%x\n", piximg->imgary); */
34  piximg->depth=8;
35  piximg->twidth=intval(eusary->c.ary.dim[1]);
36  piximg->theight=intval(eusary->c.ary.dim[0]);
37  piximg->tsize=piximg->twidth*piximg->theight;
38  piximg->width=intval(eusary->c.ary.dim[1]);
39  piximg->height=intval(eusary->c.ary.dim[0]);
40  piximg->wsize=piximg->width * piximg->height; }
41 
42 static int img_correlation1(image,pattern,x,y)
43 register pixel_image *image, *pattern;
44 register int x, y;
45 { register unsigned char *img=image->imgary, *pat=pattern->imgary;
46  register int i, j;
47  register int ww=pattern->width;
48  register long cor, corsum=0;
49  int tiw=image->twidth, piw=pattern->twidth;
50  int wh=pattern->height;
51 
52  img=imgvec(image, x, y);
53  pat=imgvec(pattern, 0, 0);
54  for (j=0; j<wh; j++) {
55  for (i=0; i<ww; i++) {
56  cor=img[i]-pat[i];
57  corsum+= abs(cor);}
58  img+=tiw; pat+=piw;
59  }
60  return(corsum);}
61 
62 struct corinfo {
63  long corval;
64  long x,y;
65  };
66 
67 static int img_correlation(image,pattern,x0,y0,size,threshold,result)
68 pixel_image *image, *pattern;
69 register int size;
70 register int threshold;
71 int x0, y0;
72 struct corinfo *result;
73 { register unsigned char *img=image->imgary, *pat=pattern->imgary;
74  register int i, j;
75  register int x, y;
76  register int tiw=image->twidth, piw=pattern->twidth;
77  register int ww=pattern->width;
78  register long cor, corsum;
79  register int wh=pattern->height;
80  int found=0;
81 
82  for (y=y0; y<y0+size; y++) {
83  for (x=x0; x<x0+size; x++) {
84  /* compute correlation for a particular window */
85  corsum=0;
86  img=imgvec(image, x, y);
87  pat=imgvec(pattern, 0, 0);
88  for (j=0; j<wh; j++) {
89  for (i=0; i<ww; i++) {
90  cor=img[i]-pat[i];
91  if (cor>0) corsum+=cor;
92  else corsum-=cor;}
93  if (corsum>threshold) goto nextwindow;
94  img+=tiw; pat+=piw; }
95 
96  /* correlation factor for a window is obtained in corsum */
97  if (corsum<threshold) {
98  if (debug) printf("cor=%ld (%d, %d)\n", corsum, x, y);
99  found=1;
100  threshold=corsum;
101  result->corval=corsum;
102  result->x=x; result->y=y;};
103  /* try a next window */
104  nextwindow: continue;
105  } }
106  return(found);}
107 
108 static pointer IMAGE_CORRELATION1(ctx,n,argv)
109 context *ctx;
110 int n;
111 pointer argv[];
112 { pointer arg0, arg1;
113  pixel_image img,pat;
114  int x,y, cor;
115  ckarg(4);
116  set_pixparams(&img, argv[0]);
117  set_pixparams(&pat, argv[1]);
118  x=ckintval(argv[2]); y=ckintval(argv[3]);
119  cor=img_correlation1(&img, &pat, x, y);
120  return(makeint(cor));}
121 
122 
123 /* (IMAGE-CORRELATION image pattern x y size threshold) */
124 
125 static pointer IMAGE_CORRELATION(ctx,n,argv)
126 context *ctx;
127 int n;
128 pointer argv[];
129 { pointer arg0, arg1;
130  pixel_image img,pat;
131  int x,y, cor, size, threshold;
132  struct { long corval, x, y;} result;
133  ckarg(6);
134  set_pixparams(&img, argv[0]);
135  set_pixparams(&pat, argv[1]);
136  x=ckintval(argv[2]); y=ckintval(argv[3]);
137  size=ckintval(argv[4]); threshold=ckintval(argv[5]);
138  cor=img_correlation(&img, &pat, x, y, size, threshold, &result);
139  if (cor) {
140  vpush(makeint(result.corval));
141  vpush(makeint(result.x));
142  vpush(makeint(result.y));
143  return((pointer)stacknlist(ctx,3));}
144  else return(NIL);}
145 
146 
148 /* Actually, this is a linear correlation. */
149 context *ctx;
150 int n;
151 pointer argv[];
152 { unsigned char *pat1, *pat2, pat2x[1024*4];
153  int len, num_components, total_len, i, j, k, x, y;
154  int c, cor, min_cor, min_pos, max_cor, avecor, total_cor;
155  float fcor, flen;
156  pointer fcorvec, r;
157  numunion nu;
158 
159  ckarg(3);
160  num_components=ckintval(argv[2]);
161  pat1=get_string(argv[0]);
162  pat2=get_string(argv[1]);
163  total_len=vecsize(argv[0]);
164  if (total_len != vecsize(argv[1])) error(E_VECSIZE);
165  if (total_len >=1024*3) error(E_ARRAYINDEX);
166  memcpy(&pat2x[0], pat2, total_len);
167  memcpy(&pat2x[total_len], pat2, total_len);
168  len=total_len/num_components;
169  flen=len;
170  fcorvec=makevector(C_FLTVECTOR, len);
171  vpush(fcorvec);
172 
173  min_pos = -1; min_cor=0x3fffffff; max_cor=0; total_cor=0;
174 
175  for (i=0; i<len; i++) {
176  cor=0; x=i*num_components;
177  for (j=0; j<len; j++) {
178  c=0; y=j*num_components;
179  for (k=0; k<num_components; k++) c += abs(pat2x[x+j+k]-pat1[j+k]);
180  cor += c/num_components; }
181  if (cor < min_cor) { min_cor=cor; min_pos=i; }
182  else if (cor>max_cor) max_cor=cor;
183  total_cor += cor;
184  fcorvec->c.fvec.fv[i]=(float)cor/total_len;
185  }
186  vpush(makeint(min_pos));
187  vpush(makeflt(min_cor/(flen*num_components)));
188  vpush(makeflt(total_cor/(flen*num_components*flen)));
189  vpush(makeflt(max_cor/(len*num_components)));
190  vpush(fcorvec);
191  r=stacknlist(ctx,5);
192  vpop();
193  return(r);}
194 
196 context *ctx;
197 int n;
198 pointer argv[];
199 { pointer mod=argv[0];
200  defun(ctx,"IMAGE-CORRELATION1",mod,IMAGE_CORRELATION1,NULL);
201  defun(ctx,"IMAGE-CORRELATION",mod,IMAGE_CORRELATION,NULL);
202  defun(ctx,"CIRCULAR-CORRELATION",mod,CIRCULAR_CORRELATION,NULL);
203  return(NIL);
204  }
eusinteger_t iv[1]
Definition: eus.h:305
#define makeint(v)
Definition: sfttest.c:2
Definition: eus.h:524
struct string str
Definition: eus.h:402
byte chars[1]
Definition: eus.h:212
static pointer CIRCULAR_CORRELATION(context *ctx, int n, argv)
GLfloat n[6][3]
Definition: cube.c:15
struct arrayheader ary
Definition: eus.h:413
#define intval(p)
Definition: sfttest.c:1
defun("ADR_TO_STRING", mod, ADR_TO_STRING)
static void set_pixparams(pixel_image *piximg, pointer eusary)
#define abs(x)
static pointer IMAGE_CORRELATION(context *ctx, int n, argv)
Definition: pix.h:2
pointer makevector(pointer, int)
Definition: makes.c:417
ckarg(2)
void add_module_initializer(char *, pointer(*)())
Definition: loadelf.c:86
Definition: eus.h:964
#define imgvec(img, x, y)
struct intvector ivec
Definition: eus.h:416
union cell::cellunion c
static int img_correlation1(pixel_image *image, pixel_image *pattern, int x, int y)
pointer entity
Definition: eus.h:313
Definition: eus.h:428
unsigned char * imgary
Definition: pix.h:3
Definition: eus.h:381
pointer error(enum errorcode ec,...) pointer error(va_alist) va_dcl
Definition: eus.c:297
pointer dim[ARRAYRANKLIMIT]
Definition: eus.h:313
byte * get_string()
pointer stacknlist(context *, int)
Definition: makes.c:129
static void init_object_module()
#define NULL
Definition: transargv.c:8
static int img_correlation(pixel_image *image, pixel_image *pattern, int x0, int y0, int size, int threshold, struct corinfo *result)
pointer C_FLTVECTOR
Definition: eus.c:146
eusfloat_t fv[1]
Definition: eus.h:309
pointer image_correlation()
pointer NIL
Definition: eus.c:110
static pointer IMAGE_CORRELATION1(context *ctx, int n, argv)
struct floatvector fvec
Definition: eus.h:415
pointer makeflt()


euslisp
Author(s): Toshihiro Matsui
autogenerated on Mon Feb 28 2022 22:18:27