当前位置: 首页 > news >正文

opencv(C++)图像的读写、翻转、绘制、鼠标事件

基础功能类封装

#pragma once

#include <opencv2/opencv.hpp>
#include <functional>

#define SHOW_FOREVER 0

#define SHOW_WINDOW(title, image)\
    cv::namedWindow(title, cv::WINDOW_AUTOSIZE);\
    cv::imshow(title, image);

class Base_function_image
{
public:
    static Base_function_image* Instance();
    ~Base_function_image();

    bool load_image(const char* pImage_path, cv::Mat& image);
    bool save_image(const char* pImage_path, cv::Mat& image);
    void show_image(const char* pTitle, cv::Mat& image, int delay_ms);

    void show_image_with_func(const char* pImage_path, const char* pTitle, cv::MouseCallback func);

private:
    Base_function_image();  
};

#define BASE_FUNC Base_function_image::Instance()
#include "base_function_image.h"
#include <future>

namespace{
#define IS_NULLPTR(ptr, result)\
    if(!ptr){return result;}
}

Base_function_image* Base_function_image::Instance()
{
    static Base_function_image instance;
    return &instance;
}

Base_function_image::Base_function_image(){}

Base_function_image::~Base_function_image(){}

bool Base_function_image::load_image(const char* pImage_path, cv::Mat& image)
{
    IS_NULLPTR(pImage_path, false);
    std::cout<<"[Base_function_image::load_image] pImage_path:"<<pImage_path<<std::endl;
    image = cv::imread(pImage_path, cv::IMREAD_COLOR);
    if (image.empty()) {
        std::cerr << "无法打开或找到图片: " << pImage_path << std::endl;
        return false;
    }
    return true;
}

bool Base_function_image::save_image(const char* pImage_path, cv::Mat& image)
{
    IS_NULLPTR(pImage_path, false);
    std::cout<<"[Base_function_image::save_image] pImage_path:"<<pImage_path<<std::endl;
    cv::imwrite(pImage_path, image);
    return true;
}

void Base_function_image::show_image(const char* pTitle, cv::Mat& image, int delay_ms)
{
    IS_NULLPTR(pTitle, ;);
    
    std::cout<<"[Base_function_image::show_image] pTitle:"<<pTitle<<std::endl;
    cv::namedWindow(pTitle, cv::WINDOW_AUTOSIZE);
    cv::imshow(pTitle, image);
    cv::waitKey(delay_ms);
}

void Base_function_image::show_image_with_func(const char *pImage_path, const char *pTitle, cv::MouseCallback func)
{
    IS_NULLPTR(pTitle, ;);
    IS_NULLPTR(pImage_path, ;);
    cv::Mat image;
    
    load_image(pImage_path, image);
    cv::namedWindow(pTitle);
    cv::setMouseCallback(pTitle, func, &image);

    char key = 0;
    for(cv::imshow(pTitle, image); 27 != key;)
        key = cv::waitKey(10);
}

原始图片

在这里插入图片描述

鼠标事件

#include "base_function_image.h"
#include <iostream>

#define IMAGE_1 "1.jpeg"

void onMouse(int event, int x, int y, int flags, void* userdata) 
{
    
    if (cv::EVENT_LBUTTONDOWN == event) 
    { 
        std::cout << "左键点击位置: (" << x << ", " << y << ")" << std::endl;
    }
}

int main() 
{
    BASE_FUNC->show_image_with_func(IMAGE_1, "test", onMouse);
    return 0;
}

绘制

#include "base_function_image.h"
#include <iostream>

#define IMAGE_1 "1.jpeg"

int main() 
{
    cv::Mat image;
    BASE_FUNC->load_image(IMAGE_1, image);
    cv::circle(image, cv::Point(122, 476), 
        65,   // radius 
        0,    // color
        3);   // thickness

    cv::putText(image, "hello world", cv::Point(40,200),
        cv::FONT_HERSHEY_PLAIN,  // font type
        2.0,                     // font scale
        255,                     // text color
        2);                      // text thickness
    BASE_FUNC->save_image("write1.jpeg", image);
    return 0;
}

在这里插入图片描述

翻转

#include "base_function_image.h"
#include <iostream>

#define IMAGE_1 "1.jpeg"

int main() 
{
    auto flip_image =
    [&](const char* pSrc_img, const char* pNew_name, int flipCode)
    {
        cv::Mat image_src;
        if(!BASE_FUNC->load_image(pSrc_img, image_src))
            return;

        cv::Mat image_res;
        cv::flip(image_src, image_res, flipCode);
        BASE_FUNC->show_image(pNew_name, image_res, SHOW_FOREVER);
    };

    flip_image(IMAGE_1, "code_1", 1);
    flip_image(IMAGE_1, "code_2", 2);
    flip_image(IMAGE_1, "code_0", 0);
    flip_image(IMAGE_1, "code_-1", -1);
    return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 源码分析之Leaflet中control模块Control基类实现原理
  • Vue2 通过 Object.defineProperty 对哪些数组进行了特殊处理?
  • C语言中的内存管理:掌握动态分配的技巧
  • 雪花算法生成的主键存在哪些问题,为什么不能使用自增ID或者UUID做MySQL的主键
  • git 对比两种优化方法的性能
  • MySQL主从复制(二)
  • Go语言入门指南:从语法基础到核心特性解析
  • 【C++】mapset使用与实战 OJ题
  • ABAP RANGE表 OPTION 运算符 SIGN
  • 无人机数据链技术及运行方式详解!
  • python生成并绘制各种类型声音噪声
  • xfreerdp 的使用
  • Spring的 init-method, @PostConstruct, InitializingBean 对比
  • 【鸿蒙5.0】两个数组,点击事件两个数组数据进行了双向数据交换,双向绑定的原理是什么?
  • JVM——模型分析、回收机制
  • 学透Spring Boot — 007. 加载外部配置
  • 【蓝桥杯14天冲刺课题单】Day 8
  • MQTT 服务器(emqx)搭建及使用(二)
  • 【原创】使用Golang和wails开发桌面程序初探
  • 基于CT成像的肿瘤图像分类:方法与实现
  • 神舟十九号载人飞船因东风着陆场气象原因推迟返回
  • 当隐身13年的北小京决定公开身份 ,专业戏剧评论依然稀缺
  • 上海明天起进入“升温通道”,五一假期冲刺33℃
  • 上海市委常委会传达学习总书记重要讲话精神,研究张江科学城建设等事项
  • 张译、惠英红分获第二十届中国电影华表奖优秀男、女演员奖
  • 图像编辑新增一款开源模型,阶跃星辰发布Step1X-Edit