如何获取使用 OpenCV 创建的窗口位置 (x,y)?
- 我可以创建一个命名窗口:
namedWindow("my window");
- 我可以移动这个窗口:
moveWindow("my window", x, y);
但是我怎样才能得到这个窗口当前位置的坐标呢?
还有一个函数void loadWindowParameters("my window");
,可以写成:
loads size, location, flags, trackbars value, zoom and panning location of the window window_name
但是它会将这些参数加载到到哪里吗?此函数的声明不返回任何内容 - 仅获取窗口名称并返回 void
。
最佳答案
直到 the feature request对于这个功能已经完成,如果你需要这个功能并且等不及了,你可以自己实现它。您将需要 opencv 源代码,然后您必须编辑一些 opencv 文件并重建 opencv 的一部分。 我遵循了 moveWindow() 的源代码作为模型。
这就是我所做的:
将此函数添加到 opencv/sources/modules/highgui/source/window_w32.cpp(我将其添加到 cvMoveWindow 定义下方):
CV_IMPL void cvGetWindowRect( const char* name, int &x, int &y, int &width, int &height)
{
CV_FUNCNAME( "cvGetWindowRect" );
__BEGIN__;
CvWindow* window;
RECT rect;
if( !name )
CV_ERROR( CV_StsNullPtr, "NULL name" );
window = icvFindWindowByName(name);
if(!window)
EXIT;
GetWindowRect( window->frame, &rect );
x = rect.left;
y = rect.top;
width = rect.right - rect.left;
height = rect.bottom - rect.top;
__END__;
}
并在 opencv/sources/modules/highgui/include/opencv2/highgui_c.h 中添加其声明:
CVAPI(void) cvGetWindowRect( const char* name, int &x, int &y, int &width, int &height);
仅此一项就可以让您使用 C/C++ 中的 cvGetWindowRect 来获取窗口矩形。但是如果你想使用 C++ 接口(interface)或者 python 接口(interface)(像我一样)你可以再编辑两个文件:
添加到opencv/sources/modules/highgui/source/window.cpp这个函数:
void cv::getWindowRect( const String& winname, CV_OUT int &x, CV_OUT int &y, CV_OUT int &width, CV_OUT int &height)
{
cvGetWindowRect(winname.c_str(), x, y, width, height);
}
并在 opencv/sources/modules/highgui/include/opencv2/highgui.hpp 中添加它的声明:
CV_EXPORTS_W void getWindowRect( const String& winname, CV_OUT int &x, CV_OUT int &y, CV_OUT int &width, CV_OUT int &height);
然后您必须重建 opencv_highgui 项目(我为带有 Visual Studio 2015 的 Windows 执行此操作)。如果您需要 python 绑定(bind),那么也重建 opencv_python3 项目。在构建 python 绑定(bind)时,需要 CV_EXPORTS_W 和 CV_OUT 宏来公开函数并识别输出参数。从 python 你会得到一个 4 元组作为返回值,->例如:
>>> cv2.getWindowRect("my window")
(1024, 0, 817, 639)
对于 python 绑定(bind),您必须将新的 cv2.cp35-win_amd64.pyd 和 opencv_highgui300.dll 复制到 PythonEnv\Lib\site -包。
关于opencv - 如何获得使用 OpenCV 创建的窗口位置 (x,y)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282105/