(OpenCV 2.4.6)将一个 Mat's roi 的标头复制到另一个 Mat's roi

(OpenCV 2.4.6) Copy header of a Mat's roi to another Mat's roi

本文关键字:roi Mat 复制 另一个 OpenCV 一个      更新时间:2023-10-16

我的问题是关于如何仅将Mat的roi的标头复制到Mat的另一个roi,以避免复制整个Mat的数据,从而节省计算时间。

例如,我有一个源Mat的roi作为

Mat src(cv::Size(4,3),CV_32FC1);
for(int i=0;i<src.rows;i++){
  for(int j=0;j<src.cols;j++){
    src.ptr<float>(i)[j] = i*src.cols+j;
  }
}
Mat src_roi = src(Rect(1,1,src.cols-2,src.rows-1));
cout << src << endl;
cout << src_roi << endl;
// = = = OUTPUT = = =
[0, 1, 2, 3;
 4, 5, 6, 7;
 8, 9, 10, 11]
[5, 6;
 9, 10]

接下来,我预计结果如下所示,键函数(func())为

Mat dst(cv::Size(src.cols*src.rows,1),CV_32FC1);
dst.setTo(-1);
Mat dst_roi = dst.colRange(2,2+src_roi.cols*src_roi.rows);
func(src_roi,dst_roi);
cout << dst << endl;
cout << dst_roi << endl;
// = = = OUTPUT = = =
[-1, -1, 5, 6, 9, 10, -1, -1, -1, -1, -1, -1]
[5, 6, 9, 10]

基本上,func()可以通过以下方式实现,只需达到我的预期输出(计算时间在发布模式下评估),

// A01 = = =
void func(const Mat &src,Mat &dst){
  Mat ma = src.clone();
  ma = ma.reshape(0,1);
  ma.copyTo(dst);
}
// = = = Computation time = = =
0.414 seconds // when src's size is changed to 15000*15000
// A02 = = =
void func(const Mat &src,Mat &dst){
  MatConstIterator_<float> it1 = src.begin<float>(), it1_end = src.end<float>();
  MatIterator_<float> dst_it = dst.begin<float>();
  for( ; it1 != it1_end; ++it1, ++dst_it ){
    *dst_it = *it1; 
  }
}
// = = = Computation time = = =
0.508 seconds // when src's size is changed to 15000*15000
// A03 = = =
void func(const Mat &src,Mat &dst){
  int count=0;
  for(int i=0;i<src.rows;i++){
    for(int j=0;j<src.cols;j++){
      ((float*)dst.data)[count++] = src.ptr<float>(i)[j];
    }
  }
}
// = = = Computation time = = =
0.269 seconds // when src's size is changed to 15000*15000

然而,当roi很大时,它们都会复制整个矩阵,从而花费大量时间。

因此,我希望有一种方法可以只复制标头或指针,以达到相同的效果,同时节省所需的计算。或者其他可以满足我期望的方式或想法

因为在func()中,您将每个elem复制两次(第一次是src.clone()和copyTo()),所以
我想到的是通过直接将elems从src复制到dst:来消除其中一个od

void func(const Mat &src,Mat &dst){
    MatConstIterator_<float> it1 = src.begin<float>(), it1_end = src.end<float>();
    MatIterator_<float> dst_it = dst.begin<float>();
    for( ; it1 != it1_end; ++it1, ++dst_it )
        *dst_it = *it1; 
}

你不必关心src和dst的大小,但它们必须有相同数量的元素。

但坦率地说,我认为你无法避免一个副本,因为src和dst有不同的大小格式。不能直接在src_roi上执行reshape(),因为它不是连续的。这就是你所期望的,因为你不能重塑一个矩阵,它是更大矩阵的一部分,在一些roi上改变它的格式形状,而不改变整个矩阵。我的意思是,如果标题的格式不相同,你不能告诉opencv只复制标题roi。