سؤال

I have a function, with the following signature:

Mat cartoonifyImage( Mat, Mat );

I also have a VS2010 program as follows, where I apply to a webcam stream a number of filters, as taught in this book: Mastering OpenCV

int main( int argc, const char** argv )
{

  VideoCapture camera;
  camera.open(0);

  if( !camera.isOpened() )
  {
    cerr << "Could not access the camera!" << endl;
    return  1;
  }

  while( true )
  {
    Mat cameraFrame;
    camera >> cameraFrame;

    if( cameraFrame.empty() )
    {
      cerr << "Could not grab a camera frame!" << endl;
      return  1;
    }

    // imshow( "Camera Test", cameraFrame );

    Mat displayedFrame( cameraFrame.size(), CV_8UC3 );

    cartoonifyImage( cameraFrame, displayedFrame );

    imshow( "Cartoonifier!", displayedFrame );

    int keypress = waitKey( 20 );
    if( keypress == 27 ) break;
  }

}

Here is my function definition:

Mat cartoonifyImage( Mat srcColor, Mat mask )
{
  Mat gray, edges;

  cvtColor( srcColor, gray, CV_BGR2GRAY );

  const int MEDIAN_BLUR_FILTER_SIZE = 7;
  const int LAPLACIAN_FILTER_SIZE = 5;
  const int EDGES_THRESHOLD = 80;

  medianBlur( gray, gray, MEDIAN_BLUR_FILTER_SIZE );
  Laplacian( gray, edges, CV_8U, LAPLACIAN_FILTER_SIZE );
  threshold( edges, mask, EDGES_THRESHOLD, 255, THRESH_BINARY_INV );

  return( mask );
}

When I run the program, I get a blank (gray) window. Where the first imshow is commented out, I made sure the webcam is working and I can see my own image in the window, so the problem must be elsewhere. Can anyone help me understand where the problem is and what I am doing wrong?

Thank you,

هل كانت مفيدة؟

المحلول

your displayedFrame never got filled.

(you pass it into the func, it gets manipulated there, but since you gave it a copy, you don't get the result back)

either return a Mat from cartoonifyImage:

Mat displayed = cartoonifyImage( cameraFrame );

or pass references :

void cartoonifyImage( const Mat & cameraFrame, Mat & displayedFrame );

نصائح أخرى

Mat cartoonifyImage( Mat srcColor )
{
    Mat gray, edges, mask;

    cvtColor( srcColor, gray, CV_BGR2GRAY );

    const int MEDIAN_BLUR_FILTER_SIZE = 7;
    const int LAPLACIAN_FILTER_SIZE = 5;
    const int EDGES_THRESHOLD = 80;

    medianBlur( gray, gray, MEDIAN_BLUR_FILTER_SIZE );
    Laplacian( gray, edges, CV_8U, LAPLACIAN_FILTER_SIZE );
    threshold( edges, mask, EDGES_THRESHOLD, 255, THRESH_BINARY_INV );

    return ( mask );
}

int main( int argc, const char** argv )
{

    VideoCapture camera;
    camera.open(0);

    if( !camera.isOpened() )
    {
        cerr << "Could not access the camera!" << endl;
        return  1;
    }

    while( true )
    {
        Mat cameraFrame;
        camera >> cameraFrame;

        if( cameraFrame.empty() )
        {
            cerr << "Could not grab a camera frame!" << endl;
            return  1;
        }

        //imshow( "Camera Test", cameraFrame );

        Mat displayedFrame( cameraFrame.size(), CV_8UC3 );

        displayedFrame = cartoonifyImage(cameraFrame);

        imshow( "Cartoonifier!", displayedFrame );

        int keypress = waitKey( 20 );
        if( keypress == 27 ) break;
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top