c++performanceopencvcomputer-visionprojective-geometry

OpenCV speed issue


I want to transform many points (whole 720p image would be best) with ~30fps. Right now i just loop through a mask and look for marked pixels. Then i transform every marked pixel to a new frame. Is there any way to speed it up? The code runs on a windows tablet, so i don't know if CUDA could help.

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

        }
    }

Solution

  • I managed to get the transform running for a 720p image in ~40ms, simply by using Matx instead of Mat. The images are stored in UMat, maybe this helped too.