简体   繁体   English

OpenCV,在显示图像时读取用户文本输入(使用waitKey)

[英]OpenCV, reading user text input while an image is being displayed (using waitKey)

I'm a bit stuck trying to get some user text input while using OpenCV. 我在使用OpenCV时尝试获取一些用户文本输入有点困难。

I have developed an iterative program which opens an image, detects colors and displays them using standard cv::imshow() followed by the mandatory waitKey() . 我开发了一个迭代程序,它打开一个图像,检测颜色并使用标准cv::imshow()和强制waitKey()显示它们。 In this state, pressing any key should just process and display the next image. 在此状态下,按任意键应该只处理并显示下一个图像。

The problem is I want to allow the user to optionally click the image displayed and input some text data. 问题是我想让用户可选择点击显示的图像并输入一些文本数据。 I tried to use cin >> mystring but it looks like the waitKey() is blocking the console, so anything i type is displayed but not took by cin as input UNTIL the waitKey ends. 我试图使用cin >> mystring但它看起来像waitKey()阻塞控制台,所以我输入的任何内容都显示但不是由cin作为输入UNTIL,waitKey结束。 It is a problem since some times i would like to get more than one text input per image, so it would be nice to be able to use console and cin normally while waitKey is working. 这是一个问题,因为有时我想为每个图像输入多个文本输入,所以在waitKey工作时能够正常使用控制台和cin会很好。

¿Any idea on how to allow a user to input text while an image is being displayed? ¿如何在显示图像时允许用户输入文字? A solution to my problem would be nice, but any other idea or workaround is also welcomed. 解决我的问题会很好,但欢迎任何其他想法或解决方法。

Ty! 泰!

I have an idea about what you might be doing wrong - it would be great (for others landing here) if you posted your code too. 我知道你可能做错了什么 - 如果你发布你的代码,它会很棒(对于其他人来说)。

Like I suggested, you can set a mouse callback and get the string in that function. 像我建议的那样,您可以设置鼠标回调并获取该函数中的字符串。 This worked fair enough for me: 这对我来说很公平:

std::string s("empty");
void on_mouse_click( int event, int x, int y, int, void* )
{
    if( event == EVENT_LBUTTONDOWN )    //are you missing this?
    {
        std::cout<<"enter string: ";
        std::cin>>s;
    }
}
int main()
{
    :
    :
    cv::imshow( "images", frame );
    cv::waitKey();
    std::cout<<s;
return 1;
}

The last display statement displays "empty" if I just hit a key directly. 如果我直接点击一个键,最后一个显示语句显示“空”。 In case I click the image first, go back to console and type something, go back to image window and then hit a key, I get the string I enter instead. 如果我先点击图像,返回控制台并输入内容,返回图像窗口,然后点击一个键,我得到的是我输入的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM