Guest User

Untitled

a guest
May 19th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3.  
  4. #include <shogun/base/init.h>
  5. #include <shogun/lib/common.h>
  6. #include <shogun/lib/SGMatrix.h>
  7. #include <shogun/features/DenseFeatures.h>
  8.  
  9. #include <iostream>
  10. #include <string>
  11.  
  12. using namespace std;
  13. using namespace shogun;
  14. using namespace cv;
  15.  
  16. enum Options {CV2SG_CONSTRUCTOR, CV2SG_MANUAL, CV2SG_MEMCPY,SG2CV_CONSTRUCTOR,
  17. SG2CV_MANUAL, SG2CV_MEMCPY};
  18.  
  19. class SGMatrixFactory
  20. {
  21. public:
  22. static SGMatrix<uint8_t> getMatrix(Mat, Options=CV2SG_CONSTRUCTOR);
  23. };
  24.  
  25. class CVMatrixFactory
  26. {
  27. public:
  28. static Mat getMatrix(SGMatrix<float64_t>, Options=SG2CV_CONSTRUCTOR);
  29. };
  30.  
  31. SGMatrix<uint8_t> SGMatrixFactory::getMatrix(Mat cvMat, Options i)
  32. {
  33. int nRows=cvMat.rows;
  34. int nCols=cvMat.cols;
  35. switch (i)
  36. {
  37. case CV2SG_CONSTRUCTOR:
  38. {
  39. SGMatrix<uint8_t> sgMat(cvMat.data, nRows, nCols, false);
  40. return sgMat;
  41. }
  42.  
  43. case CV2SG_MANUAL:
  44. {
  45. SGMatrix<uint8_t> sgMat(nRows, nCols);
  46.  
  47. for(int i=0; i < nRows; i++)
  48. for(int j=0; j < nCols; j++)
  49. sgMat(i,j) = cvMat.at<unsigned char>(i,j);
  50. return sgMat;
  51. }
  52.  
  53. case CV2SG_MEMCPY:
  54. {
  55. SGMatrix<uint8_t> sgMat(nRows, nCols);
  56. memcpy(sgMat.matrix, cvMat.data, nRows*nCols*sizeof(unsigned char));
  57. return sgMat;
  58. }
  59. }
  60. }
  61.  
  62. Mat CVMatrixFactory::getMatrix(SGMatrix<float64_t> sgMat, Options i)
  63. {
  64. int nRows=sgMat.num_rows;
  65. int nCols=sgMat.num_cols;
  66. switch(i)
  67. {
  68. case SG2CV_CONSTRUCTOR:
  69. {
  70. Mat cvMat(nRows, nCols, CV_64FC1, (void*)sgMat.matrix);
  71. return cvMat;
  72. }
  73. case SG2CV_MANUAL:
  74. {
  75. Mat cvMat(nRows, nCols, CV_64FC1);
  76. for(int i=0; i<nRows; i++)
  77. for(int j=0; j<nCols;j++)
  78. cvMat.at<double>(i,j) = sgMat(i,j);
  79. return cvMat;
  80. }
  81.  
  82. case SG2CV_MEMCPY:
  83. {
  84. Mat cvMat(nRows, nCols, CV_64FC1);
  85. memcpy((double*)cvMat.data, sgMat.matrix, nRows*nCols*sizeof(double));
  86. return cvMat;
  87. }
  88. }
  89. }
  90.  
  91. class CDenseSGMatrixFactory
  92. {
  93. public:
  94. static CDenseFeatures<uint8_t>* getDenseFeatures(Mat, Options);
  95. };
  96.  
  97. CDenseFeatures<uint8_t>* CDenseSGMatrixFactory::getDenseFeatures(Mat cvMat,Options option)
  98. {
  99. SGMatrix<uint8_t> sgMat = SGMatrixFactory::getMatrix(cvMat, option);
  100. CDenseFeatures<uint8_t>* features = new CDenseFeatures<uint8_t>(sgMat);
  101. return features;
  102. }
  103.  
  104. int main()
  105. {
  106. init_shogun_with_defaults();
  107.  
  108. Mat cvMatiii = Mat::eye(3,4,CV_8UC1);
  109. cvMatiii.at<unsigned char>(0,1) = 3;
  110.  
  111. SGMatrix<float64_t> sgMatiii;
  112. sgMatiii = SGMatrix<float64_t>::create_identity_matrix(3,1);
  113. sgMatiii(0,2) = 3;
  114.  
  115. //-----------------------------------------------------------------------------
  116. //Implementation part.
  117. //-----------------------------------------------------------------------------
  118.  
  119. CDenseFeatures<uint8_t>* A = CDenseSGMatrixFactory::getDenseFeatures
  120. (cvMatiii, CV2SG_CONSTRUCTOR);
  121.  
  122. /*
  123. SGMatrix<uint8_t> sgMat1 = SGMatrixFactory::getMatrix(cvMatiii,CV2SG_CONSTRUCTOR);
  124. sgMat1.display_matrix();
  125.  
  126. SGMatrix<uint8_t> sgMat2 = SGMatrixFactory::getMatrix(cvMatiii,CV2SG_MANUAL);
  127. sgMat2.display_matrix();
  128.  
  129. SGMatrix<uint8_t> sgMat3 = SGMatrixFactory::getMatrix(cvMatiii,CV2SG_MEMCPY);
  130. sgMat3.display_matrix();
  131.  
  132. Mat cvMat1 = CVMatrixFactory::getMatrix(sgMatiii,SG2CV_CONSTRUCTOR);
  133. cout<<cvMat1<<endl;;
  134.  
  135. Mat cvMat2 = CVMatrixFactory::getMatrix(sgMatiii,SG2CV_MANUAL);
  136. cout<<cvMat2<<endl;;
  137.  
  138. Mat cvMat3 = CVMatrixFactory::getMatrix(sgMatiii,SG2CV_MEMCPY);
  139. cout<<cvMat3<<endl;;
  140. */
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment