Chris
Chris

Reputation: 22247

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

I have some allocated cv::Mat face; with actual data in it and I want to perform something along the lines of the following:

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);

This causes the program to crash, but I assumed in the new C++ API that cv::Mat objects were good at allocating their own memory. What is the simplest way to allocate the memory for those cv::Mat objects?

Upvotes: 2

Views: 1100

Answers (1)

SSteve
SSteve

Reputation: 10728

I changed the depth parameter in the call to Sobel and your code worked for me:

#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;
}

Upvotes: 1

Related Questions