Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 优化了从openCV mat到二维浮点数组的复制_Arrays_Opencv_Optimization_Mat - Fatal编程技术网

Arrays 优化了从openCV mat到二维浮点数组的复制

Arrays 优化了从openCV mat到二维浮点数组的复制,arrays,opencv,optimization,mat,Arrays,Opencv,Optimization,Mat,我想在2D浮点数组中复制opencv Mat变量。我使用了下面的代码来达到这个目的。但是因为我开发了一个代码,速度是一个非常重要的指标,所以这种复制方法还不够优化。还有其他更优化的方法吗 float *ImgSrc_f; ImgSrc_f = (float *)malloc(512 * 512 * sizeof(float)); for(int i=0;i<512;i++) for(int j=0;j<512;j++) { ImgSrc_f[i * 5

我想在2D浮点数组中复制opencv Mat变量。我使用了下面的代码来达到这个目的。但是因为我开发了一个代码,速度是一个非常重要的指标,所以这种复制方法还不够优化。还有其他更优化的方法吗

float *ImgSrc_f;
ImgSrc_f = (float *)malloc(512 * 512 * sizeof(float));
for(int i=0;i<512;i++)
    for(int j=0;j<512;j++)
    {
        ImgSrc_f[i * 512 + j]=ImgSrc.at<float>(i,j);
    }
float*ImgSrc\u f;
ImgSrc_f=(浮点*)malloc(512*512*sizeof(浮点));

对于(inti=0;i也可以使用std::vector结构吗?如果可以的话

std::vector<float> container;
container.assign((float*)matrix.startpos, (float*)matrix.endpos);
std::向量容器;
container.assign((float*)matrix.startpos,(float*)matrix.endpos);
真的

//first method
Mat img_cropped = ImgSrc(512, 512).clone();
float *ImgSrc_f = img_cropped.data;
应该比最好的方法效率低不超过两个百分点。我建议使用这种方法,只要第二种方法的损失不超过1%

还可以试试这个方法,它非常接近绝对最佳的方法,除非您可以使用某种扩展的cpu指令集(如果有扩展的cpu指令集,),您可能会看到方法2和方法1之间的最小差异

//method 2    
//preallocated memory

//largest possible with minimum number of direct copy calls
//caching pointer arithmetic can speed up things extremely minimally
//removing the Mat header with a malloc can speed up things minimally
>>startx, >>starty, >>ROI;
Mat img_roi = ImgSrc(ROI);
Mat img_copied(ROI.height, ROI.width, CV_32FC1);

for (int y = starty; y < starty+ROI.height; ++y)
{
    unsigned char* rowptr = img_roi.data + y*img_roi.step1() + startx * sizeof(float);
    unsigned char* rowptr2 = img_coped.data + y*img_copied.step1();
    memcpy(rowptr2, rowptr, ROI.width * sizeof(float));
}
//方法2
//预分配存储器
//最大可能的直接复制调用数最少
//缓存指针算法可以将速度降到最低
//用malloc移除垫头可以将速度降到最低
>>startx,>>starty,>>ROI;
Mat img_roi=ImgSrc(roi);
复制的材料img_(ROI.高度、ROI.宽度、CV_32FC1);
对于(int y=starty;y

基本上,如果您真的非常关心这些性能细节,您通常应该远离重载运算符。代码的抽象级别越高,惩罚成本越高。当然,这会使您的代码更危险、更难阅读,并且容易出现错误。

这将复制错误的数据,除非您要复制的ROI为wide作为输入的宽度。