Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <shogun/base/init.h>
- #include <shogun/lib/common.h>
- #include <shogun/lib/SGMatrix.h>
- #include <shogun/features/DenseFeatures.h>
- #include <iostream>
- #include <string>
- using namespace std;
- using namespace shogun;
- using namespace cv;
- enum Options {CV2SG_CONSTRUCTOR, CV2SG_MANUAL, CV2SG_MEMCPY,SG2CV_CONSTRUCTOR,
- SG2CV_MANUAL, SG2CV_MEMCPY};
- class SGMatrixFactory
- {
- public:
- static SGMatrix<uint8_t> getMatrix(Mat, Options=CV2SG_CONSTRUCTOR);
- };
- class CVMatrixFactory
- {
- public:
- static Mat getMatrix(SGMatrix<float64_t>, Options=SG2CV_CONSTRUCTOR);
- };
- SGMatrix<uint8_t> SGMatrixFactory::getMatrix(Mat cvMat, Options i)
- {
- int nRows=cvMat.rows;
- int nCols=cvMat.cols;
- switch (i)
- {
- case CV2SG_CONSTRUCTOR:
- {
- SGMatrix<uint8_t> sgMat(cvMat.data, nRows, nCols, false);
- return sgMat;
- }
- case CV2SG_MANUAL:
- {
- SGMatrix<uint8_t> sgMat(nRows, nCols);
- for(int i=0; i < nRows; i++)
- for(int j=0; j < nCols; j++)
- sgMat(i,j) = cvMat.at<unsigned char>(i,j);
- return sgMat;
- }
- case CV2SG_MEMCPY:
- {
- SGMatrix<uint8_t> sgMat(nRows, nCols);
- memcpy(sgMat.matrix, cvMat.data, nRows*nCols*sizeof(unsigned char));
- return sgMat;
- }
- }
- }
- Mat CVMatrixFactory::getMatrix(SGMatrix<float64_t> sgMat, Options i)
- {
- int nRows=sgMat.num_rows;
- int nCols=sgMat.num_cols;
- switch(i)
- {
- case SG2CV_CONSTRUCTOR:
- {
- Mat cvMat(nRows, nCols, CV_64FC1, (void*)sgMat.matrix);
- return cvMat;
- }
- case SG2CV_MANUAL:
- {
- Mat cvMat(nRows, nCols, CV_64FC1);
- for(int i=0; i<nRows; i++)
- for(int j=0; j<nCols;j++)
- cvMat.at<double>(i,j) = sgMat(i,j);
- return cvMat;
- }
- case SG2CV_MEMCPY:
- {
- Mat cvMat(nRows, nCols, CV_64FC1);
- memcpy((double*)cvMat.data, sgMat.matrix, nRows*nCols*sizeof(double));
- return cvMat;
- }
- }
- }
- class CDenseSGMatrixFactory
- {
- public:
- static CDenseFeatures<uint8_t>* getDenseFeatures(Mat, Options);
- };
- CDenseFeatures<uint8_t>* CDenseSGMatrixFactory::getDenseFeatures(Mat cvMat,Options option)
- {
- SGMatrix<uint8_t> sgMat = SGMatrixFactory::getMatrix(cvMat, option);
- CDenseFeatures<uint8_t>* features = new CDenseFeatures<uint8_t>(sgMat);
- return features;
- }
- int main()
- {
- init_shogun_with_defaults();
- Mat cvMatiii = Mat::eye(3,4,CV_8UC1);
- cvMatiii.at<unsigned char>(0,1) = 3;
- SGMatrix<float64_t> sgMatiii;
- sgMatiii = SGMatrix<float64_t>::create_identity_matrix(3,1);
- sgMatiii(0,2) = 3;
- //-----------------------------------------------------------------------------
- //Implementation part.
- //-----------------------------------------------------------------------------
- CDenseFeatures<uint8_t>* A = CDenseSGMatrixFactory::getDenseFeatures
- (cvMatiii, CV2SG_CONSTRUCTOR);
- /*
- SGMatrix<uint8_t> sgMat1 = SGMatrixFactory::getMatrix(cvMatiii,CV2SG_CONSTRUCTOR);
- sgMat1.display_matrix();
- SGMatrix<uint8_t> sgMat2 = SGMatrixFactory::getMatrix(cvMatiii,CV2SG_MANUAL);
- sgMat2.display_matrix();
- SGMatrix<uint8_t> sgMat3 = SGMatrixFactory::getMatrix(cvMatiii,CV2SG_MEMCPY);
- sgMat3.display_matrix();
- Mat cvMat1 = CVMatrixFactory::getMatrix(sgMatiii,SG2CV_CONSTRUCTOR);
- cout<<cvMat1<<endl;;
- Mat cvMat2 = CVMatrixFactory::getMatrix(sgMatiii,SG2CV_MANUAL);
- cout<<cvMat2<<endl;;
- Mat cvMat3 = CVMatrixFactory::getMatrix(sgMatiii,SG2CV_MEMCPY);
- cout<<cvMat3<<endl;;
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment