I´m trying to work with C, Lua and OpenCv but I´m having problems with OpenCv methods.

First, I want to apply grayscale into an image. I´m trying this:

static int treatments_grayscale (lua_State * L) {
    void *ptr;
    ptr=lua_touserdata(L,1);

    int w,h;
    w=lua_tointeger(L,2);
    h=lua_tointeger(L,3);

    cv::Mat * img=new cv::Mat(w,h,CV_8UC4,ptr);

    cvCvtColor(img,img,CV_RGB2GRAY);

    // Return image
    lua_pushlstring(L,(char *)img->data,w*h*4);
    return 1;
}

When I run this code, I get the following error:

"Unknown array type in function cvarrToMat"

What am I doing wrong?

I also tried like this:

cv::Mat * img=new cv::Mat(w,h,CV_8UC4,ptr);

IplImage * dst_img= cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 4);
dst_img->imageData = (char *) img->data;      

cvCvtColor(dst_img,dst_img,CV_RGB2GRAY);

And then... the error:

"error: (-215) dst.data == dst0.data in function cvCvtColor"

Thank you! =)

有帮助吗?

解决方案

I think there is an error because the source image should not have the same size as the destination image. When i convert a rgb image to gray i use something like that :

IplImage * cvImage = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
IplImage * cvGray = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
cvImage->imageData = (char *) img->data;
cvCvtColor(cvImage, cvGray, CV_RGB2GRAY);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top