//@+leo-ver=4-thin //@+node:aleator.20050908100314:@thin cvWrapLEO.c //@@language c //@+all //@+node:aleator.20050908100314.1:Includes #include "cvWrapLEO.h" #include #include #include #include //@-node:aleator.20050908100314.1:Includes //@+node:aleator.20050908100314.2:Wrappers #define FGET(img,x,y) (((float *)((img)->imageData + (y)*(img)->widthStep))[(x)]) #define UGETC(img,color,x,y) (((uint8_t *)((img)->imageData + (y)*(img)->widthStep))[(x)*3+(color)]) size_t images; inline double eucNorm(CvPoint2D64f p) {return (p.x*p.x+p.y*p.y);} inline CvPoint2D64f toNormalizedCoords(CvSize area, CvPoint from) { CvPoint2D64f res; res.x = (from.x-area.width/2.0)/area.width; res.y = (from.y-area.height/2.0)/area.height; return res; } inline CvPoint fromNormalizedCoords(CvSize area, CvPoint2D64f from) { CvPoint res; res.x = (from.x+0.5)*area.width; res.y = (from.y+0.5)*area.height; return res; } inline CvPoint2D64f fromNormalizedCoords64f(CvSize area, CvPoint2D64f from) { CvPoint2D64f res; res.x = (from.x+0.5)*area.width; res.y = (from.y+0.5)*area.height; return res; } void incrImageC(void) { images++; } void wrapReleaseImage(IplImage *t) { // printf("%d ",images); cvReleaseImage(&t); images--; } void wrapReleaseCapture(CvCapture *t) { cvReleaseCapture(&t); } void wrapReleaseVideoWriter(CvCapture *t) { cvReleaseCapture(&t); } void wrapReleaseStructuringElement(IplConvKernel *t) { cvReleaseStructuringElement(&t); } IplImage* wrapLaplace(IplImage *src,int size) { IplImage *res; IplImage *tmp; tmp = cvCreateImage(cvGetSize(src),IPL_DEPTH_16S,1); res = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); cvLaplace(src,tmp,size); cvConvertScale(tmp,res,1,0); return res; } IplImage* wrapSobel(IplImage *src,int dx ,int dy,int size) { IplImage *res; IplImage *tmp; tmp = cvCreateImage(cvGetSize(src),IPL_DEPTH_16S,1); res = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1); cvSobel(src,tmp,dx,dy,size); cvConvertScale(tmp,res,1,0); cvReleaseImage(&tmp); return res; } IplImage* wrapCreateImage32F(const int width ,const int height ,const int channels) { CvSize s; IplImage *r; s.width = width; s.height = height; r = cvCreateImage(s,IPL_DEPTH_32F,channels); cvSetZero(r); return r; } IplImage* wrapCreateImage64F(const int width ,const int height ,const int channels) { CvSize s; IplImage *r; s.width = width; s.height = height; r = cvCreateImage(s,IPL_DEPTH_64F,channels); cvSetZero(r); return r; } IplImage* wrapCreateImage8U(const int width ,const int height ,const int channels) { CvSize s; IplImage *r; s.width = width; s.height = height; r = cvCreateImage(s,IPL_DEPTH_8U,channels); cvSetZero(r); return r; } IplImage *wrapCopyMakeBorder(IplImage* src ,const int top ,const int bottom ,const int left ,const int right ,const int borderType ,const float value) { CvSize s = cvGetSize(src); s.width += left + right; s.height += top + bottom; IplImage *r; CvPoint p; p.x = left; p.y = top; r = cvCreateImage(s, src->depth, src->nChannels); cvCopyMakeBorder(src,r,p,borderType,cvScalarAll((double)value)); return r; } IplImage* composeMultiChannel(IplImage* img0 ,IplImage* img1 ,IplImage* img2 ,IplImage* img3 ,const int channels) { CvSize s; IplImage *r; s = cvGetSize(img0); r = cvCreateImage(s,img0->depth,channels); cvSetZero(r); cvMerge(img0,img1,img2,img3,r); return r; } void wrapSubRS(const CvArr *src, double s, CvArr *dst) { cvSubRS(src,cvRealScalar(s),dst,0); } void wrapSubS(const CvArr *src, double s, CvArr *dst) { cvSubS(src,cvRealScalar(s),dst,0); } void wrapAddS(const CvArr *src, double s, CvArr *dst) { cvAddS(src,cvRealScalar(s),dst,0); } void wrapAbsDiffS(const CvArr *src, double s, CvArr *dst) { cvAbsDiffS(src,dst,cvScalarAll(s)); } double wrapAvg(const CvArr *src, const CvArr *mask) { CvScalar avg = cvAvg(src,mask); return avg.val[0]; } double wrapStdDev(const CvArr *src) { CvScalar dev; cvAvgSdv(src,0,&dev,0); return dev.val[0]; } double wrapStdDevMask(const CvArr *src,const CvArr *mask) { CvScalar dev; IplImage *mask8 = ensure8U(mask); cvAvgSdv(src,0,&dev,mask8); cvReleaseImage(&mask8); return dev.val[0]; } double wrapMeanMask(const CvArr *src,const CvArr *mask) { CvScalar mean; IplImage *mask8 = ensure8U(mask); cvAvgSdv(src,&mean,0,mask8); cvReleaseImage(&mask8); return mean.val[0]; } double wrapSum(const CvArr *src) { CvScalar sum = cvSum(src); return sum.val[0]; } void wrapMinMax(const CvArr *src,const CvArr *mask ,double *minVal, double *maxVal) { //cvMinMaxLoc(src,minVal,maxVal,NULL,NULL,NULL); int i,j; int minx,miny,maxx,maxy; double pixel; double maskP; int t; double min=100000,max=-100000; // Some problem with DBL_MIN. CvSize s = cvGetSize(src); for(i=0; i0.5 ) && (pixel < min) ? pixel : min; max = (maskP >0.5 ) && (pixel > max) ? pixel : max; } (*minVal) = min; (*maxVal) = max; } void wrapSetImageROI(IplImage *i,int x, int y, int w, int h) { CvRect r = cvRect(x,y,w,h); cvSetImageROI(i,r); } // Return image that is IPL_DEPTH_8U version of // given src IplImage* ensure8U(const IplImage *src) { CvSize size; IplImage *result; int channels = src->nChannels; int dstDepth = IPL_DEPTH_8U; size = cvGetSize(src); result = cvCreateImage(size,dstDepth,channels); switch(src->depth) { case IPL_DEPTH_32F: case IPL_DEPTH_64F: cvConvertScale(src,result,255.0,0); // Scale the values to [0,255] return result; case IPL_DEPTH_8U: cvConvertScale(src,result,1,0); return result; default: printf("Cannot convert to floating image"); abort(); } } // Return image that is IPL_DEPTH_32F version of // given src IplImage* ensure32F(const IplImage *src) { CvSize size; IplImage *result; int channels = src->nChannels; int dstDepth = IPL_DEPTH_32F; size = cvGetSize(src); result = cvCreateImage(size,dstDepth,channels); switch(src->depth) { case IPL_DEPTH_32F: case IPL_DEPTH_64F: cvConvertScale(src,result,1,0); // Scale the values to [0,255] return result; case IPL_DEPTH_8U: case IPL_DEPTH_8S: cvConvertScale(src,result,1.0/255.0,0); return result; case IPL_DEPTH_16S: cvConvertScale(src,result,1.0/65535.0,0); return result; case IPL_DEPTH_32S: cvConvertScale(src,result,1.0/4294967295.0,0); return result; default: printf("Cannot convert to floating image"); abort(); } } void wrapSet32F2D(CvArr *arr, int x, int y, double value) { cvSet2D(arr,x,y,cvRealScalar(value)); } double wrapGet32F2D(CvArr *arr, int x, int y) { CvScalar r; r = cvGet2D(arr,x,y); return r.val[0]; } double wrapGet32F2DC(CvArr *arr, int x, int y,int c) { CvScalar r; r = cvGet2D(arr,x,y); return r.val[c]; } uint8_t wrapGet8U2DC(IplImage *arr, int x, int y,int c) { return UGETC(arr,c,y,x); } void wrapDrawCircle(CvArr *img, int x, int y, int radius, float r,float g,float b, int thickness) { cvCircle(img,cvPoint(x,y),radius,CV_RGB(r,g,b),thickness,8,0); } void wrapDrawText(CvArr *img, char *text, float s, int x, int y,float r,float g,float b) { CvFont font; //? cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, s, s, 0, 2, 8); cvPutText(img, text, cvPoint(x,y), &font, CV_RGB(r,g,b)); } void wrapDrawRectangle(CvArr *img, int x1, int y1, int x2, int y2, float r, float g, float b, int thickness) { cvRectangle(img,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(r,g,b),thickness,8,0); } void wrapDrawLine(CvArr *img, int x, int y, int x1, int y1, double r, double g, double b, int thickness) { cvLine(img,cvPoint(x,y),cvPoint(x1,y1),CV_RGB(r,g,b),thickness,4,0); } void wrapFillPolygon(IplImage *img, int pc, int *xs, int *ys, float r, float g, float b) { int i=0; int pSizes[] = {pc}; CvPoint *pts = (CvPoint*)malloc(pc*sizeof(CvPoint)); for (i=0; idepth,img->nChannels); cvCopy(img, newImage,0); cvResetImageROI(img); return newImage; } IplImage* simpleMergeImages(IplImage *a, IplImage *b,int offset_x, int offset_y) { CvSize aSize = cvGetSize(a); CvSize bSize = cvGetSize(b); int startx = 0 < offset_x ? 0 : offset_x; int endx = aSize.width > bSize.width+offset_x ? aSize.width : bSize.width+offset_x ; int starty = 0 < offset_y ? 0 : offset_y; int endy = aSize.height > bSize.height+offset_y ? aSize.height : bSize.height+offset_y ; CvSize size; size.width = endx-startx; size.height = endy-starty; CvRect aPos = cvRect(offset_x<0?-offset_x:0 ,offset_y<0?-offset_y:0 ,aSize.width ,aSize.height); CvRect bPos = cvRect(offset_x<0?0:offset_x ,offset_y<0?0:offset_y ,bSize.width ,bSize.height); IplImage *resultImage = cvCreateImage(size,a->depth,a->nChannels); // Blit the images into bigger result image using cvCopy cvSetImageROI(resultImage,aPos); cvCopy(a,resultImage,NULL); cvSetImageROI(resultImage,bPos); cvCopy(b,resultImage,NULL); cvResetImageROI(resultImage); return resultImage; } void blitImg(IplImage *a, IplImage *b,int offset_x, int offset_y) { CvSize bSize = cvGetSize(b); CvRect pos = cvRect(offset_x ,offset_y ,bSize.width ,bSize.height); // Blit the images b into a using cvCopy printf("Doing a blit\n"); fflush(stdout); cvSetImageROI(a,pos); cvCopy(b,a,NULL); cvResetImageROI(a); printf("Done!\n"); fflush(stdout); } IplImage* makeEvenDown(IplImage *src) { CvSize size = cvGetSize(src); int w = size.width-(size.width % 2); int h = size.height-(size.height % 2); IplImage *result = wrapCreateImage32F(w,h,1); CvRect pos = cvRect(0 ,0 ,size.width ,size.height); // Blit the images b into a using cvCopy cvSetImageROI(src,pos); cvCopy(src,result,NULL); cvResetImageROI(result); return result; } IplImage* makeEvenUp(IplImage *src) { CvSize size = cvGetSize(src); int w = size.width+(size.width % 2); int h = size.height+(size.height % 2); int j; IplImage *result = wrapCreateImage32F(w,h,1); CvRect pos = cvRect(0 ,0 ,size.width ,size.height); // Blit the images b into a using cvCopy cvSetImageROI(result,pos); cvCopy(src,result,NULL); cvResetImageROI(result); if (size.width % 2 == 1) {for (j=0; j<=size.height; j++) { FGET(result,size.width,j) = FGET(result,size.width-1,j); } } if (size.width % 2 == 1) {for (j=0; j<=size.width; j++) { FGET(result,j,(size.height)) = FGET(result,j,(size.height-1)); } } return result; } IplImage* padUp(IplImage *src,int right, int bottom) { CvSize size = cvGetSize(src); int w = size.width + (right ? 1 : 0); int h = size.height+ (bottom ? 1 : 0); int j; IplImage *result = wrapCreateImage32F(w,h,1); CvRect pos = cvRect(0 ,0 ,size.width ,size.height); // Blit the images b into a using cvCopy cvSetImageROI(result,pos); cvCopy(src,result,NULL); cvResetImageROI(result); if (right) {for (j=0; j<=size.height; j++) { FGET(result,size.width,j) = 2*FGET(result,size.width-1,j) -FGET(result,size.width-2,j); } } if (bottom) {for (j=0; j<=size.width; j++) { FGET(result,j,(size.height)) = 2*FGET(result,j,(size.height-1)) -FGET(result,j,(size.height-2)); } } return result; } void masked_merge(IplImage *src1, IplImage *mask, IplImage *src2, IplImage *dst) { int i,j; CvSize size = cvGetSize(dst); for (i=0; i edgeW ? 1 : dx/edgeW; float y = dy > edgeW ? 1 : dy/edgeW; FGET(result,j,i) = x*y; } return result; } IplImage* rectangularDistance(int w, int h) { IplImage *result; int i,j; result = wrapCreateImage32F(w,h,1); for (i=0; i=aSize.width || i+offset_y>=aSize.height || i+offset_y < 0 || j+offset_x<0) continue; aA = FGET(aAlpha,j+offset_x,i+offset_y); bA = FGET(bAlpha,j,i); fV = aA+bA > 0 ? (FGET(b,j,i)*bA+FGET(a,j+offset_x,i+offset_y)*aA)/(aA+bA) : FGET(b,j,i) ; FGET(a,j+offset_x,i+offset_y) =fV; FGET(aAlpha,j+offset_x,i+offset_y) =aA+bA; } } void plainBlit(IplImage *a, IplImage *b, int offset_y, int offset_x) { // TODO: Add checks for image type and size int i,j; CvSize aSize = cvGetSize(a); CvSize bSize = cvGetSize(b); for (i=0; i=aSize.width || i+offset_y<0 || i+offset_y>=aSize.height ) continue; if (a->nChannels == 1) {FGET(a,j+offset_x,i+offset_y) =FGET(b,j,i);} else if (a->nChannels ==3) { int dx = j+offset_x; int dy = i+offset_y; ((float *)(a->imageData + dy*a->widthStep))[dx*a->nChannels + 0] = ((float *)(b->imageData + i*b->widthStep))[j*b->nChannels + 0] ; // B ((float *)(a->imageData + dy*a->widthStep))[dx*a->nChannels + 1] = ((float *)(b->imageData + i*b->widthStep))[j*b->nChannels + 1] ; // G ((float *)(a->imageData + dy*a->widthStep))[dx*a->nChannels + 2] = ((float *)(b->imageData + i*b->widthStep))[j*b->nChannels + 2] ; // R } else {printf("Can't blit this - pic weird number of channels\n"); abort();} }} } void subpixel_blit(IplImage *a, IplImage *b, double offset_y, double offset_x) { // TODO: Add checks for image type and size int i,j; CvSize aSize = cvGetSize(a); CvSize bSize = cvGetSize(b); for (i=0; i= bSize.width || y_at_b <0 || y_at_b >= bSize.height) continue; FGET(a,j,i) =bilinearInterp(b,x_at_b,y_at_b); // TODO: Check boundaries! #SAFETY } } // Histograms. void wrapReleaseHist(CvHistogram *hist) { cvReleaseHist(&hist); } CvHistogram* calculateHistogram(IplImage *img,int bins) { float st_range[] = {-1,1}; float *ranges[] = {st_range}; int hist_size[] = {bins}; CvHistogram *result = cvCreateHist(1,hist_size,CV_HIST_ARRAY,ranges,1); cvCalcHist(&img,result,0,0); return result; } void get_histogram(IplImage *img,IplImage *mask ,float a, float b,int isCumulative ,int binCount ,double *values) { int i=0; float st_range[] = {a,b}; float *ranges[] = {st_range}; int hist_size[] = {binCount}; CvHistogram *result = cvCreateHist(1,hist_size,CV_HIST_ARRAY ,ranges,1); cvCalcHist(&img,result,isCumulative,mask); for (i=0;itotal,*maxLines); i++ ) { CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i); xs[i] = line[0].x; xs1[i] = line[1].x; ys[i] = line[0].y; ys1[i] = line[1].y; } *maxLines = MIN(lines->total,*maxLines); cvReleaseImage(&tmp); cvReleaseMemStorage(&storage); } //@-node:aleator.20050908100314.2:Wrappers //@+node:aleator.20050908100314.3:Utilities /* These are utilities that operate on opencv primitives but are not really wrappers.. Due to the fact that I seem to be incapable to link multiple objects including openCV headers this seems to be the next best solution. Watch out for name collisions! */ //@+node:aleator.20070906153003:Trigonometric operations void calculateAtan(IplImage *src, IplImage *dst) { CvSize imageSize = cvGetSize(dst); double r=0; int i; int j; for(i=0; iy) ? x:y;} inline int imin(int x, int y) {return (x 0) ratio = fabs(desArea/area); // else ratio=1; //printf("Ratio(%d,%d) is %lf\n",rx1,ry1,ratio); s = blurGet2D(intImg,rx1,ry1) -blurGet2D(intImg,rx1,ry2) -blurGet2D(intImg,rx2,ry1) +blurGet2D(intImg,rx2,ry2); cvSet2D(target,j,i,cvScalarAll(s/area)); } } double haar_at(IplImage *intImg, int x1, int y1, int w, int h) { int i,j; double s = 0; s = blurGet2D(intImg,x1,y1) -blurGet2D(intImg,x1,y1+h) -blurGet2D(intImg,x1+w,y1) +blurGet2D(intImg,x1+w,y1+h); return s; } //@nonl //@-node:aleator.20070827150608:Haar Filters //@+node:aleator.20070130144337:Statistics along a line #define SWAP(a,b) { \ int c = (a); \ (a) = (b); \ (b) = c; \ } double average_of_line(int x0, int y0 ,int x1, int y1 ,IplImage *src) { int steep = abs(y1 - y0) > abs(x1 - x0); int deltax=0; int deltay=0; int error=0; int ystep=0; int x=0; int y=0; float sum=0; int len=0; if (steep) { SWAP(x0, y0); SWAP(x1, y1); } if (x0 > x1) { SWAP(x0, x1); SWAP(y0, y1); } deltax = x1 - x0; deltay = abs(y1 - y0); error = 0; y = y0; if (y0 < y1) {ystep = 1;} else {ystep = -1;} for (x=x0; x= deltax) { y = y + ystep; error = error - deltax; } } return (sum/len); } //@-node:aleator.20070130144337:Statistics along a line //@+node:aleator.20051130130836:Taking square roots of images void sqrtImage(IplImage *src,IplImage *dst) { int i;int j; double result; CvSize size = cvGetSize(src); for(i=0;iy) return y; else return x;} int SMABx(double x, CvHistogram *h,int binCount,double t) { int binnedX; double leftSM=0; double rightSM=0; int i=0; binnedX = round(min(1,max(x,0))*(binCount-1)); // Calculate left second moment: for(i=0; i 0.8) && (testij > 0.8))) {rij=0;} result += rij; } return result; } //@-node:aleator.20070511142414.1:Fitness //@+node:aleator.20070511145251:Updating distributions // This function is used to update distribution. // Notice that alpha_t must be calculated separately // and normalization is not applied. IplImage* adaUpdateDistrImage(IplImage *target ,IplImage *weigths ,IplImage *test ,double at) { CvSize size = cvGetSize(target); int i,j; int width = size.width; int height = size.height; double tij=0,wij=0,testij=0,rij=0; IplImage *result = wrapCreateImage32F(width,height,1); for (i=0; i0.2) && (tij<0.8) ) continue; if (((tij < 0.2) && (testij < 0.2)) || ((tij > 0.8) && (testij > 0.8))) {rij = wij*exp(-at); cvSetReal2D(result,j,i,rij); } else {rij = wij*exp(at); cvSetReal2D(result,j,i,rij); } } return result; } //@-node:aleator.20070511145251:Updating distributions //@-node:aleator.20070511142414:Adaboost Learning //@+node:aleator.20051207074905:LBP void get_weighted_histogram(IplImage *src, IplImage *weights, double start, double end, int bins, double *histo) { int i,j,index; double value,weight; CvSize imageSize = cvGetSize(src); for(i=0;i=bins) continue; histo[index] += weight; } } // Calculate local binary pattern for image. // LBP is outgoing array // of (preallocated) 256 bytes that are assumed to be 0. void localBinaryPattern(IplImage *src, int *LBP) { int i,j; int pattern = 0; double center = 0; CvSize imageSize = cvGetSize(src); for(i=1; i center) *1; pattern += (blurGet2D(src,i,j-1) > center) *2; pattern += (blurGet2D(src,i+1,j-1) > center) *4; pattern += (blurGet2D(src,i-1,j) > center) *8; pattern += (blurGet2D(src,i+1,j) > center) *16; pattern += (blurGet2D(src,i-1,j+1) > center) *32; pattern += (blurGet2D(src,i,j+1) > center) *64; pattern += (blurGet2D(src,i+1,j+1) > center) *128; LBP[pattern]++; pattern = 0; } } void localBinaryPattern3(IplImage *src, int *LBP) { int i,j; int pattern = 0; double center = 0; CvSize imageSize = cvGetSize(src); for(i=1; i center) *1; pattern += (blurGet2D(src,i,j-3) > center) *2; pattern += (blurGet2D(src,i+2,j-2) > center) *4; pattern += (blurGet2D(src,i-3,j) > center) *8; pattern += (blurGet2D(src,i+3,j) > center) *16; pattern += (blurGet2D(src,i-2,j+2) > center) *32; pattern += (blurGet2D(src,i,j+3) > center) *64; pattern += (blurGet2D(src,i+2,j+2) > center) *128; LBP[pattern]++; pattern = 0; } } void localBinaryPattern5(IplImage *src, int *LBP) { int i,j; int pattern = 0; double center = 0; CvSize imageSize = cvGetSize(src); for(i=1; i center) *1; pattern += (blurGet2D(src,i,j-5) > center) *2; pattern += (blurGet2D(src,i+4,j-4) > center) *4; pattern += (blurGet2D(src,i-5,j) > center) *8; pattern += (blurGet2D(src,i+5,j) > center) *16; pattern += (blurGet2D(src,i-4,j+4) > center) *32; pattern += (blurGet2D(src,i,j+5) > center) *64; pattern += (blurGet2D(src,i+4,j+4) > center) *128; LBP[pattern]++; pattern = 0; } } void weighted_localBinaryPattern(IplImage *src,int offsetX,int offsetXY , IplImage* weights, double *LBP) { int i,j; int pattern = 0; double center = 0; double weight = 0; CvSize imageSize = cvGetSize(src); for(i=1; i center) *1; pattern += (blurGet2D(src,i,j-offsetX) > center) *2; pattern += (blurGet2D(src,i+offsetXY,j-offsetXY) > center) *4; pattern += (blurGet2D(src,i-offsetX,j) > center) *8; pattern += (blurGet2D(src,i+offsetX,j) > center) *16; pattern += (blurGet2D(src,i-offsetXY,j+offsetXY) > center) *32; pattern += (blurGet2D(src,i,j+offsetX) > center) *64; pattern += (blurGet2D(src,i+offsetXY,j+offsetXY) > center) *128; LBP[pattern] += weight; pattern = 0; } } void localHorizontalBinaryPattern(IplImage *src, int *LBP) { int i,j; int pattern = 0; double center = 0; CvSize imageSize = cvGetSize(src); for(i=0; i center) *1; pattern += (blurGet2D(src,i-3,j) > center) *2; pattern += (blurGet2D(src,i-2,j) > center) *4; pattern += (blurGet2D(src,i-1,j) > center) *8; pattern += (blurGet2D(src,i+1,j) > center) *16; pattern += (blurGet2D(src,i+2,j) > center) *32; pattern += (blurGet2D(src,i+3,j) > center) *64; pattern += (blurGet2D(src,i+4,j) > center) *128; LBP[pattern]++; pattern = 0; } } void localVerticalBinaryPattern(IplImage *src, int *LBP) { int i,j; int pattern = 0; double center = 0; CvSize imageSize = cvGetSize(src); for(i=0; i center) *1; pattern += (blurGet2D(src,i,j-3) > center) *2; pattern += (blurGet2D(src,i,j-2) > center) *4; pattern += (blurGet2D(src,i,j-1) > center) *8; pattern += (blurGet2D(src,i,j+1) > center) *16; pattern += (blurGet2D(src,i,j+2) > center) *32; pattern += (blurGet2D(src,i,j+3) > center) *64; pattern += (blurGet2D(src,i,j+4) > center) *128; LBP[pattern]++; pattern = 0; } } //@-node:aleator.20051207074905:LBP //@+node:aleator.20051109102750:Selective Average // Assuming grayscale image calculate local selective average of point x y double calcSelectiveAvg(IplImage *img,double t ,int x, int y ,int wwidth, int wheight) { int i,j; double accum=0; double count=0; double centerValue; double processed=0; CvSize size = cvGetSize(img); centerValue = blurGet2D(img,x,y); for (i=-wwidth; i=size.width || y+j<0 || y+j>=size.height) continue; processed = blurGet2D(img,x+i,y+j); if (fabs(processed-centerValue) %d is %f\n",j,i,(i+j*h),d[i+j*h]); FGET(img,j,i) = d[j*h+i]; } } return img; } IplImage *acquireImageSlowF(int w, int h, float *d) { IplImage *img; int i,j; img = cvCreateImage(cvSize(w,h), IPL_DEPTH_32F,1); for (i=0; i %d is %f\n",j,i,(i+j*h),d[i+j*h]); FGET(img,j,i) = d[j*h+i]; } } return img; } #define BLUE = 0 #define GREEN = 1 #define RED = 2 IplImage *acquireImageSlow8U(int w, int h, uint8_t *d) { IplImage *img; int i,j; img = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U,1); for (i=0; istorage)); free(f); } int reset_contour(FoundContours *f) { f->contour = f->start; } int cur_contour_size(FoundContours *f) { return f->contour->total; } double contour_area(FoundContours *f) { return cvContourArea(f->contour,CV_WHOLE_SEQ,0); } CvMoments* contour_moments(FoundContours *f) { CvMoments* moments = (CvMoments*) malloc(sizeof(CvMoments)); cvMoments(f->contour,moments,0); return moments; } double contour_perimeter(FoundContours *f) { return cvContourPerimeter(f->contour); } int more_contours(FoundContours *f) { if (f->contour != 0) {return 1;} {return 0;} // no more contours } int next_contour(FoundContours *f) { if (f->contour != 0) {f->contour = f->contour->h_next; return 1;} {return 0;} // no more contours } void contour_points(FoundContours *f, int *xs, int *ys) { if (f->contour==0) {printf("unavailable contour\n"); exit(1);} CvPoint *pt=0; int total,i=0; total = f->contour->total; for (i=0; icontour,i); if (pt==0) {printf("point out of contour\n"); exit(1);} xs[i] = pt->x; ys[i] = pt->y; } } void print_contour(FoundContours *fc) { int i=0; CvPoint *pt=0; for (i=0; icontour->total;++i) { pt = (CvPoint*)cvGetSeqElem(fc->contour,i); printf("PT=%d,%d\n",pt->x,pt->y); } } /* void draw_contour(FoundContours *fc,double color , IplImage *img, IplImage *dst) { cvDrawContours( dst, fc->start, color, color, -1, 0, 8 , cvPoint(0,0)); } */ FoundContours* get_contours(IplImage *src1) { CvSize size; IplImage *src = ensure8U(src1); //int dstDepth = IPL_DEPTH_8U; //size = cvGetSize(src1); //src = cvCreateImage(size,dstDepth,1); //cvCopy(src1,src,NULL); CvPoint* pt=0; int i=0; CvMemStorage *storage=0; CvSeq *contour=0; FoundContours* result = (FoundContours*)malloc(sizeof(FoundContours)); storage = cvCreateMemStorage(0); cvFindContours( src,storage , &contour , sizeof(CvContour) ,CV_RETR_EXTERNAL //,CV_RETR_CCOMP ,CV_CHAIN_APPROX_NONE ,cvPoint(0,0) ); // result->contour = cvApproxPoly( result->contour, sizeof(CvContour) // , result->storage, CV_POLY_APPROX_DP // , 3, 1 ); result->start = contour; result->contour = contour; result->storage = storage; cvReleaseImage(&src); return result; } //@-node:aleator.20071016114634:Contours //@+node:aleator.20070814123008:moments CvMoments* getMoments(IplImage *src, int isBinary) { CvMoments* moments = (CvMoments*) malloc(sizeof(CvMoments)); cvMoments( src, moments, isBinary); return moments; } void freeCvMoments(CvMoments *x) { free(x); } void getHuMoments(CvMoments *src,double *hu) { CvHuMoments* hu_moments = (CvHuMoments*) malloc(sizeof(CvHuMoments)); cvGetHuMoments( src, hu_moments); *hu = hu_moments->hu1; ++hu; *hu = hu_moments->hu2; ++hu; *hu = hu_moments->hu3; ++hu; *hu = hu_moments->hu4; ++hu; *hu = hu_moments->hu5; ++hu; *hu = hu_moments->hu6; ++hu; *hu = hu_moments->hu7; return; } void freeCvHuMoments(CvHuMoments *x) { free(x); } //@-node:aleator.20070814123008:moments //@+node:aleator.20060727102514:blobCount int blobCount(IplImage *src) { int contourCount=0; CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contour = 0; contourCount = cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); cvReleaseMemStorage(&storage); return contourCount; } //@-node:aleator.20060727102514:blobCount //@+node:aleator.20060413093124.1:sizeFilter IplImage* sizeFilter(IplImage *src, double minSize, double maxSize) { IplImage* dst = cvCreateImage( cvGetSize(src), IPL_DEPTH_32F, 1 ); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contour = 0; cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); cvZero( dst ); for( ; contour != 0; contour = contour->h_next ) { double area=fabs(cvContourArea(contour,CV_WHOLE_SEQ,0)); if (area <=minSize || area >= maxSize) continue; CvScalar color = cvScalar(1,1,1,1); cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8, cvPoint(0,0)); } cvReleaseMemStorage(&storage); return dst; } //@-node:aleator.20060413093124.1:sizeFilter //@-node:aleator.20060413093124:Connected components //@+node:aleator.20050908101148.1:function for rotating image IplImage* rotateImage(IplImage* src,double scale,double angle) { IplImage* dst = cvCloneImage( src ); angle = angle * (180 / CV_PI); int w = src->width; int h = src->height; CvMat *M; M = cvCreateMat(2,3,CV_32FC1); CvPoint2D32f center = cvPoint2D32f(w/2.0,h/2.0); CvMat *N = cv2DRotationMatrix(center,angle,scale,M); cvWarpAffine( src, dst, N, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS , cvScalarAll(0)); return dst; cvReleaseMat(&M); } inline double cubicInterpolate( double y0,double y1, double y2,double y3, double mu) { double a0,a1,a2,a3,mu2; mu2 = mu*mu; a0 = y3 - y2 - y0 + y1; a1 = y0 - y1 - a0; a2 = y2 - y0; a3 = y1; return(a0*mu*mu2+a1*mu2+a2*mu+a3); } double bilinearInterp(IplImage *tex, double u, double v) { CvSize s = cvGetSize(tex); int x = floor(u); int y = floor(v); double u_ratio = u - x; double v_ratio = v - y; double u_opposite = 1 - u_ratio; double v_opposite = 1 - v_ratio; double result = ((x+1 >= s.width) || (y+1 >= s.height)) ? FGET(tex,x,y) : (FGET(tex,x,y) * u_opposite + FGET(tex,x+1,y) * u_ratio) * v_opposite + (FGET(tex,x,y+1) * u_opposite + FGET(tex,x+1,y+1) * u_ratio) * v_ratio; return result; } // TODO: Check boundaries! #SAFETY double bicubicInterp(IplImage *tex, double u, double v) { CvSize s = cvGetSize(tex); int x = floor(u); int y = floor(v); double u_ratio = u - x; double v_ratio = v - y; double p[4][4] = {FGET(tex,x-1,y-1), FGET(tex,x,y-1), FGET(tex,x+1,y-1), FGET(tex,x+2,y-1), FGET(tex,x-1,y), FGET(tex,x,y), FGET(tex,x+1,y), FGET(tex,x+2,y), FGET(tex,x-1,y+1), FGET(tex,x,y+1), FGET(tex,x+1,y+1), FGET(tex,x+2,y+1), FGET(tex,x-1,y+2), FGET(tex,x,y+2), FGET(tex,x+1,y+2), FGET(tex,x+2,y+2) }; double a00 = p[1][1]; double a01 = -p[1][0] + p[1][2]; double a02 = 2*p[1][0] - 2*p[1][1] + p[1][2] - p[1][3]; double a03 = -p[1][0] + p[1][1] - p[1][2] + p[1][3]; double a10 = -p[0][1] + p[2][1]; double a11 = p[0][0] - p[0][2] - p[2][0] + p[2][2]; double a12 = -2*p[0][0] + 2*p[0][1] - p[0][2] + p[0][3] + 2*p[2][0] - 2*p[2][1] + p[2][2] - p[2][3]; double a13 = p[0][0] - p[0][1] + p[0][2] - p[0][3] - p[2][0] + p[2][1] - p[2][2] + p[2][3]; double a20 = 2*p[0][1] - 2*p[1][1] + p[2][1] - p[3][1]; double a21 = -2*p[0][0] + 2*p[0][2] + 2*p[1][0] - 2*p[1][2] - p[2][0] + p[2][2] + p[3][0] - p[3][2]; double a22 = 4*p[0][0] - 4*p[0][1] + 2*p[0][2] - 2*p[0][3] - 4*p[1][0] + 4*p[1][1] - 2*p[1][2] + 2*p[1][3] + 2*p[2][0] - 2*p[2][1] + p[2][2] - p[2][3] - 2*p[3][0] + 2*p[3][1] - p[3][2] + p[3][3]; double a23 = -2*p[0][0] + 2*p[0][1] - 2*p[0][2] + 2*p[0][3] + 2*p[1][0] - 2*p[1][1] + 2*p[1][2] - 2*p[1][3] - p[2][0] + p[2][1] - p[2][2] + p[2][3] + p[3][0] - p[3][1] + p[3][2] - p[3][3]; double a30 = -p[0][1] + p[1][1] - p[2][1] + p[3][1]; double a31 = p[0][0] - p[0][2] - p[1][0] + p[1][2] + p[2][0] - p[2][2] - p[3][0] + p[3][2]; double a32 = -2*p[0][0] + 2*p[0][1] - p[0][2] + p[0][3] + 2*p[1][0] - 2*p[1][1] + p[1][2] - p[1][3] - 2*p[2][0] + 2*p[2][1] - p[2][2] + p[2][3] + 2*p[3][0] - 2*p[3][1] + p[3][2] - p[3][3]; double a33 = p[0][0] - p[0][1] + p[0][2] - p[0][3] - p[1][0] + p[1][1] - p[1][2] + p[1][3] + p[2][0] - p[2][1] + p[2][2] - p[2][3] - p[3][0] + p[3][1] - p[3][2] + p[3][3]; double x2 = u_ratio * u_ratio; double x3 = x2 * u_ratio; double y2 = v_ratio * v_ratio; double y3 = y2 * v_ratio; return a00 + a01 * v_ratio + a02 * y2 + a03 * y3 + a10 * u_ratio + a11 * u_ratio * v_ratio + a12 * u_ratio * y2 + a13 * u_ratio * y3 + a20 * x2 + a21 * x2 * v_ratio + a22 * x2 * y2 + a23 * x2 * y3 + a30 * x3 + a31 * x3 * v_ratio + a32 * x3 * y2 + a33 * x3 * y3; } void radialRemap(IplImage *source, IplImage *dest, double k) { int i,j; CvSize s = cvGetSize(dest); double x,y,cx,cy,nx,ny,r2; cx = s.width/2.0; cy = s.height/2.0; for (i=0; i=s.width || y<0 || y>=s.height) { FGET(dest,j,i) = 0; continue;} FGET(dest,j,i) = bilinearInterp(source,x,y); } } //@-node:aleator.20050908101148.1:function for rotating image //@+node:aleator.20051220091717:Matrix multiplication void wrapMatMul(int w, int h, double *mat , double *vec, double *t) { CvMat matrix; CvMat vector; CvMat target; cvInitMatHeader(&matrix,w,h,CV_64FC1,mat,CV_AUTOSTEP); cvInitMatHeader(&vector,h,1,CV_64FC1,vec,CV_AUTOSTEP); cvInitMatHeader(&target,w,1,CV_64FC1,t,CV_AUTOSTEP); cvMatMul(&matrix,&vector,&target); } void maximal_covering_circle(int ox,int oy, double or, IplImage *distmap ,int *max_x, int *max_y, double *max_r) { double distance,radius; *max_x = ox; *max_y = oy; *max_r = or; CvSize s = cvGetSize(distmap); for(int i=0; i *max_r && radius >= or+distance ) { *max_x=i; *max_y=j; *max_r=radius;} } } double juliaF(double a, double b,double x, double y) { int limit = 1000; double complex z; int i=0; double complex c; double cr,ci; c = a + b*I; z = x+y*I; for (i=0;i4) return (i*1.0)/limit; z=z*z+c; } return 0; } CvVideoWriter* wrapCreateVideoWriter(char *fn, int fourcc, double fps,int w, int h, int color) { CvVideoWriter *res = cvCreateVideoWriter(fn,CV_FOURCC('M','P','G','4'),fps,cvSize(w,h), color); return res; } int wrapFindChessBoardCorners(const void* image, int pw, int ph, CvPoint2D32f* corners, int* cornerCount, int flags) { CvSize s = {pw,ph}; //for (int i=0; icenter = box.center; out->size = box.size; out->angle = box.angle; } int wrapCamShift(const CvArr* prob_image, CvRect *window, CvTermCriteria *criteria, CvConnectedComp* comp, CvBox2D* box) { return cvCamShift(prob_image, *window, *criteria, comp, box); } void extractCVSeq(const CvSeq* seq,void *dest){ CvSeqReader reader; cvStartReadSeq( seq, &reader, 0 ); void *index=dest; for( int i = 0; i < seq->total; i++ ) { memcpy(index,(void*)reader.ptr,seq->elem_size); index += seq->elem_size; CV_NEXT_SEQ_ELEM( seq->elem_size, reader ); }} // //@-node:aleator.20051220091717:Matrix multiplication //@-all //@-node:aleator.20050908100314:@thin cvWrapLEO.c //@-leo