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  }
numunion
Definition: eus.h:428
NIL
pointer NIL
Definition: eus.c:110
C_FLTVECTOR
pointer C_FLTVECTOR
Definition: eus.c:146
defun
defun("ADR_TO_STRING", mod, ADR_TO_STRING)
makeint
#define makeint(v)
Definition: sfttest.c:2
context
Definition: eus.h:524
floatvector::fv
eusfloat_t fv[1]
Definition: eus.h:309
makevector
pointer makevector(pointer, int)
Definition: makes.c:417
imgvec
#define imgvec(img, x, y)
Definition: image_correlation.c:13
intval
#define intval(p)
Definition: sfttest.c:1
CIRCULAR_CORRELATION
static pointer CIRCULAR_CORRELATION(context *ctx, int n, argv)
Definition: image_correlation.c:147
add_module_initializer
void add_module_initializer(char *, pointer(*)())
Definition: loadelf.c:86
abs
#define abs(x)
Definition: image_correlation.c:12
eus.h
cell::cellunion::ary
struct arrayheader ary
Definition: eus.h:413
cell::cellunion::ivec
struct intvector ivec
Definition: eus.h:416
string::chars
byte chars[1]
Definition: eus.h:212
cell::c
union cell::cellunion c
set_pixparams
static void set_pixparams(pixel_image *piximg, pointer eusary)
Definition: image_correlation.c:23
E_ARRAYINDEX
@ E_ARRAYINDEX
Definition: eus.h:967
pix.h
NULL
#define NULL
Definition: transargv.c:8
IMAGE_CORRELATION
static pointer IMAGE_CORRELATION(context *ctx, int n, argv)
Definition: image_correlation.c:125
corinfo
Definition: image_correlation.c:62
get_string
byte * get_string()
IMAGE_CORRELATION1
static pointer IMAGE_CORRELATION1(context *ctx, int n, argv)
Definition: image_correlation.c:108
pixel_image
Definition: pix.h:2
corinfo::y
long y
Definition: image_correlation.c:64
makeflt
pointer makeflt()
error
pointer error(enum errorcode ec,...) pointer error(va_alist) va_dcl
Definition: eus.c:297
image_correlation
pointer image_correlation()
cell
Definition: eus.h:381
stacknlist
pointer stacknlist(context *, int)
Definition: makes.c:129
init_object_module
static void init_object_module()
Definition: image_correlation.c:18
E_VECSIZE
@ E_VECSIZE
Definition: eus.h:964
arrayheader::entity
pointer entity
Definition: eus.h:313
pixel_image::imgary
unsigned char * imgary
Definition: pix.h:3
arrayheader::dim
pointer dim[ARRAYRANKLIMIT]
Definition: eus.h:317
corinfo::corval
long corval
Definition: image_correlation.c:63
intvector::iv
eusinteger_t iv[1]
Definition: eus.h:305
img_correlation1
static int img_correlation1(pixel_image *image, pixel_image *pattern, int x, int y)
Definition: image_correlation.c:42
corinfo::x
long x
Definition: image_correlation.c:64
cell::cellunion::fvec
struct floatvector fvec
Definition: eus.h:415
n
GLfloat n[6][3]
Definition: cube.c:15
ckarg
ckarg(2)
cell::cellunion::str
struct string str
Definition: eus.h:402
img_correlation
static int img_correlation(pixel_image *image, pixel_image *pattern, int x0, int y0, int size, int threshold, struct corinfo *result)
Definition: image_correlation.c:67


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:43