0
votes

I have a program that sets up an image to record measurements through user input using the mouse. After showing the image with imshow(), I ask the user if they would like to crop. While waiting for input to the command window, the namedWindow is not responding, and if the user clicks the window before specifying whether or not to crop, the window takes that as the first point to use for cropping.

Is there a way have the command window and namedWindow work in unison?

Constructor for measurment class:

MeasurementClass::MeasurmentClass(){

SetWorkingDirectory();
std::cout << "Before continuing, make sure image files are located in:\n\n  "
          << "\"" + WorkingDirectory + "\"\n"
          << std::endl;

setfilename:
SetFileName();            //uncomment after debug
SetDate();
Image = cv::imread(XrayFileName, CV_LOAD_IMAGE_COLOR);
FinalImage = Image.clone();
if(Image.empty()){
    std::cout << "Image failed to Load. Ensure correct image name and file location.\n"
              << std::endl;
    goto setfilename;
}
GetDesktopResolution();
ResizeImageForDisplay();

FinalDisplayImage = DisplayImage.clone();
cv::namedWindow(WindowName, cv::WINDOW_AUTOSIZE);
cv::imshow(WindowName, DisplayImage);
cv::waitKey(10);

PromptForCrop();

}

PromptFor Crop()

void MeasurementClass::PromptForCrop(void){
    std::cout << "Would you like to crop image? (y/n):  ";

    std::string strCrop;
    std::getline(std::cin, strCrop);
    std::cout << std::endl;
    char Crop = strCrop[0];

    switch(Crop){
        case 'y':
        case '\0':
            CropImage();
        default:
            break;
    }

}

Bonus

It has recently been brought to my attention that goto statements are terrible practice. In this situation, what is a good alternative to the goto statement.

1
OpenCV's namedwindow is for debugging purpose. If you want to do more complicated GUI, you'd better to use something like Qt. If you insist on pure opencv, I suggest you to take a look at this example code: github.com/Itseez/opencv/blob/master/samples/cpp/ffilldemo.cpp - Derman
Yes, don't use goto. They make the code difficult to read and error prone. Here you can use something like: do { ... } while(!Image.empty()) - Miki

1 Answers

0
votes

You can use the cv::waitKey() return value to get user input. In order to do so, please give the image the window focus, not the command window otherwise it will not capture your key:

char c = 'q';
cv::Mat image = cv::imread( "C:/local/opencv30/sources/samples/data/lena.jpg" );

cv::imshow( "image", image );

do
{
    c = cv::waitKey();
}
while ( c != 'y' && c != 'n' );

if ( c == 'y' )
    cout << "yes" << endl;
else if ( c == 'n' )
    cout << "no" << endl;