Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Looking for ways to speed up pixel scanning

So I am trying to implement a pixel scanner, where I go through every pixel and check if it is white (the image has already been threshold-ed at this point) and if it is white get the pixel's respective x and y coordinate which I use to calculate distances. So far I have been doing it the double for-loop way, and it is adding significant slowdowns compared to the rest of my pipeline. I would like to speed this up. I have looked into using cv::findNonZero(), it gives me a vector of cv::Point, when I try and access the vector at some interation like vec[ i ] the output is [ rowValue, colValue ] which is expected but I don't know how to access the "rowValue" or "colValue" independently that I need in order to calculate distances. I have heard about using cv::LUT() but I'm having some trouble understanding how to use it. Thanks is advanced and let me know if more info is needed. Code below:

   cv::Mat W; // approx 1000 x 1000
    for (int i = 0; i < W.cols; i++)
    {
        for (int j = 0; j < W.rows; j++)
        {
            cv::Scalar pixelColor = counter.at<uchar>(j, i);
            if (pixelColor.val[0] == 255)
            {
                // pixelLocations are defined as a ROS message, A B C D are constants used to 
                // calculate distances in meters
                pixelLocation.x = (A * i) + B; // distance in x direction relative to robot
                pixelLocation.y = (C * j) + D; // distance in front of robot 
            }
       }
    }

Looking for ways to speed up pixel scanning

So I am trying to implement a pixel scanner, where I go through every pixel and check if it is white (the image has already been threshold-ed at this point) and if it is white get the pixel's respective x and y coordinate which I use to calculate distances. So far I have been doing it the double for-loop way, and it is adding significant slowdowns compared to the rest of my pipeline. I would like to speed this up. I have looked into using cv::findNonZero(), it gives me a vector of cv::Point, when I try and access the vector at some interation like vec[ i ] the output is [ rowValue, colValue ] which is expected but I don't know how to access the "rowValue" or "colValue" independently that I need in order to calculate distances. I have heard about using cv::LUT() but I'm having some trouble understanding how to use it. Thanks is advanced and let me know if more info is needed. Code below:

   cv::Mat W; // approx 1000 x 1000
    for (int i = 0; i < W.cols; i++)
    {
        for (int j = 0; j < W.rows; j++)
        {
            cv::Scalar pixelColor = counter.at<uchar>(j, i);
            if (pixelColor.val[0] == 255)
            {
                // pixelLocations are defined as a ROS message, A B C D are constants constants
                // used to 
                // calculate distances in meters
                pixelLocation.x = (A * i) + B; // distance in x direction relative to robot
                pixelLocation.y = (C * j) + D; // distance in front of robot 
            }
       }
    }
click to hide/show revision 3
None

updated 2019-03-14 02:44:14 -0600

berak gravatar image

Looking for ways to speed up pixel scanning

So I am trying to implement a pixel scanner, where I go through every pixel and check if it is white (the image has already been threshold-ed at this point) and if it is white get the pixel's respective x and y coordinate which I use to calculate distances. So far I have been doing it the double for-loop way, and it is adding significant slowdowns compared to the rest of my pipeline. I would like to speed this up. I have looked into using cv::findNonZero(), it gives me a vector of cv::Point, when I try and access the vector at some interation like vec[ i ] the output is [ rowValue, colValue ] which is expected but I don't know how to access the "rowValue" or "colValue" independently that I need in order to calculate distances. I have heard about using cv::LUT() but I'm having some trouble understanding how to use it. Thanks is advanced and let me know if more info is needed. Code below:

   cv::Mat W; // approx 1000 x 1000
    for (int i = 0; i < W.cols; i++)
    {
        for (int j = 0; j < W.rows; j++)
        {
            cv::Scalar pixelColor = counter.at<uchar>(j, i);
            if (pixelColor.val[0] == 255)
            {
                // pixelLocations are defined as a ROS message, A B C D are constants
                // used to calculate distances in meters
                pixelLocation.x = (A * i) + B; // distance in x direction relative to robot
                pixelLocation.y = (C * j) + D; // distance in front of robot 
            }
       }
    }