This is a followup question from this one,
I'm trying to do a simple resize using opencv which ends up crashing!
This is the example code which the access violation occurs:
void Classifier::Preprocess(const cv::Mat& img, std::vector<cv::Mat>* input_channels)
{
/* Convert the input image to the input image format of the network. */
cv::Mat sample;
if (img.channels() == 3 && num_channels_ == 1)
cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY);
else if (img.channels() == 4 && num_channels_ == 1)
cv::cvtColor(img, sample, cv::COLOR_BGRA2GRAY);
else if (img.channels() == 4 && num_channels_ == 3)
cv::cvtColor(img, sample, cv::COLOR_BGRA2BGR);
else if (img.channels() == 1 && num_channels_ == 3)
cv::cvtColor(img, sample, cv::COLOR_GRAY2BGR);
else
sample = img;
//resize image according to the input
cv::Mat sample_resized;
Size size(input_geometry_.width,input_geometry_.height );
if (sample.size() != input_geometry_)
cv::resize(sample, sample_resized, size);
else
sample_resized = sample;
//...
}
This is what I get in my C# solution when debugging :
Exception thrown at 0x00007FF8C8D9AA90 (opencv_imgproc310d.dll) in Classification Using dotNet.exe: 0xC0000005: Access violation reading location 0x0000018B000F2000.
If there is a handler for this exception, the program may be safely continued.
When I debug into the opencv code, I can see that sz
is a null struct(<struct at NULL>
) :
template<typename _Tp> inline
Size_<_Tp>::Size_(const Size_& sz)
: width(sz.width), height(sz.height) {}
and this clearly causes the access violation!.
What is wrong here and what should I do ?
Update :
More information :
sample.size()
is as exactly as img.size()
since the last else
clause is executed and thus sample = img
.
sample.size() = {width=256 height=378 } cv::Size_<int>
num_channels_
is 3
img.channels()
is 3 so is sample.channels()
When I use imshow()
to display either img or sample, the access violation exception is issued. creating a simple black image and displaying it in imshow() is fine though.