OpenCV速度问题

OpenCV speed issue

本文关键字:问题 速度 OpenCV      更新时间:2023-10-16

我想用~30fps变换许多点(整个720p图像最好)。现在我只是循环通过一个蒙版,并寻找标记像素。然后我将每个标记的像素转换为一个新的帧。有什么办法能加快速度吗?代码在windows平板电脑上运行,所以我不知道CUDA是否能帮上忙。

//Look for white pixels in mask image and transform them to new frame orientation
for (int row = 0; row < mask.rows; row++){
    for (int col = 0; col < mask.cols; col++){
        if (mask.at<uchar>(row, col) == 255){
            //Point in 2D hom
            p = (Mat_<double>(3, 1) << col, row, 1);
            p11 = CameraMatrix480.inv()*p;  //Pixel-->Camera

            //Project 2D Points to table
            double d = abs((p11 - midCam).dot(table_normal_cam)); //intersection of point with table surface is z value
            ps = p11 - d*table_normal_cam;
            p11 *= -Mat(p11 - ps).at<double>(2);
            //Get point in new frame in hom camera coordinates
            p11.copyTo(p_hom1(Range(0, 3), Range(0, 1)));
            p_hom2 = M * p_hom1; //p_hom in frame2
            //Point in frame2 in pixel coordinates
            p12 = (1 / p_hom2.at<double>(2))*(CameraMatrix480*p_hom2(Range(0, 3), Range(0, 1))); //Camera-->Pixel
            pixel = Point(p12.at<double>(0), p12.at<double>(1));
            //Check if new location is in the frame
            if (rect.contains(pixel)){
                RGB& rgb = output.ptr<RGB>(pixel.y)[pixel.x];
                rgb = white;
            }
        }
    }

没有测试,我认为逆相机矩阵的计算是你的代码中最昂贵的操作。假设相机矩阵是常数,你可以预先计算逆。

Mat invCameraMatrix(CameraMatrix480.inv());
...
p11 = invCameraMatrix*p;  //Pixel-->Camera
...

此外,您可以轻松地将for循环与OpenMP并行,并检查这是否会获得任何性能。要使用CUDA,您将需要一个Nvidia显卡,这可能在您的windows平板电脑设备中不可用。

您可以尝试使用cv::UMat进行性能测试吗?

我使用OpenMP快速图像操作像素

您是否考虑将您的操作更改为float而不是double ?由于您使用的是移动设备,因此它可能有助于加快操作速度。

另外,最后一个if条件中的rect是什么?

我设法在~40ms内获得720p图像的转换运行,只需使用Matx而不是Mat。图像存储在UMat中,也许这也有帮助。