c++ - Two-channel cv::Mat object filled with row-col indexes without for cycle -


i want create two-channel matrix in opencv, values corresponding pair of row , column indexes. can in following way:

for (int = 0 ; i< img_height ; ++ i){     (int j = 0 ; j < img_width ; ++ j){         src.ptr<point2f>(i)[j] = point2f(j,i);     } } 

i wonder if there way in opencv initialize such matrix in faster , more compact way, without using element-wise approach. searched on documentation, didn't find me purpose.

i ask because need application faster, i'm looking possible improvements can apply on code.

thanks in advance

there's no builtin function to this. can mimic matlab function meshgrid using repeat, in case going slower.

you can improve few things:

  • get pointer beginning of line out of inner loop, since same each line.
  • avoid create temporary object store values.
  • i think swapped , j.

have @ code:

mat2f src(img_height, img_width);  (int = 0; < img_height; ++i) {     vec2f* ptr = src.ptr<vec2f>(i);     (int j = 0; j < img_width; ++j) {         ptr[j][0] = i;         ptr[j][1] = j;     } } 

this snippet little faster (time in ms):

@marcoferro:    1.22755 @miki:          0.818491 

testing code:

#include <opencv2/opencv.hpp> using namespace cv; using namespace std;  int main() {     int img_height = 480;     int img_width = 640;      {         mat2f src(img_height, img_width);          double tic = double(gettickcount());         (int = 0; i< img_height; ++i){             (int j = 0; j < img_width; ++j){                 src.ptr<point2f>(i)[j] = point2f(i, j);             }         }          double toc = (double(gettickcount()) - tic) * 1000.0 / gettickfrequency();         cout << "@marcoferro: \t" << toc << endl;     }     {         mat2f src(img_height, img_width);          double tic = double(gettickcount());         (int = 0; < img_height; ++i) {             vec2f* ptr = src.ptr<vec2f>(i);             (int j = 0; j < img_width; ++j) {                 ptr[j][0] = i;                 ptr[j][1] = j;             }         }          double toc = (double(gettickcount()) - tic) * 1000.0 / gettickfrequency();         cout << "@miki: \t\t" << toc << endl;     }      getchar();     return 0; } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -