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

What is the most generic way to allocate a cv::Mat object?

本文关键字:方法 是什么 cv Mat 对象 分配      更新时间:2023-10-16

我有一些已分配的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 foundn";
        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;
}