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

时间:2011-11-08 01:16:24

标签: 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对象分配内存的最简单方法是什么?

1 个答案:

答案 0 :(得分:1)

我在调用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;
}