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;
}