I have a matrix that I would like to store integer pairs into it, so I set it up like this:
// src is some image (CV_8UC1) I used as a mask
cpc = Mat(cv::Size(src.rows, src.cols), CV_16SC2);
// initialization
for( int y = 0; y < src.rows; ++y )
for( int x = 0; x < src.cols; ++x )
{
if ((int) src.at<uchar>(y,x) == 0)
{
cpc.at<cv::Vec2i>(y,x) = cv::Vec2i(y,x);
} else {
cpc.at<cv::Vec2i>(y,x) = cv::Vec2i(-1,-1);
}
std::cout << "\tcpc(" << y << "," << x << "): " << cpc.at<Vec2i>(y,x)[0] << ", " << cpc.at<Vec2i>(y,x)[1] << std::endl;
}
std::cout << "\t********************************************************************************" << std::endl;
std::cout << "\t********************************************************************************" << std::endl;
for( int y = 0; y < src.rows; ++y )
for( int x = 0; x < src.cols; ++x )
std::cout << "\tcpc(" << y << "," << x << "): " << cpc.at<Vec2i>(y,x)[0] << ", " << cpc.at<Vec2i>(y,x)[1] << std::endl;
You'd expect the two print-outs to be identical, but they are not. Look at (0,3) for example:
cpc(0,0): -1, -1
cpc(0,1): 0, 1
cpc(0,2): -1, -1
cpc(0,3): -1, -1
cpc(1,0): -1, -1
cpc(1,1): 1, 1
cpc(1,2): -1, -1
cpc(1,3): -1, -1
cpc(2,0): -1, -1
cpc(2,1): 2, 1
cpc(2,2): -1, -1
cpc(2,3): -1, -1
cpc(3,0): -1, -1
cpc(3,1): 3, 1
cpc(3,2): -1, -1
cpc(3,3): -1, -1
********************************************************************************
********************************************************************************
cpc(0,0): -1, -1
cpc(0,1): 0, 1
cpc(0,2): -1, -1
cpc(0,3): 1, 1
cpc(1,0): -1, -1
cpc(1,1): 1, 1
cpc(1,2): -1, -1
cpc(1,3): 2, 1
cpc(2,0): -1, -1
cpc(2,1): 2, 1
cpc(2,2): -1, -1
cpc(2,3): 3, 1
cpc(3,0): -1, -1
cpc(3,1): 3, 1
cpc(3,2): -1, -1
cpc(3,3): -1, -1
I am wondering if this is OpenCV's bug since the same code with CV_32FC2 and Vec2f format does not produce unexpected result.
Assigning Vec2i matrix with Scalar seems to also produce a strange behavior when I assign it with negative value. The following line:
cpc = Mat(cv::Size(src.rows, src.cols), CV_16SC2, Scalar(-1, -1));
will sometimes give value print-out as 0 instead of -1. Anyone experienced this before? I also tried Scalar_<int>(-1,-1)
but it still didn't initialize the matrix cpc
correctly with all -1.