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:
cv::Mat
elements, use cv::Mat::ptr
to get a line pointer instead of accessing each element with cv::Mat::at
.cv::Mat::at
are given as (y,x) (i.e. row,column) and not (x,y) as some might expect.