PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cvimgutils/CVImgUtils.cpp

https://code.google.com/p/plinth-engine/
C++ | 156 lines | 111 code | 16 blank | 29 comment | 14 complexity | e1d3b7a40318a9cc09b1a69e441d030b MD5 | raw file
  1. #include "CVImgUtils.h"
  2. #include "cvimgutilslua.h"
  3. #include <cv.h>
  4. #include <cxcore.h>
  5. #include <highgui.h>
  6. RegisterCppObj<CVImgUtils> g_register_cvimgutils_object;
  7. CVImgUtils::CVImgUtils()
  8. {
  9. // nothing to do
  10. }
  11. // Return the c++ type id
  12. int CVImgUtils::getCppTypeId()
  13. {
  14. return CVIMGUTILS_OBJ_ID;
  15. }
  16. // Returns whether this can be cast to a given type
  17. bool CVImgUtils::isType(int id)
  18. {
  19. return id == CVIMGUTILS_OBJ_ID;
  20. }
  21. // Return the c++ type as a string
  22. std::string CVImgUtils::getCppTypeName()
  23. {
  24. return "CVImgUtils";
  25. }
  26. // returns a description of this thing
  27. std::string CVImgUtils::getDescription()
  28. {
  29. return "Utility object for accessing some opencv functions (e.g., save and load images)";
  30. }
  31. // create another one of these
  32. AbstractObject* CVImgUtils::newInstance()
  33. {
  34. return new CVImgUtils;
  35. }
  36. // init & update
  37. void CVImgUtils::init()
  38. {
  39. // also nothing to do here
  40. }
  41. void CVImgUtils::update(double dt)
  42. {
  43. // nope
  44. }
  45. // push its static functions into the given lua table
  46. void CVImgUtils::pushStaticFuncs(lua_State* L)
  47. {
  48. // push superclass static functions...
  49. AbstractObject::pushStaticFuncs(L);
  50. // just push these static funcs...
  51. CVImgUtilsLua::pushStaticFuncs(L);
  52. //static const struct luaL_Reg mylib [] = {
  53. // {"InitMixer", li_initMixer},
  54. // {"InitMixerDefaultSettings", li_initMixerDefaultSettings},
  55. // {"LoadWAV", li_loadWAV},
  56. // {"LoadMUS", li_loadMUS},
  57. // {"DeleteChunk", li_deleteChunk},
  58. // {"DeleteMusic", li_deleteMusic},
  59. // {NULL, NULL} /* sentinel */
  60. //};
  61. // dump them into said table
  62. //luaL_register(L, NULL, mylib);
  63. }
  64. CVImgUtils::~CVImgUtils()
  65. {
  66. // nothing special to do
  67. }
  68. void CVImgUtils::saveImage(SimpleImage* img, std::string fn)
  69. {
  70. if(img->sizeC() != 3 && img->sizeC() != 1 && img->sizeC() != 4)
  71. {
  72. LOG(logERROR) << "Cannot save image with channels != 3 (channels = " << img->sizeC();
  73. }
  74. //cv::Mat img2(cv::Size(320,240),CV_8UC3);
  75. LOG(logDEBUG) << "Saving an image which is " << img->sizeX() << "x" << img->sizeY() << "x" << img->sizeC();
  76. LOG(logDEBUG) << "Saving to filename " << fn;
  77. if(img->sizeC() == 3)
  78. {
  79. cv::Mat temp(img->sizeY(), img->sizeX(), CV_8UC3, img->getPtr());
  80. cv::Mat dimg(cv::Size(img->sizeX(), img->sizeY()), CV_8UC3);
  81. cv::Mat dimg2(cv::Size(img->sizeX(), img->sizeY()), CV_8UC3);
  82. cv::cvtColor(temp, dimg, CV_RGB2BGR);
  83. cv::flip(dimg, dimg2, 0);
  84. IplImage bla = dimg2;
  85. LOG(logDEBUG) << "Got this far... " << temp.rows << "x" << temp.cols;
  86. //cv::imwrite(fn, temp);
  87. cvSaveImage(fn.c_str(), &bla);
  88. LOG(logDEBUG) << "Done?";
  89. }
  90. if(img->sizeC() == 4)
  91. {
  92. cv::Mat temp(img->sizeY(), img->sizeX(), CV_8UC4, img->getPtr());
  93. cv::Mat dimg(cv::Size(img->sizeX(), img->sizeY()), CV_8UC3);
  94. cv::Mat dimg2(cv::Size(img->sizeX(), img->sizeY()), CV_8UC3);
  95. //cv::cvtColor(temp, dimg, CV_RGB2BGR);
  96. int fromTo[] = {0,0, 1,1, 2,2};
  97. cv::mixChannels(&temp, 1, &dimg, 1, fromTo, 3);
  98. cv::flip(dimg, dimg2, 0);
  99. IplImage bla = dimg2;
  100. LOG(logDEBUG) << "Got this far... " << temp.rows << "x" << temp.cols;
  101. //cv::imwrite(fn, temp);
  102. cvSaveImage(fn.c_str(), &bla);
  103. LOG(logDEBUG) << "Done?";
  104. }
  105. else
  106. {
  107. cv::Mat temp(img->sizeY(), img->sizeX(), CV_8UC1, img->getPtr());
  108. cv::Mat dimg(cv::Size(img->sizeX(), img->sizeY()), CV_8UC3);
  109. cv::Mat dimg2(cv::Size(img->sizeX(), img->sizeY()), CV_8UC3);
  110. cv::cvtColor(temp, dimg, CV_GRAY2RGB);
  111. cv::flip(dimg, dimg2, 0);
  112. IplImage bla = dimg2;
  113. LOG(logDEBUG) << "Got this far... " << temp.rows << "x" << temp.cols;
  114. //cv::imwrite(fn, temp);
  115. cvSaveImage(fn.c_str(), &bla);
  116. LOG(logDEBUG) << "Done?";
  117. }
  118. }
  119. SimpleImage* CVImgUtils::loadImage(std::string fn)
  120. {
  121. IplImage* tempImg = cvLoadImage(fn.c_str());
  122. cv::Mat loadIm(tempImg); // = cv::imread(fn);
  123. cv::Mat dimg(loadIm.size(), CV_8UC3);
  124. cv::Mat dimg2(loadIm.size(), CV_8UC3);
  125. cv::cvtColor(loadIm, dimg, CV_BGR2RGB);
  126. cv::flip(dimg, dimg2, 0);
  127. SimpleImage* ret = new SimpleImage();
  128. ret->allocateInterleaved(loadIm.cols, loadIm.rows, 3); // just assume 3
  129. cv::Mat_<cv::Vec3b>& tim = (cv::Mat_<cv::Vec3b>&)dimg2;
  130. for(int r = 0; r < dimg2.rows; ++r)
  131. {
  132. for(int c = 0; c < dimg2.cols; ++c)
  133. {
  134. for(int i = 0; i < 3; ++i)
  135. ret->set(c, r, i, tim(r,c)[i]);
  136. }
  137. }
  138. cvReleaseImage(&tempImg);
  139. return ret;
  140. }