Mat::create

Mat::create

Allocates new array data if needed.

C++:  void  Mat:: create (int  rows, int  cols, int  type )
C++:  void  Mat:: create (Size  size, int  type )
C++:  void  Mat:: create (int  ndims, const int*  sizes, int  type )
Parameters:
  • ndims – New array dimensionality.
  • rows – New number of rows.
  • cols – New number of columns.
  • size – Alternative new matrix size specification: Size(cols, rows)
  • sizes – Array of integers specifying a new array shape.
  • type – New matrix type.

This is one of the key Mat methods. Most new-style OpenCV functions and methods that produce arrays call this method for each output array. The method uses the following algorithm:

  1. If the current array shape and the type match the new ones, return immediately. Otherwise, de-reference the previous data by calling Mat::release().
  2. Initialize the new header.
  3. Allocate the new data of total()*elemSize() bytes.
  4. Allocate the new, associated with the data, reference counter and set it to 1.

Such a scheme makes the memory management robust and efficient at the same time and helps avoid extra typing for you. This means that usually there is no need to explicitly allocate output arrays. That is, instead of writing:

Mat color;
...
Mat gray(color.rows, color.cols, color.depth());
cvtColor(color, gray, CV_BGR2GRAY);

you can simply write:

Mat color;
...
Mat gray;
cvtColor(color, gray, CV_BGR2GRAY);

because cvtColor , as well as the most of OpenCV functions, calls Mat::create() for the output array internally.

OpenCV中出现`undefined reference to cv::Mat::create`这类链接错误,通常与编译时的库链接配置或版本不一致有关。以下是几种可能的解决方法: 1. **确保正确链接OpenCV库** 在CMake配置文件中(`CMakeLists.txt`),需要确保OpenCV库被正确引入。例如: ```cmake find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(your_target_name ${OpenCV_LIBS}) ``` 如果使用的是特定模块(如`opencv_core`、`opencv_imgproc`等),需要单独链接这些库[^3]。 2. **检查OpenCV版本与编译器的兼容性** 某些OpenCV版本可能与特定编译器工具链不兼容。例如,使用MinGW编译时,需要确保OpenCV版本与MinGW版本匹配。如果使用的是OpenCV的预编译库,需确认其构建时所用的编译器版本是否与当前环境一致。若版本不匹配,可能需要自行编译OpenCV源码以适配当前环境[^3]。 3. **避免混合使用不同编译器构建的库** 如果OpenCV库是使用MSVC编译的,而当前项目使用的是MinGW编译器,可能会导致链接错误。这种情况下,应使用与项目编译器一致的工具链重新编译OpenCV[^3]。 4. **检查OpenCV库的完整性** 在某些情况下,下载的OpenCV库可能缺少必要的模块或构建不完整。可以尝试重新构建OpenCV库,确保所有模块都被正确编译和链接。使用CMake配置OpenCV构建时,启用`BUILD_EXAMPLES`和`BUILD_TESTS`选项有助于验证构建过程是否完整[^3]。 5. **更新OpenCV版本** 如果使用的是较旧的OpenCV版本(如3.x系列),某些API(如`cv::Mat::create`)可能在后续版本中发生了变化或被弃用。尝试升级到较新的OpenCV版本(如4.9.0)以确保API兼容性[^3]。 6. **手动指定库路径** 如果OpenCV库不在系统默认的搜索路径中,可以在CMake中手动指定库路径。例如: ```cmake set(OpenCV_DIR "path/to/opencv/build") find_package(OpenCV REQUIRED) ``` 7. **检查函数调用方式** 确保使用的函数调用方式与当前OpenCV版本一致。例如,`cv::Mat::create`在某些版本中可能已被弃用,推荐使用`cv::Mat`的构造函数或`cv::Mat::create`的替代方法。如果代码中存在直接调用底层函数(如`cv::Mat::updateContinuityFlag()`),这些函数可能是OpenCV内部使用的,不应在用户代码中直接调用。应避免直接调用此类内部函数,改用更高层的API[^1]。 8. **清理并重新构建项目** 有时,构建系统可能残留旧的配置或缓存文件,导致链接错误。可以尝试清理构建目录并重新生成构建文件: ```bash rm -rf build/ mkdir build && cd build cmake .. make ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值