79170688

Date: 2024-11-08 15:46:18
Score: 1
Natty:
Report link

Can you help me with this bug?

Your source of your problem seems to be a bug in opencv library itself.
I am not sure if/when it will be fixed.

But anyway I would recommend using opencv (and cv::resize) with cv::Mat which is the natual matrix container for opencv.

It is also better suited to represent a 2D array that vector<vector<T>> thanks to contiguous memory layout which is more efficient and cache-friendly.
It is considered quite a good matrix class in general.

Here's an example how to use it in your case:

#include <iostream>
#include <opencv2/opencv.hpp>

int main()
{
    // Create and fill input:
    cv::Mat at1(4, 4, CV_64FC1);
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            at1.at<double>(j, i) = 4. * i + j;
        }
    }

    // Print input:
    std::cout << "Input:\n";
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            std::cout << "  " << at1.at<double>(j, i);
        }
        std::cout << std::endl;
    }

    // Perform resize and create output:
    cv::Mat ax1;  // no need to initialize - will be done by cv::resize
    cv::resize(at1, ax1, cv::Size(2, 2), 0, 0, cv::INTER_CUBIC);

    // Print output:
    std::cout << "\nOutput:\n";
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            std::cout << "  " << ax1.at<double>(j,i);
        }
        std::cout << std::endl;
    }
}

Output:

Input:
  0  1  2  3
  4  5  6  7
  8  9  10  11
  12  13  14  15

Output:
  2.03125  4.21875
  10.7812  12.9688

Side notes:

  1. As commented above the cubic interpolation filter you chose is not a good one for these matrix sizes, but I assume in the real case they are bigger.
  2. For more efficient access to cv::Mat elements, use cv::Mat::ptr to get a line pointer instead of accessing each element with cv::Mat::at.
  3. Note that the indices to cv::Mat::at are given as (y,x) (i.e. row,column) and not (x,y) as some might expect.
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Blacklisted phrase (1): help me
  • Whitelisted phrase (-1): in your case
  • RegEx Blacklisted phrase (3): Can you help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Can you help me with this
  • High reputation (-2):
Posted by: wohlstad