OpenCV速度问题

时间:2015-09-08 12:37:33

标签: c++ performance opencv computer-vision projective-geometry

我想用~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;
            }

        }
    }

2 个答案:

答案 0 :(得分:4)

如果不进行测试,我认为逆相机矩阵的计算是代码中最昂贵的操作。假设摄像机矩阵是常数,您可以预先计算逆矩阵。

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

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

答案 1 :(得分:0)

您可以尝试使用 //get left drawable Drawable drawable = btn.getCompoundDrawables()[0]; int imgHeight = drawable.getIntrinsicHeight(); int imgWidth = drawable.getIntrinsicWidth(); //for dpToPixel look here: https://stackoverflow.com/a/6327095/2306907 float scale = (float) (dpToPixel(40)) / imgHeight; Rect rect = drawable.getBounds(); int newWidth = (int)(imgWidth * scale); int newHeight = (int)(imgHeight * scale); rect.left = rect.left + (int)(0.5 * (imgWidth - newWidth)); rect.top = rect.top + (int)(0.5 * (imgHeight - newHeight)); rect.right = rect.left + newWidth; rect.bottom = rect.top + newHeight; drawable.setBounds(rect); 进行性能测试吗?

我使用cv::UMat按像素进行快速图像操作。