Skip to content

在 Ubuntu 中使用 Wayland highgui 后端

本教程介绍如何在 Ubuntu 24.04 中使用 Wayland highgui 后端。

Wayland highgui 后端是一项实验性实现。

  • 安装 Ubuntu 24.04。
  • 执行 sudo apt install build-essential git cmake 来编译 OpenCV。
  • 执行 sudo apt install libwayland-dev wayland-protocols libxkbcommon-dev 以启用 Wayland highgui 后端。
  • (可选)sudo apt install ninja-build(或移除 cmake 命令中的 -GNinja 选项)。
  • (可选)sudo apt install libwayland-egl1 以启用 Wayland EGL 库。
Terminal window
mkdir work
cd work
git clone --depth=1 https://github.com/opencv/opencv.git

注意: --depth=1 选项用于限制下载的提交数量。如果你想查看更多的提交历史,请移除该选项。

带 Wayland highgui 后端编译/安装 OpenCV

Section titled “带 Wayland highgui 后端编译/安装 OpenCV”

运行 cmake 并加上 -DWITH_WAYLAND=ON 选项来配置 OpenCV。

Terminal window
cmake -S opencv -B build4-main -DWITH_WAYLAND=ON -GNinja

如果成功,将会显示 Wayland Client/Cursor/Protocols 和 Xkbcommon 的版本。Wayland EGL 是可选项。

--
-- GUI: Wayland
-- Wayland: (Experimental) YES
-- Wayland Client: YES (ver 1.22.0)
-- Wayland Cursor: YES (ver 1.22.0)
-- Wayland Protocols: YES (ver 1.34)
-- Xkbcommon: YES (ver 1.6.0)
-- Wayland EGL(Option): YES (ver 18.1.0)
-- GTK+: NO
-- VTK support: NO

运行 cmake --build 进行编译,再运行 sudo cmake --install 将其安装到系统中。

Terminal window
cmake --build build4-main
sudo cmake --install build4-main
sudo ldconfig

用于尝试 Wayland highgui 后端的简单应用程序

Section titled “用于尝试 Wayland highgui 后端的简单应用程序”

尝试运行下面的代码,你将看到 currentUIFramework() 的名称,以及一个使用 Wayland highgui 后端显示 OpenCV logo 的窗口。

Terminal window
// g++ main.cpp -o a.out -I /usr/local/include/opencv4 -lopencv_core -lopencv_highgui -lopencv_imgcodecs
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <string>
int main(void)
{
std::cout << "cv::currentUIFramework() returns " << cv::currentUIFramework() << std::endl;
cv::Mat src;
src = cv::imread("opencv-logo.png");
cv::namedWindow("src");
int key = 0;
do
{
cv::imshow("src", src );
key = cv::waitKey(50);
} while( key != 'q' );
return 0;
}