maskApi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**************************************************************************
  2. * Microsoft COCO Toolbox. version 2.0
  3. * Data, paper, and tutorials available at: http://mscoco.org/
  4. * Code written by Piotr Dollar and Tsung-Yi Lin, 2015.
  5. * Licensed under the Simplified BSD License [see coco/license.txt]
  6. **************************************************************************/
  7. #include "maskApi.h"
  8. #include <math.h>
  9. #include <stdlib.h>
  10. uint umin( uint a, uint b ) { return (a<b) ? a : b; }
  11. uint umax( uint a, uint b ) { return (a>b) ? a : b; }
  12. void rleInit( RLE *R, siz h, siz w, siz m, uint *cnts ) {
  13. R->h=h; R->w=w; R->m=m; R->cnts=(m==0)?0:malloc(sizeof(uint)*m);
  14. if(cnts) for(siz j=0; j<m; j++) R->cnts[j]=cnts[j];
  15. }
  16. void rleFree( RLE *R ) {
  17. free(R->cnts); R->cnts=0;
  18. }
  19. void rlesInit( RLE **R, siz n ) {
  20. *R = (RLE*) malloc(sizeof(RLE)*n);
  21. for(siz i=0; i<n; i++) rleInit((*R)+i,0,0,0,0);
  22. }
  23. void rlesFree( RLE **R, siz n ) {
  24. for(siz i=0; i<n; i++) rleFree((*R)+i); free(*R); *R=0;
  25. }
  26. void rleEncode( RLE *R, const byte *M, siz h, siz w, siz n ) {
  27. siz i, j, k, a=w*h; uint c, *cnts; byte p;
  28. cnts = malloc(sizeof(uint)*(a+1));
  29. for(i=0; i<n; i++) {
  30. const byte *T=M+a*i; k=0; p=0; c=0;
  31. for(j=0; j<a; j++) { if(T[j]!=p) { cnts[k++]=c; c=0; p=T[j]; } c++; }
  32. cnts[k++]=c; rleInit(R+i,h,w,k,cnts);
  33. }
  34. free(cnts);
  35. }
  36. void rleDecode( const RLE *R, byte *M, siz n ) {
  37. for( siz i=0; i<n; i++ ) {
  38. byte v=0; for( siz j=0; j<R[i].m; j++ ) {
  39. for( siz k=0; k<R[i].cnts[j]; k++ ) *(M++)=v; v=!v; }}
  40. }
  41. void rleMerge( const RLE *R, RLE *M, siz n, bool intersect ) {
  42. uint *cnts, c, ca, cb, cc, ct; bool v, va, vb, vp;
  43. siz i, a, b, h=R[0].h, w=R[0].w, m=R[0].m; RLE A, B;
  44. if(n==0) { rleInit(M,0,0,0,0); return; }
  45. if(n==1) { rleInit(M,h,w,m,R[0].cnts); return; }
  46. cnts = malloc(sizeof(uint)*(h*w+1));
  47. for( a=0; a<m; a++ ) cnts[a]=R[0].cnts[a];
  48. for( i=1; i<n; i++ ) {
  49. B=R[i]; if(B.h!=h||B.w!=w) { h=w=m=0; break; }
  50. rleInit(&A,h,w,m,cnts); ca=A.cnts[0]; cb=B.cnts[0];
  51. v=va=vb=0; m=0; a=b=1; cc=0; ct=1;
  52. while( ct>0 ) {
  53. c=umin(ca,cb); cc+=c; ct=0;
  54. ca-=c; if(!ca && a<A.m) { ca=A.cnts[a++]; va=!va; } ct+=ca;
  55. cb-=c; if(!cb && b<B.m) { cb=B.cnts[b++]; vb=!vb; } ct+=cb;
  56. vp=v; if(intersect) v=va&&vb; else v=va||vb;
  57. if( v!=vp||ct==0 ) { cnts[m++]=cc; cc=0; }
  58. }
  59. rleFree(&A);
  60. }
  61. rleInit(M,h,w,m,cnts); free(cnts);
  62. }
  63. void rleArea( const RLE *R, siz n, uint *a ) {
  64. for( siz i=0; i<n; i++ ) {
  65. a[i]=0; for( siz j=1; j<R[i].m; j+=2 ) a[i]+=R[i].cnts[j]; }
  66. }
  67. void rleIou( RLE *dt, RLE *gt, siz m, siz n, byte *iscrowd, double *o ) {
  68. siz g, d; BB db, gb; bool crowd;
  69. db=malloc(sizeof(double)*m*4); rleToBbox(dt,db,m);
  70. gb=malloc(sizeof(double)*n*4); rleToBbox(gt,gb,n);
  71. bbIou(db,gb,m,n,iscrowd,o); free(db); free(gb);
  72. for( g=0; g<n; g++ ) for( d=0; d<m; d++ ) if(o[g*m+d]>0) {
  73. crowd=iscrowd!=NULL && iscrowd[g];
  74. if(dt[d].h!=gt[g].h || dt[d].w!=gt[g].w) { o[g*m+d]=-1; continue; }
  75. siz ka, kb, a, b; uint c, ca, cb, ct, i, u; bool va, vb;
  76. ca=dt[d].cnts[0]; ka=dt[d].m; va=vb=0;
  77. cb=gt[g].cnts[0]; kb=gt[g].m; a=b=1; i=u=0; ct=1;
  78. while( ct>0 ) {
  79. c=umin(ca,cb); if(va||vb) { u+=c; if(va&&vb) i+=c; } ct=0;
  80. ca-=c; if(!ca && a<ka) { ca=dt[d].cnts[a++]; va=!va; } ct+=ca;
  81. cb-=c; if(!cb && b<kb) { cb=gt[g].cnts[b++]; vb=!vb; } ct+=cb;
  82. }
  83. if(i==0) u=1; else if(crowd) rleArea(dt+d,1,&u);
  84. o[g*m+d] = (double)i/(double)u;
  85. }
  86. }
  87. void bbIou( BB dt, BB gt, siz m, siz n, byte *iscrowd, double *o ) {
  88. double h, w, i, u, ga, da; siz g, d; bool crowd;
  89. for( g=0; g<n; g++ ) {
  90. BB G=gt+g*4; ga=G[2]*G[3]; crowd=iscrowd!=NULL && iscrowd[g];
  91. for( d=0; d<m; d++ ) {
  92. BB D=dt+d*4; da=D[2]*D[3]; o[g*m+d]=0;
  93. w=fmin(D[2]+D[0],G[2]+G[0])-fmax(D[0],G[0]); if(w<=0) continue;
  94. h=fmin(D[3]+D[1],G[3]+G[1])-fmax(D[1],G[1]); if(h<=0) continue;
  95. i=w*h; u = crowd ? da : da+ga-i; o[g*m+d]=i/u;
  96. }
  97. }
  98. }
  99. void rleToBbox( const RLE *R, BB bb, siz n ) {
  100. for( siz i=0; i<n; i++ ) {
  101. uint h, w, x, y, xs, ys, xe, ye, cc, t; siz j, m;
  102. h=(uint)R[i].h; w=(uint)R[i].w; m=R[i].m;
  103. m=((siz)(m/2))*2; xs=w; ys=h; xe=ye=0; cc=0;
  104. if(m==0) { bb[4*i+0]=bb[4*i+1]=bb[4*i+2]=bb[4*i+3]=0; continue; }
  105. for( j=0; j<m; j++ ) {
  106. cc+=R[i].cnts[j]; t=cc-j%2; y=t%h; x=(t-y)/h;
  107. xs=umin(xs,x); xe=umax(xe,x); ys=umin(ys,y); ye=umax(ye,y);
  108. }
  109. bb[4*i+0]=xs; bb[4*i+2]=xe-xs+1;
  110. bb[4*i+1]=ys; bb[4*i+3]=ye-ys+1;
  111. }
  112. }
  113. void rleFrBbox( RLE *R, const BB bb, siz h, siz w, siz n ) {
  114. for( siz i=0; i<n; i++ ) {
  115. double xs=bb[4*i+0], xe=xs+bb[4*i+2];
  116. double ys=bb[4*i+1], ye=ys+bb[4*i+3];
  117. double xy[8] = {xs,ys,xs,ye,xe,ye,xe,ys};
  118. rleFrPoly( R+i, xy, 4, h, w );
  119. }
  120. }
  121. int uintCompare(const void *a, const void *b) {
  122. uint c=*((uint*)a), d=*((uint*)b); return c>d?1:c<d?-1:0;
  123. }
  124. void rleFrPoly( RLE *R, const double *xy, siz k, siz h, siz w ) {
  125. // upsample and get discrete points densely along entire boundary
  126. siz j, m=0; double scale=5; int *x, *y, *u, *v; uint *a, *b;
  127. x=malloc(sizeof(int)*(k+1)); y=malloc(sizeof(int)*(k+1));
  128. for(j=0; j<k; j++) x[j]=(int)(scale*xy[j*2+0]+.5); x[k]=x[0];
  129. for(j=0; j<k; j++) y[j]=(int)(scale*xy[j*2+1]+.5); y[k]=y[0];
  130. for(j=0; j<k; j++) m+=umax(abs(x[j]-x[j+1]),abs(y[j]-y[j+1]))+1;
  131. u=malloc(sizeof(int)*m); v=malloc(sizeof(int)*m); m=0;
  132. for( j=0; j<k; j++ ) {
  133. int xs=x[j], xe=x[j+1], ys=y[j], ye=y[j+1], dx, dy, t;
  134. bool flip; double s; dx=abs(xe-xs); dy=abs(ys-ye);
  135. flip = (dx>=dy && xs>xe) || (dx<dy && ys>ye);
  136. if(flip) { t=xs; xs=xe; xe=t; t=ys; ys=ye; ye=t; }
  137. s = dx>=dy ? (double)(ye-ys)/dx : (double)(xe-xs)/dy;
  138. if(dx>=dy) for( int d=0; d<=dx; d++ ) {
  139. t=flip?dx-d:d; u[m]=t+xs; v[m]=(int)(ys+s*t+.5); m++;
  140. } else for( int d=0; d<=dy; d++ ) {
  141. t=flip?dy-d:d; v[m]=t+ys; u[m]=(int)(xs+s*t+.5); m++;
  142. }
  143. }
  144. // get points along y-boundary and downsample
  145. free(x); free(y); k=m; m=0; double xd, yd;
  146. x=malloc(sizeof(int)*k); y=malloc(sizeof(int)*k);
  147. for( j=1; j<k; j++ ) if(u[j]!=u[j-1]) {
  148. xd=(double)(u[j]<u[j-1]?u[j]:u[j]-1); xd=(xd+.5)/scale-.5;
  149. if( floor(xd)!=xd || xd<0 || xd>w-1 ) continue;
  150. yd=(double)(v[j]<v[j-1]?v[j]:v[j-1]); yd=(yd+.5)/scale-.5;
  151. if(yd<0) yd=0; else if(yd>h) yd=h; yd=ceil(yd);
  152. x[m]=(int) xd; y[m]=(int) yd; m++;
  153. }
  154. // compute rle encoding given y-boundary points
  155. k=m; a=malloc(sizeof(uint)*(k+1));
  156. for( j=0; j<k; j++ ) a[j]=(uint)(x[j]*(int)(h)+y[j]);
  157. a[k++]=(uint)(h*w); free(u); free(v); free(x); free(y);
  158. qsort(a,k,sizeof(uint),uintCompare); uint p=0;
  159. for( j=0; j<k; j++ ) { uint t=a[j]; a[j]-=p; p=t; }
  160. b=malloc(sizeof(uint)*k); j=m=0; b[m++]=a[j++];
  161. while(j<k) if(a[j]>0) b[m++]=a[j++]; else {
  162. j++; if(j<k) b[m-1]+=a[j++]; }
  163. rleInit(R,h,w,m,b); free(a); free(b);
  164. }
  165. char* rleToString( const RLE *R ) {
  166. // Similar to LEB128 but using 6 bits/char and ascii chars 48-111.
  167. siz i, m=R->m, p=0; long x; bool more;
  168. char *s=malloc(sizeof(char)*m*6);
  169. for( i=0; i<m; i++ ) {
  170. x=(long) R->cnts[i]; if(i>2) x-=(long) R->cnts[i-2]; more=1;
  171. while( more ) {
  172. char c=x & 0x1f; x >>= 5; more=(c & 0x10) ? x!=-1 : x!=0;
  173. if(more) c |= 0x20; c+=48; s[p++]=c;
  174. }
  175. }
  176. s[p]=0; return s;
  177. }
  178. void rleFrString( RLE *R, char *s, siz h, siz w ) {
  179. siz m=0, p=0, k; long x; bool more; uint *cnts;
  180. while( s[m] ) m++; cnts=malloc(sizeof(uint)*m); m=0;
  181. while( s[p] ) {
  182. x=0; k=0; more=1;
  183. while( more ) {
  184. char c=s[p]-48; x |= (c & 0x1f) << 5*k;
  185. more = c & 0x20; p++; k++;
  186. if(!more && (c & 0x10)) x |= -1 << 5*k;
  187. }
  188. if(m>2) x+=(long) cnts[m-2]; cnts[m++]=(uint) x;
  189. }
  190. rleInit(R,h,w,m,cnts); free(cnts);
  191. }