Odd imshow() results during debug

asked 2015-12-20 23:53:49 -0500

zipzit gravatar image

updated 2015-12-21 00:03:44 -0500

I'm trying to learn exactly how OpenCV works. Right now I'm working thru the sample programs provided with the book ("Mastering OpenCV with Practical Computer Vision Projects") Source code available (here), and here at Github.

I'm working on a fun example, Cartoonify, from "Chapter 1. Cartoonifier and Skin Changer for Android", written by Daniel Lelis Baggio. I'm just working the sample in desktop mode. The sample, updated to run on Win10, Microsoft Visual Studio 2015, and OpenCV3.0 compiled anew from github works generally well.

However, I'm unable to get the alien mode (cast to green skin color) to function at all. No errors, just no output. I'm attempting to step through the code in debugger mode. So I understand what the code is doing, I'm just not sure what is going south when I enter the following code...

I'm getting weird display failures after a simple if statement. Anybody been here before? What am I doing wrong?

void cartoonifyImage(Mat srcColor, Mat dst, bool sketchMode, bool alienMode, bool evilMode, int debugType)
{
    // Convert from BGR color to Grayscale
    Mat srcGray;
    cvtColor(srcColor, srcGray, CV_BGR2GRAY);

    // Remove the pixel noise with a good Median filter, before we start detecting edges.
    medianBlur(srcGray, srcGray, 7);

    Size size = srcColor.size();
    Mat mask = Mat(size, CV_8U);
    Mat edges = Mat(size, CV_8U);

    // Generate a nice edge mask, similar to a pencil line drawing.
    Laplacian(srcGray, edges, CV_8U, 5);
    threshold(edges, mask, 80, 255, THRESH_BINARY_INV);
    // Mobile cameras usually have lots of noise, so remove small
    // dots of black noise from the black & white edge mask.
    removePepperNoise(mask);

    if (debugType >= 2) {
        imshow("edges", edges);
        imshow("mask", mask);
    }

    // Do the bilateral filtering at a shrunken scale, since it
    // runs so slowly but doesn't need full resolution for a good effect.
    Size smallSize;
    smallSize.width = size.width/2;
    smallSize.height = size.height/2;
    Mat smallImg = Mat(smallSize, CV_8UC3);
    resize(srcColor, smallImg, smallSize, 0,0, INTER_LINEAR);

    // Perform many iterations of weak bilateral filtering, to enhance the edges
    // while blurring the flat regions, like a cartoon.
    Mat tmp = Mat(smallSize, CV_8UC3);
    int repetitions = 7;        // Repetitions for strong cartoon effect.  
    for (int i=0; i<repetitions; i++) {
        int size = 9;           // Filter size. Has a large effect on speed.
        double sigmaColor = 9;  // Filter color strength.
        double sigmaSpace = 7;  // Positional strength. Effects speed.
        bilateralFilter(smallImg, tmp, size, sigmaColor, sigmaSpace);
        bilateralFilter(tmp, smallImg, size, sigmaColor, sigmaSpace);
        //imshow("smallImg"+ std::to_string(i), smallImg);   // <--- works fine in debug mode
    }
    imshow("smallImg", smallImg);// Testing <---   Works fine in debug mode

    if (alienMode) {
        imshow("smallImg_prealien_call", smallImg);// <--- FAIL!   I get a flat gray box here?

        // Apply an "alien" filter, when given a shrunken image and the full-res edge mask.
        // Detects the color of the pixels in the middle of the image, 
        // then changes region color to green.
        changeFacialSkinColor(smallImg, edges, debugType);
    }

    // Go back to the original scale.
    resize(smallImg, srcColor, size, 0,0, INTER_LINEAR);

    // Clear the output image to black, so that the cartoon line drawings will be black (ie: not drawn).
    memset ...
(more)
edit retag flag offensive close merge delete

Comments

WaitKey() after the imshow() is needed (always)

LorenaGdL gravatar imageLorenaGdL ( 2015-12-21 00:49:35 -0500 )edit

LoranaGdl Not sure I'm getting that... is the WaitKey() before or after the imshow() call?

zipzit gravatar imagezipzit ( 2015-12-21 02:12:53 -0500 )edit

@zipzit after. You need to do something like:

imshow("Window name", img);
waitKey(1);
LorenaGdL gravatar imageLorenaGdL ( 2015-12-21 02:44:14 -0500 )edit