Skip to content

Invalid example in cv::Mat documentation #19131

@JC3

Description

@JC3

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:
    1. 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:

  1. It's not possible, as pixels is const but there is no constructor for Mat which takes a const data pointer.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions