image doesn't display

asked 2018-01-20 11:55:21 -0600

djtommye gravatar image

I've got an app that I'm developing in Windows using MFC.

When I display an image directory from the main dialog, it shows fine - with the crosshair cursor over the image. When I display an image from a thread, it shows the window, but no image. The cursor in this case is a "Wait" cursor.

I've debugged and confirmed that both image paths are the same, and it's running through the same code.

Any thoughts?

Here's the code. The "CreateImageWindow" function determines the correct size (windowed or full screen) based on user preference, and calls namedWindow("Name", WINDOW_NORMAL | CV_WINDOW_KEEPRATIO);

    // Make sure we have a window
    CreateImageWindow();

    // Convert our string to ASCII (required by cv library)
    CT2A ascii(pszImage);

    // Read the file
    Mat imgSrc = imread(ascii.m_psz); // Image from the file
    if (!imgSrc.data) {               // If the read failed - bail out
        TRACE("DisplayImage: Failed to read file %s : %ld\n", pszImage, GetLastError());
        return FALSE;
    }

    // It's probably safe to assume that the image we just loaded isn't
    //  the same size as the display - so we'll always resize it.

    Size    resizeDims;
    float   iScreenRatio = (float)gWindowRect.Width() / (float)gWindowRect.Height();
    float   iImageRatio = (float)imgSrc.cols / (float)imgSrc.rows;
    int     iLeftRightBorder = 0;
    int     iTopBottomBorder = 0;

    if (iScreenRatio > iImageRatio) {   // Image will scale to display height, smaller width
        resizeDims.height = gWindowRect.Height();
        resizeDims.width = imgSrc.cols * gWindowRect.Height() / imgSrc.rows;
        iLeftRightBorder = ((gWindowRect.Width() - resizeDims.width) / 2) + 1;
    }
    else { // Image will scale to display width, smaller height
        resizeDims.height = imgSrc.rows * gWindowRect.Width() / imgSrc.cols;
        resizeDims.width = gWindowRect.Width();
        iTopBottomBorder = ((gWindowRect.Height() - resizeDims.height) / 2) + 1;
    }

    Mat imgResized;
    resize(imgSrc, imgResized, resizeDims, 0, 0, INTER_LINEAR);

    Mat imgDst; // Target image with the border and fill

    // Copy the file image to the destination, and fill the remainder (border) with black
    copyMakeBorder(imgResized, imgDst, iTopBottomBorder, iTopBottomBorder, iLeftRightBorder, iLeftRightBorder, BORDER_CONSTANT, Scalar(0, 0, 0));

    // Show the photo 
    imshow(SCREEN_TITLE, imgDst);
edit retag flag offensive close merge delete

Comments

1

if you want to use imshow(), there has to be a waitKey(someMillis) following it, else it won't display anything

berak gravatar imageberak ( 2018-01-20 12:18:18 -0600 )edit

Hmm - but that blocks the thread. This is like a slideshow, so I'd like to display an image in the window, then some seconds later, update the window with a new image. Is there some other function than imShow() that I should be using?

djtommye gravatar imagedjtommye ( 2018-01-20 15:08:11 -0600 )edit

@djtommye just use a waitKey(1), which will hardly be noticable. The thing is that the function calls the redraw function of all the windows :)

StevenPuttemans gravatar imageStevenPuttemans ( 2018-01-21 09:18:22 -0600 )edit