I have a function, resizeToShortSide, which takes a Mat and resizes its short side to the specified value. To resize, I setup a destination mat, resizedMat, perform the resize, and then assign resizedMat to the input Mat, mat. This occurs successfully.
However, when the function ends, the Mat which was passed to the function is again its original size, as if the assignment to resizedMat had not occured! OpenCv Mat are always passed by reference, so I'm not sure why it is acting like a copy of the Mat is being passed. Here is the function in question...
void resizeToShortSide(Mat mat, int shortSide, int resamplingMethod)
{
//determine which side is the short side
bool shortSideIsRows;
if (mat.rows <= mat.cols) {
shortSideIsRows = true;
} else {
shortSideIsRows = false;
}
//caluculate the size of the long side
Size outputSize;
if (shortSideIsRows) {
int cols = (shortSide / (float) mat.rows) * mat.cols;
int rows = shortSide;
outputSize = Size(cols, rows);
} else {
int rows = (shortSide / (float) mat.cols) * mat.rows;
int cols = shortSide;
outputSize = Size(cols, rows);
}
//setup a destination mat
Mat resizedMat(outputSize, CV_8UC4);
//resize
if (resamplingMethod == BashSettings::ResamplingMethod::NearestNeighbor)
resize(mat, resizedMat, outputSize, 0, 0, INTER_NEAREST);
else
resize(mat, resizedMat, outputSize); //defaults to INTER_LINEAR
//assign mat to resized mat
mat = resizedMat;
qDebug() << "resize to short side " << shortSide;
qDebug() << "resized mat width, height " << resizedMat.cols << ", " << resizedMat.rows;
qDebug() << "input mat width, height " << mat.cols << ", " << mat.rows;
qDebug() << " ";
}
The Mat
class itself contains some "header information" as well as a pointer to a UMatData
object. The UMatData
handles the reference counting. Unfortunately, the MatSize size
definition is in the Mat
object. Since your function resizeToShortSide
takes a Mat
by value, there's no way for the size to update upon return by your function. You'll still need to pass your Mat
by reference. Here is a relevant portion of the Mat
class definition:
class CV_EXPORTS Mat
{
...
//! interaction with UMat
UMatData* u;
MatSize size;
MatStep step;
...
}
Notice that the cv::resize
function is defined with InputArray
and OutputArray
parameters.
void cv::resize(InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR)
These InputArray
and OutputArray
classes implement constructors that wrap a Mat
reference and so changes can persist to dst
_InputArray(const Mat& m);
_OutputArray(Mat& m);
您按值将mat
传递给函数,按引用重试或返回resizeMat
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.