Official OCV 4 replacement for cvGetWindowHandle

What is the official OCV 4 replacement for cvGetWindowHandle? The only reference I can find is a “contributor” to OpenCV who says to use

getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0

which, of course does not make sense since 1) getWindowProperty returns a double and not a HWND and >=0 would be boolean.

So what is the replacement for OCV 4.x?

Ed

what’s that used for in your code?

in c++ you can use it by adding #include <opencv2/highgui/highgui_c.h>

I do have <opencv2/highgui/highgui_c.h> included but cvGetWindowHandle was crashing in OCV 4 if the window already existed previously. Following the debug on subsequent call it was saying that the window did not exist. This all worked in OCV 3.4

namedWindow(“video”, CV_WINDOW_KEEPRATIO);
HWND hw = (HWND)cvGetWindowHandle(“video”);

What I am finding in OCV 4.5 is that it works the first time the “video” window is created but crashes if we are coming into this OpenFile() procedure another time to open another video.

I could not find an OCV way of discovering if a namedWindow already existed and I found that destroyWindow crashed if the window did not already exist.

Using the Windows API

hw = (HWND)FindWindow(NULL, _T(“video”));

I saw that it gave me a handle if the namedWindow was just created and had not existed before but returned NULL if that window already existed.

So what I have done so far was to check with Windows first this way:

HWND hw = (HWND)FindWindow(NULL, _T(“video”));
if (!hw == NULL) {
hw = (HWND)cvGetWindowHandle(“video”);
}
else {
cv::destroyWindow(“video”);
namedWindow(“video”, CV_WINDOW_KEEPRATIO);
hw = (HWND)cvGetWindowHandle(“video”);
}

This works but it seems awfully clumsy. I was hoping for an OCV way of 1) deciding if a namedWindow already existed and 2) an OCV 4.x way of getting a handle to a namedWindow.

I would have to look at all of the references but at least one reason is to put a specific icon on that window as in

DWORD dwresult;
dwresult = ::SetClassLong(hw,
GCL_HCURSOR,
(LONG)LoadCursor(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDC_ARROW)));

i was expecting FindWindow and cvGetWindowHandle give the same result but gives different results.

i think you should use FindWindow and leave using current cvGetWindowHandle

#include "opencv2/highgui.hpp"
#include <iostream>
#include <Windows.h>

using namespace cv;
using namespace std;


int main()
{
    Mat test_mat = Mat(240,320,CV_8UC3,Scalar(100,255,100));
    cv::imshow("test", test_mat);
    waitKey();
    HWND hw = FindWindow(NULL, "test");
    SetWindowText(hw, "changed");
    waitKey();
    return 0;
}

I just noticed the redundancy in getting a Window handle and, if it exists, getting a OCV handle. I was getting ready to check the same thing that are they the same or different. I have a feeling that just using the Windows handle is the way to go.

I just noticed a strange thing with this. Once I get a handle to the namedWindow I use it go set the cursor for that window with

dwresult = ::SetClassLong(hwCamera,
GCL_HCURSOR,
(LONG)LoadCursor(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDC_ARROW_NW)));

Which is a Win-API and it works with the handle returned from the OpenCV call

hwCamera = (HWND)cvGetWindowHandle(“cameraWindow”);

However, if I instead get the handle with

hwCamera = (HWND)FindWindow(NULL, _T(“cameraWindow”));

which is itself a Win-API call, the cursors never get changed as if that handle is not recognized or, more correctly, not the correct handle. So, apparently, the Windows FindWindow is not getting the correct handle to a OCV namedWindow.

I thought this was interesting.