JDL Wahaha
JDL Wahaha

Reputation: 715

OpenCV: How to use cvSobel?

I'm trying to find the gradient direction from the edges using OpenCv 2.4.5, but I'm having problem with cvSobel() and below is the error message and my code. I read somewhere that it might be due to the conversion between floating point(??) but I have no idea on how to fix it. Any Help??

enter image description here

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2\opencv.hpp>
#include <opencv2\calib3d\calib3d.hpp>

#include <iostream>
#include <stdlib.h>
#include "stdio.h"

using namespace cv;
using namespace std;

int main()
{
    Mat im = imread("test1.jpg");
    if (im.empty()) {
        cout << "Cannot load image!" << endl;
    }
    Mat *dx, *dy;
    dx = new Mat( Mat::zeros(im.rows, im.cols, 1)); 
    dy = new Mat( Mat::zeros(im.rows, im.cols, 1));

    imshow("Image", im);

    // Convert Image to gray scale
    Mat im_gray;
    cvtColor(im, im_gray, CV_RGB2GRAY);
    imshow("Gray", im_gray);

            //trying to find the direction, but gives errors here
    cvSobel(&im_gray, dx, 1,0,3);


    waitKey(0);
    return 0;
}

Upvotes: 0

Views: 3343

Answers (1)

hetepeperfan
hetepeperfan

Reputation: 4411

You are mixing the C++ and C api. cv::Mat is from the C++ api and CvArr* is from the C api. here you are using The C api cvSobel on C++ classes.

//trying to find the direction, but gives errors here
cvSobel(&im_gray, dx, 1,0,3);

What happens if you do

cv::Sobel( im_gray, dx, im_gray.depth(), 1, 0, 3);

EDIT and declare

Mat dx;
Mat dy;

I think this might solve your problem, I'm actually quite surprised your code compiles.

Upvotes: 1

Related Questions