OpenCv将向量转换为矩阵C ++

时间:2016-02-02 06:34:59

标签: c++ image opencv matrix vector

我尝试使用c ++ opencv将矢量转换为(128 * 128)矩阵 这是代码

Mat image = imread("1.jpg", 0);
vector<double> vec ImgVec[i].assign(image.datastart, image.dataend);
vector<double> avgFace = calAvgFace(vec);
Mat img=avgFace;

代码的最后一行不正确但只是一个例子。

1 个答案:

答案 0 :(得分:7)

虽然之前已经提出同样的问题并且已经回答过,例如:

我认为对这一明显解决方案的明确解释仍然缺失。因此,我的答案。

使用std::vector构造函数,OpenCV提供了两种简单的方法将cv::Mat转换为Mat

您可以创建矢量数据的副本(如果修改vectorMat中的数据将保持不变),或创建Mat 视图向量中的内容(如果您修改了vectorMat将显示更改)。

请查看此代码。首先,我创建一个双Mat的随机vector,我将其复制到cv::Mat中。然后应用一些接受此向量的函数并返回一个新函数。这只是为了使代码符合您的要求。然后,您可以查看如何从std::vector获取#include <opencv2/opencv.hpp> using namespace std; using namespace cv; vector<double> foo(const vector<double>& v) { // Apply some operation to the vector, and return the result vector<double> result(v); sort(result.begin(), result.end()); return result; } int main() { // A random CV_8UC1 matrix Mat1d dsrc(128,128); randu(dsrc, Scalar(0), Scalar(1)); imshow("Start double image", dsrc); waitKey(); // Get the vector (makes a copy of the data) vector<double> vec(dsrc.begin(), dsrc.end()); // Apply some operation on the vector, and return a vector vector<double> res = foo(vec); // Get the matrix from the vector // Copy the data Mat1d copy_1 = Mat1d(dsrc.rows, dsrc.cols, res.data()).clone(); // Copy the data Mat1d copy_2 = Mat1d(res, true).reshape(1, dsrc.rows); // Not copying the data // Modifying res will also modify this matrix Mat1d no_copy_1(dsrc.rows, dsrc.cols, res.data()); // Not copying the data // Modifying res will also modify this matrix Mat1d no_copy_2 = Mat1d(res, false).reshape(1, dsrc.rows); imshow("Copy 1", copy_1); imshow("No Copy 1", no_copy_1); waitKey(); // Only no_copy_1 and no_copy_2 will be modified res[0] = 1.0; imshow("After change, Copy 1", copy_1); imshow("After change, No Copy 1", no_copy_1); waitKey(); return 0; } ,无论是否复制数据。

function logInToService(callback, errback, login, password, rememberLogin) {
            var url = "User/Login";

            var authorizationHeader = {'Authorization': "Basic " + login + ":" + password};
            httpWrapperService.post(url, {login: login, password: password}, authorizationHeader).then(
                function success(loginToken) {
                    // transform data here

                    self.currentUser.emailAddress = login;
                    self.currentUser.password = password;
                    // store token in a cookie
                    self.currentUser.token = loginToken;
                    $rootScope.currentUser = self.currentUser;


                    if (rememberLogin) {

                        localStorage.userName=login;
                        localStorage.password=password;
                    }

                    httpWrapperService.setAuthenticationToken(loginToken);
                    callback(loginToken);
                },
                function error(errorObject) {
                    errback(errorObject);
                }
            );
        }