c++ - 分配 cv::Mat 对象的最通用方法是什么?

标签 c++ opencv

我有一些已分配的 cv::Mat face;,其中包含实际数据,我想执行以下操作:

cv::Mat gray_image;
cv::Mat x_gradient_image;
cv::Mat temp;

cv::cvtColor(face, gray_image, CV_RGB2GRAY);
cv::Sobel(gray_image, temp, 1, 1, 0); 
cv::convertScaleAbs(temp, x_gradient_image, 1, 0);

这会导致程序崩溃,但我在新的 C++ API 中假设 cv::Mat 对象擅长分配自己的内存。为那些 cv::Mat 对象分配内存的最简单方法是什么?

最佳答案

我在对 Sobel 的调用中更改了深度参数,您的代码对我有用:

#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

int main(int argc, const char * argv[]) {

    cv::Mat face;
    // read an image
    if (argc < 2)
        face = cv::imread("../../IMG_0080.jpg");
    else
        face = cv::imread(argv[1]);

    if (!face.data) {
        std::cout << "Image file not found\n";
        return 1;
    }

    cv::Mat gray_image;
    cv::Mat x_gradient_image;
    cv::Mat temp;

    cv::cvtColor(face, gray_image, CV_RGB2GRAY);
    cv::Sobel(gray_image, temp, 5, 1, 0); 
    cv::convertScaleAbs(temp, x_gradient_image, 1, 0);

    // show the image in a window
    cv::imshow("so8044872", x_gradient_image);
    // wait for key
    cv::waitKey(0);

    return 0;
}

关于c++ - 分配 cv::Mat 对象的最通用方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8044872/

相关文章:

c++ - 运行文件时保持cmd打开

c++ - C++线程创建/删除与线程停止/恢复

java - undefined symbol : _ZTTN2cv4SURFE

python - opencv python具有坐标的三角形网格

c++ - OPENCV 无法在 Debug模式下打开我的图像

c++ - 在 C++ 中使用抽象类进行依赖注入(inject)

c++ - C++中的饱和短(int16)

c++ - 将值插入 vector 时没有任何反应

opencv - 将 cython 内存 View 传递给 OpenCV 函数

c++ - cornerSubPix() 可以检测到的最大角点数