Implement Nearest Neighbour Interpolation using a Lookup Table?

asked 2017-03-04 21:38:41 -0500

sazr gravatar image

Is it possible to use a Lookup Table to perform Nearest Neighbour Interpolation? I have implemented this algorithm using standard for loops. But I have heard using Lookup Tables are much more efficient.

*Note I am aware of cv::resize() that can perform Nearest Neighbour Interpolation. I implementing my own Nearest Neighbour Interpolation for learning.

My below code performs Nearest Neighbour Interpolation using for loops. How can I convert it to use a cv::LUT?

void nearest_neighbour_interpolation(const Mat& image, Mat& result, double scaleFactor)
{
    // TODO: handle both 1 channel and 3 channel
    // TODO: see if can use a LUT here

    if (scaleFactor < 1.0000)
        return;

    int width = image.cols*scaleFactor;
    int height = image.rows*scaleFactor;
    result = Mat(height, width, image.type());

    int x_ratio = (int)((image.cols << 16) / width) + 1;
    int y_ratio = (int)((image.rows << 16) / height) + 1;

    for (int c = 0; c < width; c++) {
        for (int r = 0; r < height; r++) {

            int imageC = ((c*x_ratio) >> 16); // c * scaleFactor?
            int imageR = ((r*y_ratio) >> 16);
            result.at<Vec3b>(Point(c, r)) = image.at<Vec3b>(Point(imageC, imageR));
        }
    }
}
edit retag flag offensive close merge delete

Comments

I do not think it is possible to use a Lookup Table to perform Nearest Neighbour Interpolation.And why do you want to use a Lookup Table to perform Nearest Neighbour Interpolation?

jsxyhelu gravatar imagejsxyhelu ( 2017-03-05 08:25:48 -0500 )edit