Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize without interpolation (zero-padding) in OpenCV?

Is there an efficient way to resize an image in OpenCV without using any interpolation? Instead of the conventional "resize" I would like my image to remap the pixels into a larger image but pad everything else with 0.

e.g. to scale up img1 below 2x to img2:

img1 = [ 1, 2, 3,
         4, 5, 6,
         7, 8, 9 ]

cv::resize(img1, img2, cv::Size(6, 6));

img2 = [ 1, 0, 2, 0, 3, 0,
         0, 0, 0, 0, 0, 0,
         4, 0, 5, 0, 6, 0,
         0, 0, 0, 0, 0, 0,
         7, 0, 8, 0, 9, 0,
         0, 0, 0, 0, 0, 0 ]

I know the obvious way is to just use a for loop, but I'm wondering if there is a more efficient way using an OpenCV call?

like image 330
Sefu Avatar asked Jan 05 '23 22:01

Sefu


1 Answers

One option that comes to mind would be to use cv::resize with INTER_NEAREST and then mask out the unwanted pixels.

Example:

#include <opencv2/opencv.hpp>

#include <cstdint>
#include <iostream>

int main()
{
    cv::Mat m1((cv::Mat_<uint8_t>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9));
    std::cout << "Input:\n" << m1 << "\n\n";

    cv::Mat mask((cv::Mat_<uint8_t>(2, 2) << 255, 0, 0, 0));
    mask = cv::repeat(mask, m1.rows, m1.cols);
    std::cout << "Mask:\n" << mask << "\n\n";

    cv::Mat m2;
    cv::resize(m1, m2, cv::Size(), 2, 2, cv::INTER_NEAREST);
    std::cout << "Resized:\n" << m2 << "\n\n";

    cv::bitwise_and(m2, mask, m2);
    std::cout << "Masked:\n" << m2 << "\n\n";
}

Console output:

Input:
[  1,   2,   3;
   4,   5,   6;
   7,   8,   9]

Mask:
[255,   0, 255,   0, 255,   0;
   0,   0,   0,   0,   0,   0;
 255,   0, 255,   0, 255,   0;
   0,   0,   0,   0,   0,   0;
 255,   0, 255,   0, 255,   0;
   0,   0,   0,   0,   0,   0]

Resized:
[  1,   1,   2,   2,   3,   3;
   1,   1,   2,   2,   3,   3;
   4,   4,   5,   5,   6,   6;
   4,   4,   5,   5,   6,   6;
   7,   7,   8,   8,   9,   9;
   7,   7,   8,   8,   9,   9]

Masked:
[  1,   0,   2,   0,   3,   0;
   0,   0,   0,   0,   0,   0;
   4,   0,   5,   0,   6,   0;
   0,   0,   0,   0,   0,   0;
   7,   0,   8,   0,   9,   0;
   0,   0,   0,   0,   0,   0]

Update

If we eliminate parts of Miki's code that are unnecessary for our specific scenario, we pretty much reduce it to a simple loop.

Doing some quick comparisons, this turns out to be somewhat faster.

#include <opencv2/opencv.hpp>

#include <chrono>
#include <cstdint>
#include <iostream>

cv::Mat resize_1(cv::Mat image)
{
    cv::Mat result(cv::Mat::zeros(image.rows * 2, image.cols * 2, CV_8UC1));

    for (int ra(0); ra < image.rows; ++ra) {
        for (int ca = 0; ca < image.cols; ++ca) {
            result.at<uint8_t>(ra * 2, ca * 2) = image.at<uint8_t>(ra, ca);
        }
    }

    return result;
}

cv::Mat resize_2(cv::Mat image)
{
    cv::Mat mask((cv::Mat_<uint8_t>(2, 2) << 255, 0, 0, 0));
    mask = cv::repeat(mask, image.rows, image.cols);

    cv::Mat result;
    cv::resize(image, result, cv::Size(), 2, 2, cv::INTER_NEAREST);
    cv::bitwise_and(result, mask, result);

    return result;
}

template<typename T>
void timeit(T f)
{
    using std::chrono::high_resolution_clock;
    using std::chrono::duration_cast;
    using std::chrono::microseconds;

    cv::Mat m1((cv::Mat_<uint8_t>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9));
    m1 = cv::repeat(m1, 1024, 1024);

    high_resolution_clock::time_point t1 = high_resolution_clock::now();
    for (uint32_t i(0); i < 256; ++i) {
        cv::Mat result = f(m1);
    }
    high_resolution_clock::time_point t2 = high_resolution_clock::now();

    auto duration = duration_cast<microseconds>(t2 - t1).count();
    double t_ms(static_cast<double>(duration) / 1000.0);
    std::cout
        << "Total = " << t_ms << " ms\n"
        << "Iteration = " << (t_ms / 256) << " ms\n"
        << "FPS = " << (256 / t_ms * 1000.0) << "\n";
}

int main()
{
    timeit(&resize_1);
    timeit(&resize_2);
}

Timing:

resize_1

Total = 6344.86 ms
Iteration = 24.7846 ms
FPS = 40.3476

resize_2

Total = 7271.31 ms
Iteration = 28.4036 ms
FPS = 35.2068

Update 2

Parallelized version:

class ResizeInvoker : public cv::ParallelLoopBody
{
public:
    ResizeInvoker(cv::Mat const& src, cv::Mat& dst)
        : image(src)
        , result(dst)
    {
    }

    void operator()(const cv::Range& range) const
    {
        for (int y(range.start); y < (range.end); ++y) {
            for (int x(0); x < image.cols; ++x) {
                result.at<uint8_t>(y * 2, x * 2) = image.at<uint8_t>(y, x);
            }
        }
    }

    cv::Mat const& image;
    cv::Mat& result;
};

cv::Mat resize_3(cv::Mat image)
{
    cv::Mat result(cv::Mat::zeros(image.rows * 2, image.cols * 2, CV_8UC1));

    ResizeInvoker loop_body(image, result);
    cv::parallel_for_(cv::Range(0, image.rows)
        , loop_body
        , result.total() / (double)(1 << 16));

    return result;
}

Timing:

resize_3

Total = 3876.63 ms
Iteration = 15.1431 ms
FPS = 66.0367

Update 3

We can do a little better if we use raw pointers in the invoker:

void operator()(const cv::Range& range) const
{
    for (int y(range.start); y < (range.end); ++y) {
        uint8_t* D = result.data + result.step * y * 2;
        uint8_t const* S = image.data + image.step * y;
        for (int x(0); x < image.cols; ++x) {
            D[x * 2] = S[x];
        }
    }
}

Timing:

Total = 3387.87 ms
Iteration = 13.2339 ms
FPS = 75.5636
like image 189
Dan Mašek Avatar answered Feb 01 '23 15:02

Dan Mašek