From at least 3.4 to the current master build, an invalid example has been present in the cv::Mat documentation (https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html):
- Make a header for user-allocated data. It can be useful to do the following:
- Process "foreign" data using OpenCV (for example, when you implement a DirectShow* filter or a processing module for gstreamer, and so on). For example:
void process_video_frame(const unsigned char* pixels,
int width, int height, int step)
{
Mat img(height, width, CV_8UC3, pixels, step);
GaussianBlur(img, img, Size(7,7), 1.5, 1.5);
}
This example has two problems:
- It's not possible, as
pixels is const but there is no constructor for Mat which takes a const data pointer.
- It's broken, as the call to
GaussianBlur indirectly modifies pixels which the caller assumed to be const.
The easiest fix is to just remove the const from pixels in the example.
The wrong fix, if anybody is tempted, is to add const_cast because of point 2.
The practical fix, which also defeats the purpose of the example (and renders the example function useless):
Mat img(height, width, CV_8UC3);
for (int row = 0; row < height; ++ row)
memcpy(img.ptr(row), pixels + (row * step));
The fantasy fix is to work some template magic to allow Mat_<const unsigned char> to exist.
From at least 3.4 to the current master build, an invalid example has been present in the cv::Mat documentation (https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html):
This example has two problems:
pixelsisconstbut there is no constructor forMatwhich takes aconstdata pointer.GaussianBlurindirectly modifiespixelswhich the caller assumed to beconst.The easiest fix is to just remove the
constfrompixelsin the example.The wrong fix, if anybody is tempted, is to add
const_castbecause of point 2.The practical fix, which also defeats the purpose of the example (and renders the example function useless):
The fantasy fix is to work some template magic to allow
Mat_<const unsigned char>to exist.