13608

Opencv Mat vector assignment to a row of a matrix, fastest way?

Question:

What is the fastest way of assigning a vector to a matrix row in a loop? I want to fill a <strong>data matrix</strong> along its rows with vectors. These vectors are computed in a loop. This loop last until all the entries of <strong>data matrix</strong> is filled those vectors.

Currently I am using cv::Mat::at<>() method for accessing the elements of the matrix and fill them with the vector, however, it seems this process is quite slow. I have tried another way by using cv::Mat::X.row(index) = data_vector, it works fast but fill my matrix X with some garbage values which I can not understand, why.

I read that there exists another way of using pointers (fastest way), however, I can not able to understand. Can somebody explain how to use them or other different methods?

Here is a part of my code:

#define OFFSET 2 cv::Mat im = cv::imread("001.png", CV_LOAD_IMAGE_GRAYSCALE); cv::Mat X = cv::Mat((im.rows - 2*OFFSET)*(im.cols - 2*OFFSET), 25, CV_64FC1); // Holds the training data. Data contains image patches cv::Mat patch = cv::Mat(5, 5, im.type()); // Holds a cropped image patch typedef cv::Vec<float, 25> Vec25f; int ind = 0; for (int row = 0; row < (im.rows - 2*OFFSET); row++){ for (int col = 0; col < (im.cols - 2*OFFSET); col++){ cv::Mat temp_patch = im(cv::Rect(col, row, 5, 5)); // crop an image patch (5x5) at each pixel patch = temp_patch.clone(); // Needs to do this because temp_patch is not continuous in memory patch.convertTo(patch, CV_64FC1); Vec25f data_vector = patch.reshape(0, 1); // make it row vector (1X25). for (int i = 0; i < 25; i++) { X.at<float>(ind, i) = data_vector[i]; // Currently I am using this way (quite slow). } //X_train.row(ind) = patch.reshape(0, 1); // Tried this but it assigns some garbage values to the data matrix! ind += 1; } }

Answer1:

To do it the regular opencv way you could do :-

ImageMat.row(RowIndex) = RowMat.clone();

or

RowMat.copyTo(ImageMat.row(RowIndex));

Haven't tested for correctness or speed.

Answer2:

Just a couple of edits in your code

double * xBuffer = X.ptr<double>(0); for (int row = 0; row < (im.rows - 2*OFFSET); row++){ for (int col = 0; col < (im.cols - 2*OFFSET); col++){ cv::Mat temp_patch = im(cv::Rect(col, row, 5, 5)); // crop an image patch (5x5) at each pixel patch = temp_patch.clone(); // Needs to do this because temp_patch is not continuous in memory patch.convertTo(patch, CV_64FC1); memcpy(xBuffer, patch.data, 25*sizeof(double)); xBuffer += 25; } }

Also, you dont seem to do any computation in patch just extract grey level values, so you can create X with the same type as im, and convert it to double at the end. In this way, you could memcpy each row of your patch, the address in memory beeing `unsigned char* buffer = im.ptr(row) + col

Recommend

  • eC (Ecere) how to not worry about private data fields of a class
  • including Python.h in C++ file CDT
  • Get all existing pointers to an object
  • Java Garbage collection, setting reference to null
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • Where these are stored?
  • How to display special characters in SQL server 2008?
  • SQL Query - Table Joining Problems
  • Check all values in string[] for length?
  • Zurb Foundation _global.scss meta styles for js?
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Groovy: Unexpected token “:”
  • How to define and use opencv mat of user type
  • Is there a way to do normal logging with EureakLog?
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Record samples being played with OpenAL
  • How to create a file in java without a extension
  • Asynchronous UI Testing in Xcode With Swift
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • What is the “return” in scheme?
  • What is Eclipse's Declaration View used for?
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • TFS: Get latest causes slow project reloading
  • MySQL WHERE-condition in procedure ignored
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Convert array of 8 bytes to signed long in C++
  • Large data - storage and query
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • Cannot Parse HTML Data Using Android / JSOUP
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • Understanding cpu registers
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Append folder name and increment by 1 using batch script
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • java string with new operator and a literal