C++ 使用带浮点的openCV重新映射*

C++ 使用带浮点的openCV重新映射*,c++,opencv,rgb,motion,remap,C++,Opencv,Rgb,Motion,Remap,我目前正在做一个涉及运动补偿的项目 此时,我在cv::Mat input中有了输入图像 cv::resize( input, input_resized, cvSize(64,48), 12, 12, CV_INTER_AREA); input_resized.convertTo(input_float, CV_32F); //this might be redundant while cv::rezise dst should be 32F. 现在,运动矢量存储在2通道cv::Mat mot

我目前正在做一个涉及运动补偿的项目

此时,我在cv::Mat input中有了输入图像

cv::resize( input, input_resized, cvSize(64,48), 12, 12, CV_INTER_AREA);
input_resized.convertTo(input_float, CV_32F); //this might be redundant while cv::rezise dst should be 32F.
现在,运动矢量存储在2通道cv::Mat motion中,存储方式如下:

// w -image.width , h-image.height
    X0  Y0  X1   Y1   X2   Y2   ...  Xw-1  Yw-1      
    Xw  Yw  Xw+1 Yw+1 Xw+2 Yw+2 ...  X2w-1 Y2w-1
    ...
    X(h-1)w Y(h-1)w   ............. X(h-1)(w-1)Y(h-1)(w-1)
1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
Function1 ( (float*) input_float.data , (float*) motion.data, (unsigned char*) out.data );
   Function2 ( float* input, float* R, float* G, float* B);
   Function3 ( float* R, float* G, float* B, float* W, float* Z);
for(int i=0; i< rows; i++){
        for(int j=0; j<col; j++)
            { 
             map_x.at<float>(i,j) = j + motion_vect.ptr<float>(i)[2*j+0];
             map_y.at<float>(i,j) = i + motion_vect.ptr<float>(i)[2*j+1];;
        }}
因此,如果我使用:

std::vector<cv::Mat> motionvect(2);
cv::split(motion, motionvect);
那么Y应该是:

1  6  11  
2  7  12  
3  8  13
4  9  14
5  10 15
对吗?某种转置矩阵。如果这是真的,是否有任何功能会将motiovect[2]更改为这样,或者我应该为每个单独的组件执行此操作

另外,对于最后一步,我需要在流程中的不同时间应用重新映射。大概是这样的:

// w -image.width , h-image.height
    X0  Y0  X1   Y1   X2   Y2   ...  Xw-1  Yw-1      
    Xw  Yw  Xw+1 Yw+1 Xw+2 Yw+2 ...  X2w-1 Y2w-1
    ...
    X(h-1)w Y(h-1)w   ............. X(h-1)(w-1)Y(h-1)(w-1)
1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
Function1 ( (float*) input_float.data , (float*) motion.data, (unsigned char*) out.data );
   Function2 ( float* input, float* R, float* G, float* B);
   Function3 ( float* R, float* G, float* B, float* W, float* Z);
for(int i=0; i< rows; i++){
        for(int j=0; j<col; j++)
            { 
             map_x.at<float>(i,j) = j + motion_vect.ptr<float>(i)[2*j+0];
             map_y.at<float>(i,j) = i + motion_vect.ptr<float>(i)[2*j+1];;
        }}
其中,R、G、B、W、Z为浮点*类型,且分配有

 R = (float*) malloc (frame_size * sizeof(float) );
我需要对float*A和float*B应用运动补偿,但这些是不允许重新映射的输入。它们必须是InputArray,我找不到任何方法将float*转换为任何可接受的InputArray类型

我知道我的思想不是很有条理,但我希望你至少能理解我的一个问题。 1.如果运动矢量存储在cv::Mat(如x0 y0 x1 y1.)中,如何应用重映射? 2.我可以将浮点*转换为cv::remap的可接受输入吗?

对于#2,您可以这样做以将浮点值从指针获取到某个矩阵:

cv::Mat testMat = cv::Mat::zeros(2, 3, CV_32F);
std::cout << "(1, 2) elem = " << testMat.at<float>(1, 2) << std::endl;

float* pTestVal = new float;
*pTestVal = 3.14F;
testMat.at<float>(1, 2) = *pTestVal;
delete pTestVal;

std::cout << "(1, 2) elem = " << testMat.at<float>(1, 2) << std::endl;
cv::Mat testMat=cv::Mat::zero(2,3,cv_32F);
我发现本教程对于理解如何设置map1、map2参数非常有用。最后是这样的:

// w -image.width , h-image.height
    X0  Y0  X1   Y1   X2   Y2   ...  Xw-1  Yw-1      
    Xw  Yw  Xw+1 Yw+1 Xw+2 Yw+2 ...  X2w-1 Y2w-1
    ...
    X(h-1)w Y(h-1)w   ............. X(h-1)(w-1)Y(h-1)(w-1)
1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
Function1 ( (float*) input_float.data , (float*) motion.data, (unsigned char*) out.data );
   Function2 ( float* input, float* R, float* G, float* B);
   Function3 ( float* R, float* G, float* B, float* W, float* Z);
for(int i=0; i< rows; i++){
        for(int j=0; j<col; j++)
            { 
             map_x.at<float>(i,j) = j + motion_vect.ptr<float>(i)[2*j+0];
             map_y.at<float>(i,j) = i + motion_vect.ptr<float>(i)[2*j+1];;
        }}
for(int i=0;i对于(int j=0;jTry“访问元素”中的建议)第节。如果你明白了,请友好地在这里回答你自己的问题。感谢这篇文章,我以前读过它,但我又读了一遍。我了解如何访问cv::Mat的元素。只是我不太理解map1和map2到底指的是什么。起初我以为它们是运动向量,所以我将它们设置为0。motion_0_32FC2=cv::Mat::zeros(img._size.height,cimg._size.width,cv_32FC2);然后我将此cv::Mat拆分为两个向量,并用它们调用了remap函数。之后,我看到输出将只使用第一个元素[位置0]从每个通道重复整个帧。如果我将它们设置为1,输出将在每个通道的位置1上获取元素并重复它。这就是为什么我认为map1 map2的内容可能应该是索引,所以我将它们设置为0 1 2 3 4 5 6 7 9 10 11。但是现在输出只有第一行和第二行其中一个与原来的不同。所以问题是,为了将图片重新映射到相同的位置并使输出相同,map1和map2应该是什么样子。然后,我将向此内容添加运动矢量。