简体   繁体   English

使用带有float *的openCV重映射

[英]Using openCV remap with float*

I'm currently working on a project which involves motion compensation. 我目前正在从事一个涉及运动补偿的项目。

At this point I have the input image in cv::Mat input; 这时我在cv :: Mat输入中有输入图像;

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.

Now the motion vectors are stored in a 2 channel cv::Mat motion and it's stored like this: 现在,运动矢量存储在2通道cv :: Mat运动中,其存储方式如下:

// 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)

So if I use : 所以,如果我使用:

std::vector<cv::Mat> motionvect(2);
cv::split(motion, motionvect);

I will get X in motionvect(0) and Y in motionvect2(1). 我将在motionvect(0)中获得X,在motionvect2(1)中获得Y。

Furthermore I've tried: 此外,我尝试了:

std::vector<cv::Mat> rgbChannels(3);
cv::split(input_float, rgbChannels);
cv::remap(rgbChannels[0], outChannels[0], motionvect[0], motionvect[1], CV_INTER_LINEAR, IPL_BORDER_CONSTANT, cvScalar(0,0, 0));
cv::remap(rgbChannels[1], outChannels[1], motionvect[0], motionvect[1], CV_INTER_LINEAR, IPL_BORDER_CONSTANT, cvScalar(0,0, 0));
cv::remap(rgbChannels[2], outChannels[2], motionvect[0], motionvect[1], CV_INTER_LINEAR, IPL_BORDER_CONSTANT, cvScalar(0,0, 0));

cv::merge(outChannels, input_with_motion);

The results I get after this are very strange and somebody told me it is because of the Y vector. 我得到的结果非常奇怪,有人告诉我这是因为Y向量。 Aparently if X is like this: 显然,如果X像这样:

1  2  3  4  5
6  7  8  9  10
11 12 13 14 15

Then Y should be : 那么Y应该是:

1  6  11  
2  7  12  
3  8  13
4  9  14
5  10 15

Is that correct? 那是对的吗? Sort of transpose matrix. 转置矩阵的排序。 If it is true, is there any function that will change the motiovect[2] to look like this or should I do it for each individual component? 如果是真的,是否有任何函数可以将motiovect [2]更改为以下形式,还是应该对每个单独的组件进行更改?

Also, for the final step I would need to apply the remap at a different time in my process. 另外,对于最后一步,我需要在过程中的其他时间应用重新映射。 Something like this: 像这样:

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);

where R, G, B, W, Z are float* types and they are allocated with 其中R,G,B,W,Z为float *类型,并分配有

 R = (float*) malloc (frame_size * sizeof(float) );

I need to apply motion compensation to float* A and float* B, but these are not allowed inputs to remap. 我需要对float * A和float * B应用运动补偿,但是不允许这些输入重新映射。 They have to be InputArray and I can't find any method to convert float* to any accepted InputArray type. 它们必须是InputArray,我找不到任何将float *转换为任何可接受的InputArray类型的方法。

I know I'm not very organized in thoughts, but I hope you'll understand at least one of my questions. 我知道我思想上不太有条理,但希望您能至少理解我的一个问题。 1. How to apply remap if I have the motion vectors stored in a cv::Mat like x0 y0 x1 y1 .. ? 1.如果我将运动矢量存储在x0 y0 x1 y1 ..这样的cv :: Mat中,该如何应用重映射? 2. Can I convert float* to an accepted input for cv::remap ? 2.我可以将float *转换为cv :: remap的可接受输入吗?

I've found this tutorial http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/remap/remap.html very useful for understanding how to set map1, map2 parameters. 我发现本教程http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/remap/remap.html对于理解如何设置map1,map2参数非常有用。 In the end it's something like this: 最后是这样的:

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];;
        }}

To have the output image as the original : 要将输出图像作为原始图像:

map_x.at<float>(i,j) = j ;
map_y.at<float>(i,j) = i ;

For your #2, you can do this to get float values from pointer into some Mat: 对于您的#2,您可以执行以下操作以将浮点值从指针放入某些Mat中:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM