【发布时间】:2020-04-25 23:09:16
【问题描述】:
我有一个外接显示器,我想显示一个与外接显示器具有完全相同高度和宽度的无边框/无框图像。 我开始使用 OpenCV,但在获取无边框图像时遇到了问题。 经过一番搜索,我发现了这个问题:
How to display an image in full screen borderless window in openCV
karlphillip 的回答非常有帮助。 但是,我被困在 A.k. 的问题上。在他/她对答案的评论中提到:
此方法适用于低于显示器分辨率的图像。 如果我有一个分辨率等于我的显示器的图像,它会在底部留下一个灰色条。请问怎么去掉?
此外,图像顶部似乎还有一个 1px 宽的灰色条。 对于我的应用程序来说,每个像素都具有其应有的值是非常重要的,并且没有遗漏任何像素 (或被灰色条覆盖)。图像不得以任何方式扭曲。
我不是在寻找超快的解决方案,但我打算以大约 10Hz 的频率写入图像。 另外,我只在 Windows 上工作,因此解决方案不必是跨平台的。
这是我的代码,我正在使用 VS2019 在 Windows 10 上工作:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <vector>
#include <Windows.h>
int main() {
// Pixels of external monitor, can be different later
size_t N_x = 2560; // 1920
size_t N_y = 1440; // 1080
// My display resolution. Used to shift the OpenCV image
size_t disp_width = 2560;
size_t disp_height = 1440;
// To verify that the image is written correctly I generate a sawtooth image with a 4 pixel period.
byte period = 4;
byte slope = 110 / (period - 1);
std::vector<byte> image_vec (N_y * N_x);
for (size_t i = 0; i < N_y; i++) {
for (size_t j = 0; j < N_x; j++) {
image_vec.at(i * N_x + j) = slope * (j % period);
}
}
cv::Mat image = cv::Mat(N_y, N_x, CV_8UC1);
memcpy(image.data, image_vec.data(), image_vec.size() * sizeof(byte));
// When I use cv::WINDOW_NORMAL instead of cv::WINDOW_FULLSCREEN the image gets distorted in the horizontal direction
cv::namedWindow("Display Window", cv::WINDOW_FULLSCREEN);
imshow("Display Window", image);
// Grab the image and resize it, code taken from karlphillip's answer
HWND win_handle = FindWindowA(0, "Display Window");
if (!win_handle) {
printf("Could not find window\n");
}
// Resize
unsigned int flags = (SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
flags &= ~SWP_NOSIZE;
unsigned int x = 0;
unsigned int y = 0;
unsigned int w = image.cols;
unsigned int h = image.rows;
SetWindowPos(win_handle, HWND_NOTOPMOST, x, y, w, h, flags);
// Borderless
SetWindowLong(win_handle, GWL_STYLE, GetWindowLong(win_handle, GWL_EXSTYLE) | WS_EX_TOPMOST);
ShowWindow(win_handle, SW_SHOW);
cv::waitKey(0);
return EXIT_SUCCESS;
}
【问题讨论】: