12 #include "edge/BgImage.h"
13 #include "edge/BgEdge.h"
14 #include "edge/BgEdgeList.h"
15 #include "edge/BgEdgeDetect.h"
16 #include "edge/BgDefaults.h"
21 #define SIGN(x) (x<0)? -1:1;
23 static int my_sign(
double val)
32 extern double factorial(
double);
34 BgEdgeDetect::BgEdgeDetect(
int filtDim)
41 tcustx_ =
new float[MAX_CUSTT];
42 tcusty_ =
new float[MAX_CUSTT];
47 BgEdgeDetect::~BgEdgeDetect()
55 delete [] permNmxRank_;
56 delete [] permNmxConf_;
73 void BgEdgeDetect::IsGood(
void)
82 float BgEdgeDetect::EllipseEval(
float x,
float y)
84 return ((x*x)/(rankTr_*rankTr_)+(y*y)/(confTr_*confTr_)-1);
87 float BgEdgeDetect::EllipseComp(
float x0,
float y0,
float x,
float y)
90 return ((x*x-x0*x0)/(rankTr_*rankTr_)+(y*y-y0*y0)/(confTr_*confTr_));
93 float BgEdgeDetect::LineEval(
float x,
float y)
95 return (confTr_*x+rankTr_*y-confTr_*rankTr_);
98 float BgEdgeDetect::LineComp(
float x0,
float y0,
float x,
float y)
101 return (confTr_*(x-x0)+rankTr_*(y-y0));
104 float BgEdgeDetect::VerticalLineEval(
float x,
float y)
109 float BgEdgeDetect::VerticalLineComp(
float x0,
float y0,
float x,
float y)
116 float BgEdgeDetect::HorizontalLineEval(
float x,
float y)
121 float BgEdgeDetect::HorizontalLineComp(
float x0,
float y0,
float x,
float y)
127 float BgEdgeDetect::SquareEval(
float x,
float y)
129 if ((x/rankTr_)>(y/confTr_))
135 float BgEdgeDetect::SquareComp(
float x0,
float y0,
float x,
float y)
139 tret = ((x/rankTr_)>(y/confTr_)) ? x-rankTr_ : y-confTr_;
140 tret -= ((x0/rankTr_)>(y0/confTr_)) ? x0-rankTr_ : y0-confTr_;
144 float BgEdgeDetect::CustomRegionEval(
float r,
float c)
149 if ((r+c)<=ZERO_TRESH)
156 for (i=0; i<ncust_; i++)
158 tcustx_[i]=custx_[i]-r;
159 tcusty_[i]=custy_[i]-c;
162 for (i=0; i<(ncust_-1); i++)
164 if ( (tcusty_[i] >0 && tcusty_[i+1]<=0) ||
165 (tcusty_[i+1]>0 && tcusty_[i] <=0) )
167 x = (tcustx_[i]*tcusty_[i+1]-tcustx_[i+1]*tcusty_[i])/(tcusty_[i+1]-tcusty_[i]);
173 if ((crossings % 2) ==1)
179 float BgEdgeDetect::CustomRegionComp(
float r0,
float c0,
float r,
float c)
184 void BgEdgeDetect::GenerateMaskAngle(
double* a,
double theta) {
187 static double cval[4];
188 static double corner[2][4];
189 static double sinv,cosv;
190 static double intrs[2][4];
191 static int scor[4],nscor[4];
192 static int sind,rowind,colind;
193 static double cordi[2][4];
194 static int lsigind,corin;
195 static int sigind[4];
196 static double diffin[2];
197 static double comcoor;
199 theta = theta*PI/180.0;
203 for (i=0; i<WW_*WW_; i++)
206 for (i=WL_; i>=-WL_; i--)
208 for(j=-WL_; j<=WL_; j++)
210 corner[0][0] = j-0.5;
211 corner[0][1] = j+0.5;
212 corner[0][2] = j+0.5;
213 corner[0][3] = j-0.5;
215 corner[1][0] = i+0.5;
216 corner[1][1] = i+0.5;
217 corner[1][2] = i-0.5;
218 corner[1][3] = i-0.5;
220 cval[0] = -sinv*corner[0][0]+cosv*corner[1][0];
221 cval[1] = -sinv*corner[0][1]+cosv*corner[1][1];
222 cval[2] = -sinv*corner[0][2]+cosv*corner[1][2];
223 cval[3] = -sinv*corner[0][3]+cosv*corner[1][3];
225 scor[0] = my_sign(cval[0]);
226 scor[1] = my_sign(cval[1]);
227 scor[2] = my_sign(cval[2]);
228 scor[3] = my_sign(cval[3]);
232 nscor[sind++] = scor[0];
234 nscor[sind++] = scor[1];
236 nscor[sind++] = scor[2];
238 nscor[sind++] = scor[3];
243 if (nscor[k]!=nscor[0])
253 a[colind+rowind*WW_] = 1.0;
255 a[colind+rowind*WW_] = 0.0;
261 intrs[0][k] = intrs[1][k] = 0.0;
265 intrs[0][0] = corner[0][0];
266 intrs[1][0] = corner[1][0];
268 if (scor[0]*scor[1]<0)
270 intrs[0][0] = corner[1][0]*cosv/sinv;
271 intrs[1][0] = corner[1][0];
275 intrs[0][1] = corner[0][1];
276 intrs[1][1] = corner[1][1];
278 if (scor[1]*scor[2]<0)
280 intrs[0][1] = corner[0][1];
281 intrs[1][1] = corner[0][1]*sinv/cosv;
285 intrs[0][2] = corner[0][2];
286 intrs[1][2] = corner[1][2];
288 if (scor[2]*scor[3]<0)
290 intrs[0][2] = corner[1][2]*cosv/sinv;
291 intrs[1][2] = corner[1][2];
295 intrs[0][3] = corner[0][3];
296 intrs[1][3] = corner[1][3];
298 if (scor[3]*scor[0]<0)
300 intrs[0][3] = corner[0][3];
301 intrs[1][3] = corner[0][3]*sinv/cosv;
305 if (fabs(intrs[0][0])>TOL_E || fabs(intrs[1][0])>TOL_E)
307 cordi[0][corin] = intrs[0][0];
308 cordi[1][corin++] = intrs[1][0];
310 if (fabs(intrs[0][1])>TOL_E || fabs(intrs[1][1])>TOL_E)
312 cordi[0][corin] = intrs[0][1];
313 cordi[1][corin++] = intrs[1][1];
315 if (fabs(intrs[0][2])>TOL_E || fabs(intrs[1][2])>TOL_E)
317 cordi[0][corin] = intrs[0][2];
318 cordi[1][corin++] = intrs[1][2];
320 if (fabs(intrs[0][3])>TOL_E || fabs(intrs[1][3])>TOL_E)
322 cordi[0][corin] = intrs[0][3];
323 cordi[1][corin++] = intrs[1][3];
328 sigind[lsigind++] = 0;
330 sigind[lsigind++] = 1;
332 sigind[lsigind++] = 2;
334 sigind[lsigind++] = 3;
338 a[colind+rowind*WW_] = 0.5*fabs(cordi[0][0]-cordi[0][1])*fabs(cordi[1][0]-cordi[1][1]);
342 diffin[0] = (int) fabs(cordi[0][0]-cordi[0][1]);
343 diffin[1] = (int) fabs(cordi[1][0]-cordi[1][1]);
346 comcoor = corner[1][sigind[0]];
347 a[colind+rowind*WW_] = 0.5*(fabs(comcoor-cordi[1][0])+fabs(comcoor-cordi[1][1]));
351 comcoor = corner[0][sigind[0]];
352 a[colind+rowind*WW_] = 0.5*(fabs(comcoor-cordi[0][0])+fabs(comcoor-cordi[0][1]));
357 a[colind+rowind*WW_] = 1.0-0.5*fabs(cordi[0][0]-cordi[0][1])*fabs(cordi[1][0]-cordi[1][1]);
365 for (i=0; i<WW_*WW_; i++)
368 for (i=0; i<WW_*WW_; i++)
373 for (i=0; i<WW_*WW_; i++)
374 comcoor += a[i]*a[i];
375 comcoor = sqrt(comcoor);
376 for (i=0; i<WW_*WW_; i++)
380 void BgEdgeDetect::CreateFilters(
void)
384 for (i=-WL_; i<=WL_; i++)
386 w = pow(2.0,(-2*WL_))*factorial(2*WL_)/(factorial(WL_-i)*factorial(WL_+i));
388 diffil_[i+WL_] = (2*i*w)/WL_;
390 for (j=0; j<WW_; j++)
392 for (i=0; i<WW_; i++)
394 wdy_[j+i*WW_] = wdx_[i+j*WW_] = smofil_[j]*diffil_[i];
400 for (i=0; i<WW_; i++)
402 norms += smofil_[i]*smofil_[i];
403 normd += diffil_[i]*diffil_[i];
406 for (j=0; j<WW_; j++)
408 for (i=0; i<WW_; i++)
410 mQ_[i][j] = (smofil_[j]*smofil_[i])/norms + (diffil_[j]*diffil_[i])/normd;
411 mN_[i][j] = (i==j) ? 1-mQ_[i][j] : -mQ_[i][j];
416 void BgEdgeDetect::CreateLookTable()
418 bgLog(
"Creating angle lookup table\n");
420 for (i=-180; i<=180; i++)
422 lookTable_[i+180] =
new double[WW_*WW_];
423 GenerateMaskAngle(lookTable_[i+180], (
double) i);
427 void BgEdgeDetect::DeleteLookTable()
430 for (i=0; i<NO_ANGLES; i++)
432 delete [] lookTable_[i];
436 void BgEdgeDetect::GetPixels(
int* nopix,
int* pixx,
int* pixy,
double x1,
double x2,
double y1,
double y2)
438 double minx,maxx,miny,maxy;
468 if (permRank_[i+j*x_]<maxx && permRank_[i+j*x_]>minx &&
469 permConf_[i+j*x_]<maxy && permConf_[i+j*x_]>miny)
479 void BgEdgeDetect::GetNmxPixels(
int* nopix,
int* pixx,
int* pixy,
double x1,
double x2,
double y1,
double y2)
481 double minx,maxx,miny,maxy;
510 if (permNmxRank_[i+j*x_]<maxx && permNmxRank_[i+j*x_]>minx &&
511 permNmxConf_[i+j*x_]<maxy && permNmxConf_[i+j*x_]>miny)
521 void BgEdgeDetect::DoRecompute(BgEdgeList* cel,
double nmxr,
double nmxc,
522 double rh,
double ch,
double rl,
double cl,
523 int nMin,
int nmxType,
int hystTypeHigh,
int hystTypeLow)
525 float *tr, *tc, *tdh, *tdl;
526 bgLog(
"Start edge detection...\n");
527 tr =
new float[x_*y_];
528 tc =
new float[x_*y_];
529 tdh =
new float[x_*y_];
530 tdl =
new float[x_*y_];
533 bgLog(
"...nonmaxima supression: ");
535 float (BgEdgeDetect::*fcomp)(float,float,float,float);
536 float (BgEdgeDetect::*feval)(float,float);
541 fcomp = &BgEdgeDetect::EllipseComp;
542 feval = &BgEdgeDetect::EllipseEval;
546 fcomp = &BgEdgeDetect::VerticalLineComp;
547 feval = &BgEdgeDetect::VerticalLineEval;
548 bgLog(
"vertical line\n");
551 fcomp = &BgEdgeDetect::HorizontalLineComp;
552 feval = &BgEdgeDetect::HorizontalLineEval;
553 bgLog(
"horizontal line\n");
556 fcomp = &BgEdgeDetect::SquareComp;
557 feval = &BgEdgeDetect::SquareEval;
561 fcomp = &BgEdgeDetect::LineComp;
562 feval = &BgEdgeDetect::LineEval;
566 bgLog(
"Type not known\n");
569 confTr_ = (float) nmxc;
570 rankTr_ = (float) nmxr;
572 NewNonMaxSupress(permRank_,permConf_,permGx_,permGy_,permNmxRank_,permNmxConf_,fcomp);
573 bgLog(
"...hysteresis thresholding, high: ");
578 fcomp = &BgEdgeDetect::EllipseComp;
579 feval = &BgEdgeDetect::EllipseEval;
583 fcomp = &BgEdgeDetect::VerticalLineComp;
584 feval = &BgEdgeDetect::VerticalLineEval;
585 bgLog(
"vertical line");
588 fcomp = &BgEdgeDetect::HorizontalLineComp;
589 feval = &BgEdgeDetect::HorizontalLineEval;
590 bgLog(
"horizontal line");
593 fcomp = &BgEdgeDetect::SquareComp;
594 feval = &BgEdgeDetect::SquareEval;
598 fcomp = &BgEdgeDetect::LineComp;
599 feval = &BgEdgeDetect::LineEval;
606 fcomp = &BgEdgeDetect::CustomRegionComp;
607 feval = &BgEdgeDetect::CustomRegionEval;
611 confTr_ = (float) ch;
612 rankTr_ = (float) rh;
613 StrConfEstim(permNmxRank_, permNmxConf_, tdh, feval);
617 switch(hystTypeLow) {
619 fcomp = &BgEdgeDetect::EllipseComp;
620 feval = &BgEdgeDetect::EllipseEval;
624 fcomp = &BgEdgeDetect::VerticalLineComp;
625 feval = &BgEdgeDetect::VerticalLineEval;
626 bgLog(
"vertical line\n");
629 fcomp = &BgEdgeDetect::HorizontalLineComp;
630 feval = &BgEdgeDetect::HorizontalLineEval;
631 bgLog(
"horizontal line\n");
634 fcomp = &BgEdgeDetect::SquareComp;
635 feval = &BgEdgeDetect::SquareEval;
639 fcomp = &BgEdgeDetect::LineComp;
640 feval = &BgEdgeDetect::LineEval;
647 fcomp = &BgEdgeDetect::CustomRegionComp;
648 feval = &BgEdgeDetect::CustomRegionEval;
652 confTr_ = (float) cl;
653 rankTr_ = (float) rl;
654 StrConfEstim(permNmxRank_, permNmxConf_, tdl, feval);
661 NewHysteresisTr(tdh, tdl, cel, minpt, tr, tc);
662 bgLog(
"Done edge detection.\n");
670 void BgEdgeDetect::SaveNmxValues()
675 fd = fopen(
"ranknmx.dat",
"w");
680 fprintf(fd,
"%f ", *(permNmxRank_+j*x_+i));
686 fd=fopen(
"confnmx.dat",
"w");
691 fprintf(fd,
"%f ", *(permNmxConf_+j*x_+i));
702 void BgEdgeDetect::ComputeEdgeInfo(BgImage* cim,
float* confMap,
float *rank)
706 bgLog(
"Computing confidence map...\n");
707 float *pGx, *pGy, *pTemp;
708 BgImage tcim(x_, y_);
709 if (cim->colorIm_==1)
711 tcim.SetImageFromRGB(cim->im_, x_, y_,
false);
717 pGx =
new float[x_*y_];
718 pGy =
new float[x_*y_];
719 pTemp =
new float[x_*y_];
722 bgLog(
"...smooth-differentiation filtering\n");
723 GaussDiffFilter(&tcim, pGx, pGy, pTemp);
726 bgLog(
"...subspace estimate\n");
727 SubspaceEstim(pTemp, pGx, pGy, confMap);
730 bgLog(
"...edge strengths\n");
731 Strength(pGx, pGy, pTemp);
734 bgLog(
"...computing ranks\n");
735 CompRanks(pTemp, rank);
757 void BgEdgeDetect::DoEdgeDetect(BgImage* cim, BgEdgeList* cel,
double nmxr,
double nmxc,
758 double rh,
double ch,
double rl,
double cl,
759 int nMin,
int nmxType,
int hystTypeHigh,
int hystTypeLow)
763 bgLog(
"Start edge detection...\n");
764 permGx_ =
new float[x_*y_];
765 permGy_ =
new float[x_*y_];
766 permConf_ =
new float[x_*y_];
767 permRank_ =
new float[x_*y_];
768 permNmxRank_ =
new float[x_*y_];
769 permNmxConf_ =
new float[x_*y_];
776 tr =
new float[x_*y_];
777 tc =
new float[x_*y_];
778 tdh =
new float[x_*y_];
779 tdl =
new float[x_*y_];
782 bgLog(
"...smooth-differentiation filtering\n");
783 GaussDiffFilter(cim, permGx_, permGy_, tr);
786 bgLog(
"...subspace estimate\n");
787 SubspaceEstim(tr, permGx_, permGy_, permConf_);
790 bgLog(
"...edge strengths\n");
791 Strength(permGx_, permGy_, tr);
794 bgLog(
"...computing ranks\n");
795 CompRanks(tr, permRank_);
798 bgLog(
"...nonmaxima supression: ");
801 float (BgEdgeDetect::*fcomp)(float,float,float,float);
802 float (BgEdgeDetect::*feval)(float,float);
806 fcomp = &BgEdgeDetect::EllipseComp;
807 feval = &BgEdgeDetect::EllipseEval;
811 fcomp = &BgEdgeDetect::VerticalLineComp;
812 feval = &BgEdgeDetect::VerticalLineEval;
813 bgLog(
"vertical line\n");
816 fcomp = &BgEdgeDetect::HorizontalLineComp;
817 feval = &BgEdgeDetect::HorizontalLineEval;
818 bgLog(
"horizontal line\n");
821 fcomp = &BgEdgeDetect::SquareComp;
822 feval = &BgEdgeDetect::SquareEval;
826 fcomp = &BgEdgeDetect::LineComp;
827 feval = &BgEdgeDetect::LineEval;
831 bgLog(
"Type not known\n");
835 confTr_ = (float) nmxc;
836 rankTr_ = (float) nmxr;
837 NewNonMaxSupress(permRank_, permConf_, permGx_, permGy_, permNmxRank_, permNmxConf_, fcomp);
840 bgLog(
"...hysteresis thresholding, high: ");
846 fcomp = &BgEdgeDetect::EllipseComp;
847 feval = &BgEdgeDetect::EllipseEval;
851 fcomp = &BgEdgeDetect::VerticalLineComp;
852 feval = &BgEdgeDetect::VerticalLineEval;
853 bgLog(
"vertical line");
856 fcomp = &BgEdgeDetect::HorizontalLineComp;
857 feval = &BgEdgeDetect::HorizontalLineEval;
858 bgLog(
"horizontal line");
861 fcomp = &BgEdgeDetect::SquareComp;
862 feval = &BgEdgeDetect::SquareEval;
866 fcomp = &BgEdgeDetect::LineComp;
867 feval = &BgEdgeDetect::LineEval;
874 fcomp = &BgEdgeDetect::CustomRegionComp;
875 feval = &BgEdgeDetect::CustomRegionEval;
880 confTr_ = (float) ch;
881 rankTr_ = (float) rh;
882 StrConfEstim(permNmxRank_, permNmxConf_, tdh, feval);
890 fcomp = &BgEdgeDetect::EllipseComp;
891 feval = &BgEdgeDetect::EllipseEval;
895 fcomp = &BgEdgeDetect::VerticalLineComp;
896 feval = &BgEdgeDetect::VerticalLineEval;
897 bgLog(
"vertical line\n");
900 fcomp = &BgEdgeDetect::HorizontalLineComp;
901 feval = &BgEdgeDetect::HorizontalLineEval;
902 bgLog(
"horizontal line\n");
905 fcomp = &BgEdgeDetect::SquareComp;
906 feval = &BgEdgeDetect::SquareEval;
910 fcomp = &BgEdgeDetect::LineComp;
911 feval = &BgEdgeDetect::LineEval;
918 fcomp = &BgEdgeDetect::CustomRegionComp;
919 feval = &BgEdgeDetect::CustomRegionEval;
923 confTr_ = (float) cl;
924 rankTr_ = (float) rl;
926 StrConfEstim(permNmxRank_, permNmxConf_, tdl, feval);
931 NewHysteresisTr(tdh, tdl, cel, nMin, tr, tc);
933 bgLog(
"Done edge detection.\n");
941 void BgEdgeDetect::SubspaceEstim(
float* im,
float* grx,
float* gry,
float* cee)
959 ti =
new double[WW_*WW_];
971 for (j=WL_; j<(y_-WL_); j++)
973 for (i=0; i<WL_; i++)
981 for (i=WL_; i<(x_-WL_); i++, itim++, itgx++, itgy++, itcee++)
983 if ((fabs(*itgx)+fabs(*itgy))>TOL_E)
985 angleEdge = (-atan2(*itgx, *itgy))*180.0/PI;
986 tae = lookTable_[(int) (angleEdge+180.49)];
990 for (l=0; l<WW_; l++)
992 for (c=0; c<WW_; c++)
994 v1 += ti[l*WW_+c] = *(itim+(l-WL_)*x_+c-WL_);
998 for (l=0; l<WW2; l++)
1003 for (l=0; l<WW2; l++)
1006 for (l=0; l<WW2; l++)
1011 for (l=0; l<WW2; l++)
1014 *itcee = (float) v1;
1021 for (i=0; i<WL_; i++)
1029 for (j=0; j<(WL_*x_); j++)
1039 void BgEdgeDetect::GaussFilter(BgImage* cim,
float* fim,
double sigma,
int width)
1051 for (i=0; i<x_*y_;i++)
1057 width = (int) (1+2*ceil(2.5*sigma));
1062 filter =
new double[width];
1063 for (i=-tail; i<=tail; i++)
1064 sum += filter[i+tail] = exp(-i*i/(2*sigma*sigma));
1065 for (i=0; i<width; i++)
1070 tim =
new double[x_*y_];
1071 for (j=0; j<y_; j++)
1073 for (i=tail; i<(x_-tail); i++)
1076 for (k=-tail; k<=tail; k++)
1077 sum += im[j*x_+i+k]*filter[k+tail];
1081 for (i=0; i<tail; i++)
1084 tim[j*x_+x_-i-1] = 0;
1085 for (k=-tail; k<=tail; k++)
1087 ii = (k+i)>=0 ? k+i : 0;
1088 tim[j*x_+i] += im[j*x_+ii]*filter[k+tail];
1089 ii = (x_-i-1+k)<x_ ? x_-i-1+k : x_-1;
1090 tim[j*x_+x_-i-1] += im[j*x_+ii]*filter[k+tail];
1095 for (i=0; i<x_; i++)
1097 for (j=tail; j<(y_-tail); j++)
1100 for (k=-tail; k<=tail; k++)
1101 sum += tim[(j+k)*x_+i]*filter[k+tail];
1102 fim[j*x_+i] = (float) (sum);
1104 for (j=0; j<tail; j++)
1108 for (k=-tail; k<=tail; k++)
1110 jj = (k+j)>=0 ? k+j : 0;
1111 sum += tim[jj*x_+i]*filter[k+tail];
1112 jj = (y_-j-1+k)<y_ ? y_-j-1+k : y_-1;
1113 sum1 += tim[jj*x_+i]*filter[k+tail];
1115 fim[j*x_+i] = (float) (sum);
1116 fim[(y_-j-1)*x_+i] = (
float) (sum1);
1123 void BgEdgeDetect::GaussDiffFilter(BgImage* cim,
float* grx,
float* gry,
float* rezIm)
1140 tim =
new double[x_*y_];
1141 for (i=0; i<x_*y_; i++)
1143 grx[i] = gry[i] = 0;
1150 for (i=0; i<x_; i++)
1152 for (j=WL_; j<(y_-WL_); j++)
1155 for (k=-WL_; k<=WL_; k++)
1156 sum += im[(j+k)*x_+i]*sf[k+WL_];
1161 for (j=0; j<y_; j++)
1163 for (i=WL_; i<(x_-WL_); i++)
1166 for (k=-WL_; k<=WL_; k++)
1167 sum += tim[j*x_+i+k]*df[k+WL_];
1168 grx[j*x_+i] = (float) (sum);
1173 for (i=0; i<x_*y_;i++)
1177 for (j=0; j<y_; j++)
1179 for (i=WL_; i<(x_-WL_); i++)
1182 for (k=-WL_; k<=WL_; k++)
1183 sum += im[j*x_+i+k]*sf[k+WL_];
1188 for (i=0; i<x_; i++)
1190 for (j=WL_; j<(y_-WL_); j++)
1193 for (k=-WL_; k<=WL_; k++)
1194 sum += tim[(j+k)*x_+i]*df[k+WL_];
1195 gry[j*x_+i] = (float) (sum);
1201 void BgEdgeDetect::CompRanks(
float* strength,
float* ranks)
1205 ra =
new float[x_*y_];
1206 index =
new int[x_*y_];
1209 for (ii=0; ii<x_*y_; ii++)
1213 ra[ii] = strength[ii];
1217 unsigned long i, ir, j, l;
1239 index[ir-1] = index[1-1];
1251 if (j<ir && ra[j-1]<ra[j+1-1])
1256 index[i-1] = index[j-1];
1269 for (ii=1; ii<x_*y_; ii++)
1271 if (ra[ii]>ZERO_TRESH)
1273 ranks[index[ii]] = (float) irra;
1274 if (ra[ii]>ra[ii-1])
1279 for (ii=0; ii<x_*y_; ii++)
1286 void BgEdgeDetect::StrConfEstim(
float* ranks,
float* confidence,
float* rezult,
1287 float (BgEdgeDetect::*feval)(
float,
float))
1290 for (i=0; i<x_*y_; i++)
1292 rezult[i] = (this->*feval)(ranks[i], confidence[i]);
1296 void BgEdgeDetect::Strength(
float* grx,
float* gry,
float* strength)
1306 for (j=0; j<y_; j++)
1309 val = sqrt(((
double) (*itgx*(*(itgx++))))+((
double) (*itgy*(*(itgy++)))));
1310 *(its++)=(
float) (val);
1314 void BgEdgeDetect::NewNonMaxSupress(
float* rank,
float* conf,
float* grx,
float* gry,
float* nmxRank,
float* nmxConf,
1315 float (BgEdgeDetect::*feval)(
float,
float,
float,
float))
1324 float alpha,r1,c1,r2,c2,lambda;
1332 for (i=0; i<x_*y_; i++)
1334 itnmxr[i] = itnmxc[i] = 0;
1338 itr[i] = itc[i] = 0;
1339 itr[(y_-1)*x_+i] = itc[(y_-1)*x_+i] = 0;
1343 itr[j*x_] = itc[j*x_] = 0;
1344 itr[j*x_+x_-1] = itc[j*x_+x_-1] = 0;
1347 for (j=0; j<y_; j++)
1349 for (i=0; i<x_; i++, itr++, itc++, itgx++, itgy++, itnmxr++, itnmxc++)
1351 if (*itr>0 && *itc>0)
1353 alpha = (float) atan2(*itgy, *itgx);
1354 alpha = (alpha<0) ? alpha+(
float)PI : alpha;
1357 lambda = (float) tan(alpha);
1358 r1 = (1-lambda)*(*(itr+1))+lambda*(*(itr+x_+1));
1359 c1 = (1-lambda)*(*(itc+1))+lambda*(*(itc+x_+1));
1360 r2 = (1-lambda)*(*(itr-1))+lambda*(*(itr-x_-1));
1361 c2 = (1-lambda)*(*(itc-1))+lambda*(*(itc-x_-1));
1362 if ((this->*feval)(*itr, *itc, r1, c1)<0 && (this->*feval)(*itr, *itc, r2, c2)<=0)
1368 else if (alpha<=PI/2)
1370 lambda = (float) tan(PI/2-alpha);
1371 r1 = (1-lambda)*(*(itr+x_))+lambda*(*(itr+x_+1));
1372 c1 = (1-lambda)*(*(itc+x_))+lambda*(*(itc+x_+1));
1373 r2 = (1-lambda)*(*(itr-x_))+lambda*(*(itr-x_-1));
1374 c2 = (1-lambda)*(*(itc-x_))+lambda*(*(itc-x_-1));
1375 if ((this->*feval)(*itr, *itc, r1, c1)<0 && (this->*feval)(*itr, *itc, r2, c2)<=0)
1382 else if(alpha<=3*PI/4)
1384 lambda = (float) tan(alpha-PI/2);
1385 r1 = (1-lambda)*(*(itr+x_))+lambda*(*(itr+x_-1));
1386 c1 = (1-lambda)*(*(itc+x_))+lambda*(*(itc+x_-1));
1387 r2 = (1-lambda)*(*(itr-x_))+lambda*(*(itr-x_+1));
1388 c2 = (1-lambda)*(*(itc-x_))+lambda*(*(itc-x_+1));
1389 if ((this->*feval)(*itr, *itc, r1, c1)<0 && (this->*feval)(*itr, *itc, r2, c2)<=0)
1397 lambda = (float) tan(PI-alpha);
1398 r1 = (1-lambda)*(*(itr-1))+lambda*(*(itr+x_-1));
1399 c1 = (1-lambda)*(*(itc-1))+lambda*(*(itc+x_-1));
1400 r2 = (1-lambda)*(*(itr+1))+lambda*(*(itr-x_+1));
1401 c2 = (1-lambda)*(*(itc+1))+lambda*(*(itc-x_+1));
1402 if ((this->*feval)(*itr, *itc, r1, c1)<0 && (this->*feval)(*itr, *itc, r2, c2)<=0)
1413 void BgEdgeDetect::NewHysteresisTr(
float* edge,
float* low, BgEdgeList* cel,
int nMin,
float* mark,
float* coord)
1421 for (i=0, tm=mark; i<x_*y_; i++,tm++)
1428 for (j=0; j<y_; j++)
1430 for (i=0; i<x_; i++, tm++, te++)
1432 if ((*tm==0) && ((*te)>HYST_LOW_CUT))
1438 NewEdgeFollow(i, j);
1440 if (npt_>=nMin) cel->AddEdge(coord, npt_);
1446 void BgEdgeDetect::NewEdgeFollow(
int ii,
int jj)
1454 if ((tm_[jjn*x_+iin]==0) && ((tl_[jjn*x_+iin])>0))
1456 tm_[jjn*x_+iin] = 1;
1457 NewEdgeFollow(iin, jjn);
1460 *(tc_++) = (
float) ii;
1461 *(tc_++) = (
float) jj;
1465 void BgEdgeDetect::SetCustomHigh(
int* x,
int* y,
int n,
int sx,
int sy)
1473 hcustx_ = hcusty_ = 0;
1475 hcustx_ =
new float[nhcust_];
1476 hcusty_ =
new float[nhcust_];
1484 hcustx_[idx] = ((float) x[i])/sx;
1485 hcusty_[idx++] = (float)(1.0-((
float) y[i])/sy);
1490 bgLog(
" hyst high custom x: ");
1491 for (i=0; i<=n; i++)
1492 bgLog(
" %f", hcustx_[i]);
1494 bgLog(
" hist high custom y: ");
1495 for (i=0; i<=n; i++)
1496 bgLog(
" %f", hcusty_[i]);
1500 void BgEdgeDetect::SetCustomHigh(
double* x,
double* y,
int n)
1508 hcustx_ = hcusty_ = 0;
1510 hcustx_ =
new float[nhcust_];
1511 hcusty_ =
new float[nhcust_];
1519 hcustx_[idx] = (float) x[i];
1520 hcusty_[idx++] = (float) y[i];
1525 bgLog(
" hyst high custom x: ");
1526 for (i=0; i<=n; i++)
1527 bgLog(
" %f", hcustx_[i]);
1529 bgLog(
" hist high custom y: ");
1530 for (i=0; i<=n; i++)
1531 bgLog(
" %f", hcusty_[i]);
1535 void BgEdgeDetect::SetCustomLow(
int* x,
int* y,
int n,
int sx,
int sy)
1543 lcustx_ = lcusty_ = 0;
1545 lcustx_ =
new float[nlcust_];
1546 lcusty_ =
new float[nlcust_];
1554 lcustx_[idx] = ((float) x[i])/sx;
1555 lcusty_[idx++] = (float)(1.0-((
float) y[i])/sy);
1559 bgLog(
" hyst low custom x: ");
1560 for (i=0; i<=n; i++)
1561 bgLog(
" %f", lcustx_[i]);
1563 bgLog(
" low custom y: ");
1564 for (i=0; i<=n; i++)
1565 bgLog(
" %f", lcusty_[i]);
1569 void BgEdgeDetect::SetCustomLow(
double* x,
double* y,
int n)
1577 lcustx_ = lcusty_ = 0;
1579 lcustx_ =
new float[nlcust_];
1580 lcusty_ =
new float[nlcust_];
1588 lcustx_[idx] = (float) x[i];
1589 lcusty_[idx++] = (float) y[i];
1593 bgLog(
" hyst low custom x: ");
1594 for (i=0; i<=n; i++)
1595 bgLog(
" %f", lcustx_[i]);
1597 bgLog(
" low custom y: ");
1598 for (i=0; i<=n; i++)
1599 bgLog(
" %f", lcusty_[i]);