为什么 window 名称在创建时更改 window 大小?

Why window name changes window size on creation?

我使用 OpenCV 库创建了一个 window,我在其中显示文件夹中的图像。 (通过单击 space 栏更改图像)。

在 window 名称中,我想显示已显示的图像数,例如“20 张图片中的 10 张”。

问题出现了,当我点击 space 条并显示带有另一张图片的新 window 时,window 的大小发生了变化(有时 window 移动到屏幕上的另一个位置)。如何使 windows 的大小稳定?我希望每次显示新图像时创建的所有 windows 都具有相同的大小和位置。

下面是我使用的代码:

fullWindowName = SmString(windowName) << " " << i <<  " Out of " << filePaths.size();
cv::namedWindow(cv::String(fullWindowName), WINDOW_NORMAL | WINDOW_KEEPRATIO);
cv::imshow(cv::String(fullWindowName), srcDisp);
cv::setMouseCallback(cv::String(fullWindowName), on_Mouse, &srcDisp);

您只需要 resizeWindow and moveWindow 即可实现。

下面是一些示例代码:

#include <opencv.hpp>

int main()
{
    std::vector<cv::Mat> images;
    /* Feed some images here ... */

    int width = 640;
    int height = 480;
    int x = 100;
    int y = 100;
    for (int i = 0; i < images.size(); i++) {
        cv::destroyAllWindows();
        std::stringstream ss;
        ss << i + 1 << " out of " << images.size() << " images";
        std::string windowName = ss.str();
        cv::namedWindow(windowName, cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
        cv::resizeWindow(windowName, width, height);
        cv::moveWindow(windowName, x, y);
        cv::imshow(windowName, images[i]);
        cv::waitKey(0);
    }

    return 0;
}

cv::WINDOW_KEEPRATIO 对我不起作用,可能是因为缺少 Qt 后端,请参见。文档:

Qt backend supports additional flags:

WINDOW_NORMAL or WINDOW_AUTOSIZE: WINDOW_NORMAL enables you to resize the window, whereas WINDOW_AUTOSIZE adjusts automatically the window size to fit the displayed image (see imshow ), and you cannot change the window size manually.

WINDOW_FREERATIO or WINDOW_KEEPRATIO: WINDOW_FREERATIO adjusts the image with no respect to its ratio, whereas WINDOW_KEEPRATIO keeps the image ratio.

WINDOW_GUI_NORMAL or WINDOW_GUI_EXPANDED: WINDOW_GUI_NORMAL is the old way to draw the window without statusbar and toolbar, whereas WINDOW_GUI_EXPANDED is a new enhanced GUI. By default, flags == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED

除此之外,window 大小和位置对于所有显示的图像都是不变的。