4

我正在尝试从头开始实现openCV方法warpPerspective(),我编写了下面的代码,它可以处理y和x的变化,但是,当我将单应矩阵从findHomography()传递给函数时,我让它总是给出空白图像warpPerspective() 输出。

我按照这个定义来找到像素的新位置:

s*x' h1 h2 h3 x s*y' = h4 h5 h6 * y s h7 h8 1 1

我的映射适用于简单的转变,例如 { 1, 0.5,-51,0,1,50,0,0,1} 在此处输入图像描述

但是当矩阵是这样 [1.0340946, 0.032195676, -6.419126; 0.00302419, 1.0487343, -96.520393; 3.7013847e-06, 0.00010837225, 1] 的:输出是这样的: 在此处输入图像描述

我的实现: - 给定 H 和图像 A, - 查找 A 中像素的新位置并将它们保存在 TransArry 中。数组的索引是 A 的线性化索引。 - 将 A 的像素重新映射到 tranImg。

    Mat transform(Mat A, Mat H)
{
// allocate array of all locations
int Numrows = A.rows;
int Numcols = A.cols;
int channels   = A.channels();
cout << "rows " << Numrows << "col " << Numcols << "channels " << channels <<endl;
int size = Numrows*Numcols;
int MaxX,MaxY = -1000;
int MinX,MinY =  1000;
int *TransArry = (int *)malloc(sizeof(int)*size);
int Idx;

 int homeX=Idx % Numcols;
 int homeY=Idx / Numcols;
 cout << H << endl;

 waitKey();         
for (Idx=0; Idx < size; ++Idx ){

        homeX=Idx % Numcols;
        homeY=Idx / Numcols;

        float x  = (H.at<float>(0,0) * (homeX)) +( H.at<float>(0,1) * (homeY)) + ( H.at<float>(0,2) * 1) ;
        float y  = (H.at<float>(1,0) * (homeX)) +( H.at<float>(1,1) * (homeY)) + ( H.at<float>(1,2) * 1) ;
        float s  = (H.at<float>(2,0) * (homeX)) +( H.at<float>(2,1) * (homeY)) + ( H.at<float>(2,2) * 1) ;

        cout << " x = " << x << " y= " << y << " s= " << s;
        x = (x/s);

        y = y/s;

        // for the first col in TransMatrix
        if (homeX ==0){
            if (x > MaxX) MaxX = x;
            if (x < MinX) MinX = x; 
        }

        //for thee first row in TransMatrix
        if (homeY ==0){
            if (y > MaxY) MaxY = y;
            if (y < MinY) MinY = y;
        }
        if((y)>=A.rows || (y)<0 || (x)>=A.cols || (x)<0){
            TransArry[Idx]  = -1;
            cout << "x= " << x << "y= "<< y << endl;
        }else{
            TransArry[Idx] = (y * Numcols + x); 
        }           

        //cout << Numcols << endl;
        cout <<     "New index of " << Idx << "is " << TransArry[Idx] << endl;
        }

         Mat   tranImg ;

         A.copyTo(tranImg);
         tranImg = tranImg - tranImg;
        cout <<     "Rows" << tranImg.rows << "cols" << tranImg.cols << "cha" <<  A.channels() << endl;


        waitKey();
        // Remap Image
        for (Idx=0; Idx < size; Idx ++ ){

            homeX=Idx % Numcols;
            homeY=Idx / Numcols;                
            //tranImg.at<uchar>(homeY, homeX) =0;
            if(TransArry[Idx] != -1){   
                //cout << "Index " << Idx << "Passed " << endl;
                int newhomeX=TransArry[Idx] % Numcols; // Col ID
                int newhomeY=TransArry[Idx] / Numcols;  // Row ID


                 cout << "Index is " << Idx << endl;
                 cout << "HomeX is " << homeX << " and HomeY is " << homeY << endl;
                 cout << "New Index is " << TransArry[Idx] << endl;
                 cout << "New HomeX is " << newhomeX << " and New HomeY is " << newhomeY << endl;   
                 cout << "*****************************************"<< endl; 
                // if (!(Idx%100)) sleep(20);  

                tranImg.at<uchar>(newhomeY, (newhomeX*channels)) = A.at<uchar>(homeY, homeX*channels);
                if(channels>1)
                    tranImg.at<uchar>(newhomeY, newhomeX*channels+1) = A.at<uchar>(homeY, homeX*channels+1);
                if(channels>2)
                    tranImg.at<uchar>(newhomeY, newhomeX*channels+2) = A.at<uchar>(homeY, homeX*channels+2);
                // if (!(Idx%100)){
                    // imshow("inside", tranImg);
                    // waitKey(1);
                    // }
                }
        }
        //cout << tranImg << endl;  

return tranImg;

}

计算和验证 H。

那么,我访问矩阵 H 和 A 的方式有问题吗?

4

2 回答 2

3

在这里查看我的答案,它应该回答您的两个问题。这个答案仍然使用warpPerspective(),但它可以全面了解如何计算单应性扭曲图像的图像边界外多远,因此您可以在图像的每一侧适当地填充。

至于warpPerspective()手动操作,您只需将所有图像坐标放入线性齐次数组并乘以您的单应性,然后除以最后一个坐标即可返回笛卡尔坐标。然后,您可以使用remap()进行插值。for 的语法remap()可能会令人困惑,因此您可以在此处参考我的答案以了解如何使用它。这个答案显示了手动转换和插值,它应该给你(至少几乎)相同的结果warpPerspective()

于 2017-11-26T21:54:25.493 回答
0

我发现了问题。您使用的坐标是浮点值而不是整数。将 x 和 y 类型转换为整数,并将值 x = floor(x/s) 和 y =floor(y/s) 下限。

于 2018-11-16T13:55:34.157 回答