从0开始的Opencv之旅(到尝试构建一个图像编辑器):0,opencv demo ...

打印 上一主题 下一主题

主题 1014|帖子 1014|积分 3042

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
关于opencv的下载,参考笔者的博客OpenCV4.9.0 + 扩展 + WITH_QT(Qt6)模块编译教程(Windows)_opencv4.9 qt-CSDN博客
大伙都知道,opencv是一个图像处理的一个重要的库。在 OpenCV 4 中,我们有多个模块。每个模块负责不同的图像处理范畴或方法。先不发急仔细一头扎进我们的图像处理里去,而是从一个大概的流程举行把握。
别担心,您几乎总是会使用:


  • 焦点部分,因为这里定义了库的基本构建块
  • imgcodecs 模块,它提供读取和写入的函数
  • highgui 模块,因为它包含在窗口中显示图像的函数
通过声明 using namespace cv;,在下面,可以访问库函数而无需明确说明定名空间。但是笔者的建议是,不要头文件中这样干!
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/imgcodecs.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <iostream>
  5. using namespace cv;
复制代码
笔者稍微魔改了一下opencv的样例代码,请看:
  1. /*
  2.    This is for beginners of the OpenCV library.
  3.    If you finished your work in installing the opencv library
  4.    You can try building and running the example
  5.    Author: Charliechen114514
  6.    Date:   2024/12/30
  7. */
  8. #include "example_common.h"
  9. #include <iostream>
  10. #include <opencv2/core.hpp>
  11. #include <opencv2/highgui.hpp>
  12. #include <opencv2/imgcodecs.hpp>
  13. using namespace cv;
  14. std::string make_up_example_path() {
  15.    /* don't worry,  EXAMPLE_COMMON_PATH points to the base dir of the
  16.     * example!*/
  17.    return std::string(EXAMPLE_COMMON_PATH) + "/beginners/image.jpg";
  18. }
  19. int main() {
  20.    auto image_path = make_up_example_path();
  21.    Mat  img        = imread(image_path, IMREAD_COLOR);
  22.    if (img.empty()) {
  23.        std::cout << "Could not read the image: " << image_path << std::endl;
  24.        return 1;
  25.    }
  26.    imshow("Display window", img);
  27.    std::cout << "Press 's' to save the image" << std::endl;
  28.    int k = waitKey(0);  // Wait for a keystroke in the window
  29.    if (k == 's') {
  30.        bool result = imwrite("You success!.png", img);
  31.        if (result) {
  32.            std::cout << "Image saved successfully, see your building "
  33.                         "directory to check the result"
  34.                      << std::endl;
  35.        } else {
  36.            std::cerr << "Could not save the image" << std::endl;
  37.        }
  38.    }
  39.    return 0;
  40. }
复制代码
如今,让我们分析一下主代码。make_up_example_path函数只是简单的找到了我们的example源码中的image图像。这一点您可以手动的打印一下路径测试!
首先,我们从 OpenCV 样本中读取图像"image.jpg”。为此,调用 cv::imread 函数使用第一个参数指定的文件路径加载图像。第二个参数是可选的,指定我们想要的图像格式。这可能是:


  • IMREAD_COLOR 以 BGR 8 位格式加载图像。这是此处使用的默认值。
  • IMREAD_UNCHANGED 按原样加载图像(包括 alpha 通道,如果存在)
  • IMREAD_GRAYSCALE 将图像加载为强度图像
读取后,图像数据将存储在 cv::Mat 对象中。OpenCV 支持 Windows 位图 (bmp)、便携式图像格式 (pbm、pgm、ppm) 和 Sun 光栅 (sr、ras) 等图像格式。借助插件(如果您自行构建库,则需要指定使用它们,只管我们默认提供的软件包中存在这些插件),您还可以加载图像格式,如 JPEG (jpeg、jpg、jpe)、JPEG 2000 (jp2 - 在 CMake 中代号为 Jasper)、TIFF 文件 (tiff、tif) 和便携式网络图形 (png)。此外,OpenEXR 也是一种可能性。
之后,将执行检查,以确定图像是否已正确加载。
  1.    auto image_path = make_up_example_path();
  2.    Mat  img        = imread(image_path, IMREAD_COLOR);
  3.    if (img.empty()) {
  4.        std::cout << "Could not read the image: " << image_path << std::endl;
  5.        return 1;
  6.    }
复制代码
然后,使用对 cv::imshow 函数的调用显示图像。第一个参数是窗口的标题,第二个参数是将显示的 cv::Mat 对象。因为我们希望窗口一直显示,直到用户按下某个键(否则步伐会很快结束),以是我们使用 cv::waitKey 函数,该函数的唯一参数就是等候用户输入的时间(以毫秒为单位)。零表示永世等候。返回值是按下的键。
  1.     imshow("Display window", img);
  2.    std::cout << "Press 's' to save the image" << std::endl;
  3.    int k = waitKey(0);  // Wait for a keystroke in the window
复制代码
最后,如果按下的键是“s”键,则将图像写入文件。为此,将调用 cv::imwrite 函数,该函数以文件路径和 cv::Mat 对象作为参数。值得一提的是:我们可以检查返回值来查察我们的生存是否成功。
  1.    if (k == 's') {
  2.        bool result = imwrite("You success!.png", img);
  3.        if (result) {
  4.            std::cout << "Image saved successfully, see your building "
  5.                         "directory to check the result"
  6.                      << std::endl;
  7.        } else {
  8.            std::cerr << "Could not save the image" << std::endl;
  9.        }
  10.    }
复制代码
样例代码在:examples/beginners,笔者的工作将发布在opencv上,请看Charliechen114514/CCPixelCraft: A PixelLevel Image Convertor And Processor. Also Provide Opencv4 Tourial Usage... (github.com)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

兜兜零元

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表