-1
void GrayFilterCommand::apply_filter(IplImage* image) {
if (!image) {
    throw ....
}
IplImage *gray_image = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
if (!gray_image) {
    throw ....
}
cvCvtColor(image, gray_image, CV_BGR2GRAY);

image = cvCloneImage(gray_image); // does not work
}

I can not return the image, so I returned gray_image. How can I save a gray_image in image?

image = cvCloneImage(gray_image);

or

image = gray_image

does not work

russianstudent
  • 129
  • 2
  • 12
  • please stay away from opencv's deprecated c-api. they did away with that in 2010 already. use cv::Mat, cv::cvtColor(), etc. – berak May 24 '14 at 16:11
  • @berak, thanks for the advice. But i just wrote a big project on c-api and do not have time to fix everything. Help me to solve this particular problem please. – russianstudent May 24 '14 at 16:24
  • no, you shouldn't have. major mistake from start on. 2010 !! all your problems stem from using the wrong api. – berak May 24 '14 at 16:29

1 Answers1

0

your code above will probably result in a memleak ( who's to release image or gray_image ?)

the correct way ( using the c++ api ) would be:

void GrayFilterCommand::apply_filter(const Mat & rgb_image, Mat & gray_image) {
    cv::cvtColor(rgb_image, gray_image, CV_BGR2GRAY);
}

you wouldn't even consider making a function of it in c++.

berak
  • 39,159
  • 9
  • 91
  • 89
  • My code worked until I began to do pattern command and now i can't return image. In your code, you can use one argument? – russianstudent May 24 '14 at 16:52
  • sure. you could just use `cv::cvtColor(image, image, CV_BGR2GRAY);` and no, you can't do that 'inplace' with the c-api – berak May 24 '14 at 16:56
  • Thanks, but unfortunately this method does not suit me. I'll wait for another answer and try to solve the problem himself. – russianstudent May 24 '14 at 17:04