PageRenderTime 102ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/rubiks_inspect/include/rubiks_inspect/OcvImage.h

https://github.com/trainman419/pr2_rubiks_solver
C Header | 245 lines | 191 code | 36 blank | 18 comment | 9 complexity | 52d9e3c312bd7f585d5e27f8abb8d92a MD5 | raw file
  1. /*
  2. * OcvImage.h
  3. *
  4. * Created on: 06-Apr-2009
  5. * Author: chris
  6. *
  7. * Implements a wrapper for IplImages
  8. */
  9. #ifndef OCVIMAGE_H_
  10. #define OCVIMAGE_H_
  11. #include <opencv/cv.h>
  12. #include <stdio.h>
  13. #include <opencv/highgui.h>
  14. #include <stdarg.h>
  15. template<class T> class OcvImage {
  16. private:
  17. bool allocated;
  18. public:
  19. IplImage* iplimage;
  20. OcvImage(IplImage* img=0) {iplimage=img;allocated=false;}
  21. OcvImage(int width, int height, int depth, int channels) {
  22. iplimage = cvCreateImage(cvSize(width,height),depth,channels);
  23. cvSetZero(iplimage);
  24. allocated=true;
  25. }
  26. OcvImage(OcvImage<T>& tocopy) {
  27. allocated=tocopy.allocated;
  28. iplimage=tocopy.iplimage;
  29. }
  30. ~OcvImage(){
  31. if (allocated)
  32. cvReleaseImage(&iplimage);
  33. }
  34. operator IplImage*() {
  35. return iplimage;
  36. }
  37. void operator=(IplImage* img) {iplimage=img;}
  38. inline void release() {
  39. if (iplimage!=0) {
  40. cvReleaseImage(&iplimage);
  41. }
  42. }
  43. inline void clear() {
  44. memset(iplimage->imageData,0,iplimage->imageSize);
  45. }
  46. inline T* operator[](const int rowIndx) {
  47. return ((T *)(iplimage->imageData + rowIndx*iplimage->widthStep));
  48. }
  49. inline int width() {
  50. return iplimage->width;
  51. }
  52. inline int height() {
  53. return iplimage->height;
  54. }
  55. /// Convert the image to greyscale. Only works if the image is 3 channel 8U RGB
  56. /// @param format should be the opencv conversion constant, eg CV_RGB2GRAY
  57. inline void toGreyScale(int format) {
  58. IplImage *conved = cvCreateImage(cvSize(iplimage->width,iplimage->height),IPL_DEPTH_8U,1);
  59. cvCvtColor(iplimage,conved,format);
  60. cvReleaseImage(&iplimage);
  61. iplimage=conved;
  62. }
  63. inline void resize(int x, int y) {
  64. if (x== width() && y==height())
  65. return;
  66. // std::cout << "Resize.\n";
  67. IplImage *conved = cvCreateImage(cvSize(x,y),iplimage->depth,iplimage->nChannels);
  68. cvResize(iplimage,conved);
  69. cvReleaseImage(&iplimage);
  70. iplimage=conved;
  71. }
  72. inline void newImage(int x, int y, int depth, int channels) {
  73. release();
  74. iplimage = cvCreateImage(cvSize(x,y),depth,channels);
  75. }
  76. inline void rotate(float cx, float cy, float theta) {
  77. IplImage *conved = cvCreateImage(cvSize(iplimage->width, iplimage->height),iplimage->depth, iplimage->nChannels);
  78. float m[6];
  79. CvMat M;
  80. M = cvMat(2, 3, CV_32F, m);
  81. m[0] = (float) (cos(-theta));
  82. m[1] = (float) (sin(-theta));
  83. m[3] = -m[1];
  84. m[4] = m[0];
  85. // and the translation (centre point from original image)
  86. m[2] = cx;
  87. m[5] = cy;
  88. cvGetQuadrangleSubPix(iplimage, conved, &M);
  89. cvReleaseImage(&iplimage);
  90. iplimage=conved;
  91. }
  92. inline OcvImage<T>* rotateCopy(float cx, float cy, float theta, OcvImage<T>* destination=NULL) {
  93. OcvImage<T> *conved;
  94. if (destination==NULL)
  95. conved = new OcvImage<T>(iplimage->width, iplimage->height,iplimage->depth, iplimage->nChannels);
  96. else
  97. conved=destination;
  98. float m[6];
  99. CvMat M;
  100. M = cvMat(2, 3, CV_32F, m);
  101. m[0] = (float) (cos(-theta));
  102. m[1] = (float) (sin(-theta));
  103. m[3] = -m[1];
  104. m[4] = m[0];
  105. // and the translation (centre point from original image)
  106. m[2] = cx;
  107. m[5] = cy;
  108. cvGetQuadrangleSubPix(iplimage, (*conved), &M);
  109. return conved;
  110. }
  111. inline void crop(float removeTop,float removeBottom,float removeLeft, float removeRight) {
  112. IplImage *conved = cvCreateImage(cvSize(iplimage->width-removeLeft-removeRight, iplimage->height-removeTop-removeBottom),iplimage->depth, iplimage->nChannels);
  113. float m[6];
  114. CvMat M;
  115. M = cvMat(2, 3, CV_32F, m);
  116. m[0] = 1;
  117. m[1] = 0;
  118. m[3] = 0;
  119. m[4] = 1;
  120. // and the translation (centre point from original image)
  121. m[2] = iplimage->width * 0.5f;
  122. m[5] = iplimage->height * 0.5f;
  123. cvGetQuadrangleSubPix(iplimage, conved, &M);
  124. cvReleaseImage(&iplimage);
  125. iplimage=conved;
  126. }
  127. inline void writeString(int x, int y, const char* text, CvScalar colour = CV_RGB(0,0,0), double scale=0.3, int thickness=1) {
  128. CvFont font;
  129. cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,scale,scale,0,thickness);
  130. cvPutText(iplimage,text,cvPoint(x,y),&font,colour);
  131. }
  132. inline void writeInt(int x, int y, int number, CvScalar colour = CV_RGB(0,0,0), double scale=0.3, int thickness=1) {
  133. char buff[10];
  134. sprintf(buff,"%d",number);
  135. writeString(x,y,buff,colour,scale,thickness);
  136. }
  137. inline void writeFormatted(int x, int y, const char *format, CvScalar colour, double scale, int thickness, ...) {
  138. char buff[50];
  139. va_list vargs;
  140. va_start(vargs,format); //10?va_start (args, format);
  141. vsprintf(buff,format,vargs);
  142. writeString(x,y,buff,colour,scale,thickness);
  143. }
  144. inline void loadImage(const char *imagefile) {
  145. release();
  146. iplimage = cvLoadImage(imagefile);
  147. }
  148. inline OcvImage<T> getROI(int x, int y, int width, int height) {
  149. if (x+width > iplimage->width)
  150. width=iplimage->width-x;
  151. if (y+height > iplimage->height)
  152. width=iplimage->height-y;
  153. if (x<0) x=0;
  154. if (y<0) y=0;
  155. OcvImage<T> roi(cvCreateImageHeader(cvSize(width,height),iplimage->depth,iplimage->nChannels));
  156. roi.iplimage->imageData=iplimage->imageData+y*iplimage->widthStep+x*iplimage->nChannels;
  157. roi.iplimage->widthStep=iplimage->widthStep;
  158. return roi;
  159. }
  160. };
  161. template <class T> class BGRPixel {
  162. public:
  163. T r,g,b;
  164. BGRPixel() {
  165. }
  166. BGRPixel(int red, int green, int blue) {
  167. r=red; b=blue; g=green;
  168. }
  169. inline BGRPixel& operator=(BGRPixel p) {
  170. r=p.r; g=p.g; b=p.b;
  171. return *this;
  172. }
  173. inline void operator=(int v) {
  174. r=g=b=v;
  175. }
  176. inline T intensityAvg() {
  177. return (r+g+b)/3;
  178. }
  179. operator T() {
  180. return intensityAvg;
  181. }
  182. };
  183. template <class T> class RGBPixel {
  184. public:
  185. T b,g,r;
  186. RGBPixel() {
  187. }
  188. RGBPixel(int red, int green, int blue) {
  189. r=red; b=blue; g=green;
  190. }
  191. inline RGBPixel& operator=(RGBPixel p) {
  192. r=p.r; g=p.g; b=p.b;
  193. return *this;
  194. }
  195. inline void operator=(int v) {
  196. r=g=b=v;
  197. }
  198. inline T intensityAvg() {
  199. return (r+g+b)/3;
  200. }
  201. operator T() {
  202. return intensityAvg;
  203. }
  204. };
  205. //typedef Image<RgbPixel> RgbImage;
  206. //typedef Image<RgbPixelFloat> RgbImageFloat;
  207. //typedef Image<unsigned char> BwImage;
  208. //typedef Image<float> BwImageFloat;
  209. #endif /* OCVIMAGE_H_ */