00001 #ifndef OPENCVEXT_H
00002 #define OPENCVEXT_H
00003
00004 #include <opencv/cv.hpp>
00005 #include <iostream>
00006 #include <cmath>
00007 #include <ostream>
00008
00009 namespace seemicro
00010 {
00011 inline double sqr(const double x)
00012 {
00013 return x*x;
00014 }
00015
00020 inline uchar getPixel8u_1(const IplImage *img, const int x, const int y)
00021 {
00022 int step = img->widthStep;
00023 uchar *p = (uchar*)img->imageData + step*y;
00024 return p[x];
00025 }
00026
00031 inline void putPixel8u_1(const IplImage *img, const int x, const int y, const uchar c)
00032 {
00033 if(x >= img->width) return;
00034 if(y >= img->height) return;
00035 int step = img->widthStep;
00036 uchar *p = (uchar*)img->imageData + step*y;
00037 p[x] = c;
00038 }
00039
00044 inline int getPixel8u_3(const IplImage *img, const int x, const int y)
00045 {
00046 int step = img->widthStep;
00047 uchar *p = (uchar*)img->imageData + step*y + x*3;
00048
00049 return CV_RGB( p[2], p[1], p[0] );
00050 }
00051
00056
00057 inline void putPixel8u_3(const IplImage *img, const int x, const int y, const int color)
00058 {
00059 if(x >= img->width) return;
00060 if(y >= img->height) return;
00061 int step = img->widthStep;
00062 uchar *p = (uchar*)img->imageData + step*y + x*3;
00063
00064 p[2]=(uchar) (color>>16);
00065 p[1]=(uchar) (color>>8);
00066 p[0]=(uchar) color;
00067 }
00068
00073 inline double getPixel32f_1(const IplImage *img, const int x, const int y)
00074 {
00075 int step = img->widthStep;
00076 char *p = (img->imageData + step*y + x*4);
00077 return *(float *)p;
00078 }
00079
00084 inline void putPixel32f_1(const IplImage *img, const int x,
00085 const int y, const double color)
00086 {
00087 if(x >= img->width) return;
00088 if(y >= img->height) return;
00089 int step = img->widthStep;
00090 float *p = (float*)((uchar*)(img->imageData + step*y + x*4));
00091 *p=color;
00092 }
00093
00119 void circular_shift(IplImage *src, IplImage *dst, int dx, int dy);
00120
00127 struct mypoint
00128 {
00129 CvPoint p;
00130
00131 mypoint() { }
00132 mypoint(const CvPoint& c) { p=c; }
00133 mypoint(int x0, int y0) { p.x=x0;p.y=y0; }
00134
00138 void fromPolar(double l, double alpha)
00139 {
00140 p.x=int(l*cos(alpha));
00141 p.y=int(l*sin(alpha));
00142 }
00143
00144 friend mypoint operator+(const mypoint&, const mypoint&);
00145 friend mypoint operator-(const mypoint& p1, const mypoint& p2);
00146 operator CvPoint() { return p; }
00147 };
00148
00152 std::ostream& operator<<(std::ostream& s, const mypoint& p);
00153
00157 double abs(const mypoint& p);
00158
00163 struct myrect
00164 {
00165 CvRect r;
00166 myrect() { }
00167 myrect(const CvRect c) { r=c; }
00168 myrect(const int x0, const int y0, const int w, const int h)
00169 {
00170 r.x=x0;r.y=y0;r.width=w;r.height=h;
00171 }
00172 myrect(const mypoint& p1, const mypoint& p2)
00173 {
00174 r.x=p1.p.x;
00175 r.y=p1.p.y;
00176 r.width=p2.p.x-p1.p.x;
00177 r.height=p2.p.y-p1.p.y;
00178 }
00179 void crop(const int w, const int h)
00180 {
00181 if(r.x<0) r.x = 0;
00182 if(r.y<0) r.y = 0;
00183 if(r.x>=w) r.x = w-10;
00184 if(r.y>=h) r.y = h-10;
00185 if(r.x+r.width>=w) r.width = w-r.x;
00186 if(r.y+r.height>=h) r.height = h-r.y;
00187
00188 if(r.width <= 0) r.width = 1;
00189 if(r.height <= 0) r.height = 1;
00190 }
00191 mypoint p1()
00192 {
00193 return mypoint(r.x, r.y);
00194 }
00195 mypoint p2()
00196 {
00197 return mypoint(r.x+r.width, r.y+r.height);
00198 }
00199 operator CvRect() { return r; }
00200 };
00201
00205 std::ostream& operator<<(std::ostream& s, const myrect& r);
00206
00211 inline float normalize2PI(const float angle)
00212 {
00213 float ret = angle;
00214 int npi = int(angle/M_PI*0.5);
00215 if(npi<0 || angle<0) npi--;
00216 if(npi!=0) ret = angle - npi*2*M_PI;
00217
00218 if(ret<0) ret=0;
00219 if(ret>2*M_PI) ret=2*M_PI-1e-8;
00220 assert(ret>=0);
00221 if(ret>2*M_PI) std::cerr << "ret=" << ret << " angle=" << angle
00222 << " npi=" << npi;
00223 assert(ret <= float(2*M_PI));
00224 return ret;
00225 }
00226
00233 inline float anglediff(float a1, float a2)
00234 {
00235 a1 = normalize2PI(a1);
00236 a2 = normalize2PI(a2);
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 float diff = fabs(a1-a2);
00253 if(diff>=M_PI) return 2*M_PI-diff;
00254 return diff;
00255 }
00256
00257 inline void cvPutTextSenkrecht(IplImage *img, const char *text, CvPoint p,
00258 CvFont *font, int color, int backgroundColor=0)
00259 {
00260 CvSize size;
00261 int ymin;
00262 cvGetTextSize(text, font, &size, &ymin);
00263
00264 size.height -= ymin;
00265 IplImage *tmp = cvCreateImage(size, img->depth, img->nChannels);
00266 cvSet(tmp, cvScalar(backgroundColor));
00267 cvPutText(tmp, text, cvPoint(0,size.height+ymin-1), font, color);
00268
00269
00270 for(int x=0; x<size.width; x++)
00271 for(int y=0; y<size.height; y++)
00272 {
00273 int destX = p.x+y;
00274 if(destX<0 || destX>=img->width) continue;
00275 int destY = p.y-x;
00276 if(destY<0 || destY>=img->height) continue;
00277 if(img->nChannels==1)
00278 {
00279 if(img->depth==IPL_DEPTH_8U)
00280 {
00281 char c = getPixel8u_1(tmp, x, y);
00282 putPixel8u_1(img, destX, destY, c);
00283 }
00284 else
00285 {
00286 std::cerr << "cvPutTextSenkrecht(): image format not supported\n";
00287 abort();
00288 }
00289 }
00290 else if(img->nChannels==3)
00291 {
00292 std::cerr << "cvPutTextSenkrecht(): image format not supported\n";
00293 abort();
00294 }
00295 else
00296 {
00297 std::cerr << "cvPutTextSenkrecht(): image format not supported\n";
00298 abort();
00299 }
00300 }
00301
00302 cvReleaseImage(&tmp);
00303 }
00304
00305 }
00306
00307 #endif
00308