File: remove_corneal_reflection.cc

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (234 lines) | stat: -rw-r--r-- 8,125 bytes parent folder | download | duplicates (7)
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
/*
 *
 * cvEyeTracker is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * cvEyeTracker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with cvEyeTracker; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 * cvEyeTracker - Version 1.2.5
 * Part of the openEyes ToolKit -- http://hcvl.hci.iastate.edu/openEyes
 * Release Date:
 * Authors : Dongheng Li <dhli@iastate.edu>
 *           Derrick Parkhurst <derrick.parkhurst@hcvl.hci.iastate.edu>
 *           Jason Babcock <babcock@nyu.edu>
 *           David Winfield <dwinfiel@iastate.edu>
 * Copyright (c) 2004-2006
 * All Rights Reserved.
 *
 */

#ifdef PSYCHCV_USE_OPENCV

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "remove_corneal_reflection.h"

// Includes from PTB:
//
// It is important that these are included *last*, so remapping of some functions,
// e.g., printf() -> mexFunction() works correctly!
#include <Psych.h>
#include <PsychCV.h>

void remove_corneal_reflection(IplImage *image, IplImage *threshold_image, int sx, int sy, int window_size, int 
biggest_crr, int& crx, int& cry, int& crr)
{
  int crar = -1;	//corneal reflection approximate radius
  crx = cry = crar = -1;

  float angle_delta = 1*PI/180;
  int angle_num = (int)(2*PI/angle_delta);
  printf("(corneal reflection) sx:%d; sy:%d\n", sx, sy);
  double *angle_array = (double*)malloc(angle_num*sizeof(double));
  double *sin_array = (double*)malloc(angle_num*sizeof(double));
  double *cos_array = (double*)malloc(angle_num*sizeof(double));
  for (int i = 0; i < angle_num; i++) {
    angle_array[i] = i*angle_delta;
    sin_array[i] = sin(angle_array[i]);
    cos_array[i] = cos(angle_array[i]);
  }

  locate_corneal_reflection(image, threshold_image, sx, sy, window_size, (int)(biggest_crr/2.5), crx, cry, crar);
  crr = fit_circle_radius_to_corneal_reflection(image, crx, cry, crar, (int)(biggest_crr/2.5),  sin_array, cos_array, angle_num);
  crr = (int)(2.5*crr);
  interpolate_corneal_reflection(image, crx, cry, crr, sin_array, cos_array, angle_num);

  free(angle_array);
  free(sin_array);
  free(cos_array);
}

void locate_corneal_reflection(IplImage *image, IplImage *threshold_image, int sx, int sy, int window_size, int 
biggest_crar, int &crx, int &cry, int &crar)
{
  if (window_size%2 == 0) {
    printf("Error! window_size should be odd!\n");
  }

  int r = (window_size-1)/2;
  int startx = MAX(sx-r, 0);
  int endx = MIN(sx+r, image->width-1);
  int starty = MAX(sy-r, 0);
  int endy = MIN(sy+r, image->height-1);
  cvSetImageROI(image, cvRect(startx, starty, endx-startx+1, endy-starty+1));
  cvSetImageROI(threshold_image, cvRect(startx, starty, endx-startx+1, endy-starty+1));

  double min_value, max_value;
  CvPoint min_loc, max_loc; //location
  cvMinMaxLoc(image, &min_value, &max_value, &min_loc, &max_loc);

  int threshold, i;
  CvSeq* contour=NULL;
  CvMemStorage* storage = cvCreateMemStorage(0);
  double *scores = (double*)malloc(sizeof(double)*((int)max_value+1));
  memset(scores, 0, sizeof(double)*((int)max_value+1));
  int area, max_area, sum_area;
  for (threshold = (int)max_value; threshold >= 1; threshold--) {
    cvThreshold(image, threshold_image, threshold, 1, CV_THRESH_BINARY);
    cvFindContours(threshold_image, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE);    
    max_area = 0;
    sum_area = 0;
    CvSeq *max_contour = contour;
    for( ; contour != 0; contour = contour->h_next) {
      area = contour->total + (int)(fabs(cvContourArea(contour, CV_WHOLE_SEQ)));
      sum_area += area;
      if (area > max_area) {
        max_area = area;
        max_contour = contour;
      }
    }
    if (sum_area-max_area > 0) {
      scores[threshold-1] = max_area / (sum_area-max_area);
      //printf("max_area: %d, max_contour: %d, sum_area: %d; scores[%d]: %lf\n", 
      //        max_area, max_contour->total, sum_area, threshold-1, scores[threshold-1]);      
    }
    else
      continue;

    if (scores[threshold-1] - scores[threshold] < 0) {
      //found the corneal reflection
      crar = (int)sqrt(max_area / PI);
      int sum_x = 0;
      int sum_y = 0;
      CvPoint *point;
      for (i = 0; i < max_contour->total; i++) {
        point = CV_GET_SEQ_ELEM(CvPoint, max_contour, i);
        sum_x += point->x;
        sum_y += point->y;
      }
      crx = sum_x/max_contour->total;
      cry = sum_y/max_contour->total;
      break;
    }
  }
  /*/ printf("(corneal reflection) max_value = %lf; threshold = %d\n", max_value, threshold);
  printf("(corneal reflection) Scores:\n");
  for (int i = (int)max_value; i >= threshold-1; i--) {
    printf("%6.2lf", scores[i]);
  }
  printf("\n");*/
    
  free(scores);
  cvReleaseMemStorage(&storage);
  cvResetImageROI(image);
  cvResetImageROI(threshold_image);

  if (crar > biggest_crar) {
    printf("(corneal) size wrong! crx:%d, cry:%d, crar:%d (should be less than %d)\n", crx, cry, crar, biggest_crar);
    cry = crx = -1;
    crar = -1;
  }

  if (crx != -1 && cry != -1) {
    printf("(corneal) startx:%d, starty:%d, crx:%d, cry:%d, crar:%d\n", startx, starty, crx, cry, crar);
    crx += startx;
    cry += starty;
  }
        
}

int fit_circle_radius_to_corneal_reflection(IplImage *image, int crx, int cry, int crar, int biggest_crar, double *sin_array, double *cos_array, int array_len)
{
  if (crx == -1 || cry == -1 || crar == -1)
    return -1;

  double *ratio = (double*)malloc((biggest_crar-crar+1)*sizeof(double));
  int i, r, r_delta=1; 
  int x, y, x2, y2;
  double sum, sum2;
  for (r = crar; r <= biggest_crar; r++) {
    sum = 0;
    sum2 = 0;
    for (i = 0; i < array_len; i++) {
      x = (int)(crx + (r+r_delta)*cos_array[i]);
      y = (int)(cry + (r+r_delta)*sin_array[i]);
      x2 = (int)(crx + (r-r_delta)*cos_array[i]);
      y2 = (int)(cry + (r+r_delta)*sin_array[i]);
      if ((x >= 0 && y >=0 && x < image->width && y < image->height) &&
          (x2 >= 0 && y2 >=0 && x2 < image->width && y2 < image->height)) {
        sum += *(image->imageData+y*image->width+x);
        sum2 += *(image->imageData+y2*image->width+x2);
      }
    }
    ratio[r-crar] = sum / sum2;
    if (r - crar >= 2) {
      if (ratio[r-crar-2] < ratio[r-crar-1] && ratio[r-crar] < ratio[r-crar-1]) {
        free(ratio);
        return r-1;
      }
    } 
  }
  
  free(ratio);
  printf("ATTN! fit_circle_radius_to_corneal_reflection() do not change the radius\n");
  return crar;
}

void interpolate_corneal_reflection(IplImage *image, int crx, int cry, int crr, double *sin_array, double *cos_array, 
int array_len)
{
  if (crx == -1 || cry == -1 || crr == -1)
    return;

  if (crx-crr < 0 || crx+crr >= image->width || cry-crr < 0 || cry+crr >= image->height) {
    printf("Error! Corneal reflection is too near the image border\n");
    return;
  }

  int i, r, r2,  x, y;
  UINT8 *perimeter_pixel = (UINT8*)malloc(array_len*sizeof(int));
  int sum=0, pixel_value;
  double avg;
  for (i = 0; i < array_len; i++) {
    x = (int)(crx + crr*cos_array[i]);
    y = (int)(cry + crr*sin_array[i]);
    perimeter_pixel[i] = (UINT8)(*(image->imageData+y*image->width+x));
    sum += perimeter_pixel[i];
  }
  avg = sum*1.0/array_len;

  for (r = 1; r < crr; r++) {
    r2 = crr-r;
    for (i = 0; i < array_len; i++) {
      x = (int)(crx + r*cos_array[i]);
      y = (int)(cry + r*sin_array[i]);
      *(image->imageData+y*image->width+x) = (UINT8)((r2*1.0/crr)*avg + (r*1.0/crr)*perimeter_pixel[i]);
    }
    //printf("r=%d: %d (avg:%lf, end:%d)\n", r, (UINT8)((r2*1.0/crr)*avg + (r*1.0/crr)*perimeter_pixel[i-1]),
    //       avg, perimeter_pixel[i-1]);
  }
  free(perimeter_pixel);
}

#endif