基于C++的imwrite
时间: 2024-05-10 10:17:20 浏览: 94
imwrite是OpenCV库中用于将图像保存为文件的函数,可以用于C++,以下是一个基本的使用示例:
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat image = imread("path/to/image.jpg");
if(image.empty())
{
std::cout << "Failed to read image file." << std::endl;
return -1;
}
bool success = imwrite("path/to/output.jpg", image);
if(!success)
{
std::cout << "Failed to save image file." << std::endl;
return -1;
}
return 0;
}
```
在以上示例中,首先使用imread函数读取图像文件,然后使用imwrite函数将图像保存到指定路径下。如果imread函数失败,返回的图像为空,需要做错误处理。如果imwrite函数失败,会返回false,也需要做错误处理。
相关问题
基于C++的TNN分割代码
以下是一个基于C++的TNN分割代码的示例:
```
#include <tnn/core/macro.h>
#include <tnn/utils/blob_converter.h>
#include <tnn/utils/dims_utils.h>
#include <tnn/utils/omp_utils.h>
#include <tnn/utils/cpu_utils.h>
#include <tnn/device/cpu/cpu_device.h>
#include <tnn/interpreter/default_model_interpreter.h>
#include <tnn/interpreter/layer_resource_generator.h>
#include <tnn/network/tensorrt/tensorrt_network.h>
#include <tnn/utils/string_utils_inner.h>
#include <tnn/utils/string_utils.h>
#include <tnn/utils/mat_utils.h>
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace TNN_NS;
using namespace cv;
int main(int argc, char** argv) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " proto_file model_file" << std::endl;
return 1;
}
// create network
auto proto_content = fdLoadFile(argv[1]);
auto model_content = fdLoadFile(argv[2]);
auto interpreter = std::make_shared<DefaultModelInterpreter>();
auto network = interpreter->CreateNetwork(proto_content, model_content);
CHECK_TNN_OK(network->SetCpuNumThreads(4));
CHECK_TNN_OK(network->SetDeviceType(DeviceType::DEVICE_ARM));
// get input info
auto input_info_map = network->GetInputInfo();
auto input_name = input_info_map.begin()->first;
auto input_info = input_info_map.begin()->second;
auto input_dims = input_info->shape;
// prepare input blob
auto input_data_type = input_info->data_type;
auto input_blob = std::make_shared<Blob>(input_data_type, input_dims);
auto input_mat = cv::imread("input.jpg");
auto input_mat_resize = Mat(input_dims[2], input_dims[3], CV_32FC3);
cv::resize(input_mat, input_mat_resize, input_mat_resize.size(), 0, 0, cv::INTER_LINEAR);
auto input_data = input_blob->GetHandle().base;
ConvertFromMat(input_mat_resize, input_data, input_dims);
// forward
std::vector<std::shared_ptr<Blob>> input_blobs = {input_blob};
auto status = network->Forward(input_blobs);
if (status != TNN_OK) {
std::cerr << "Forward Failed: " << TNN_NS::StatusUtils::StatusDescription(status) << std::endl;
return 2;
}
// get output info
auto output_info_map = network->GetOutputInfo();
auto output_name = output_info_map.begin()->first;
auto output_info = output_info_map.begin()->second;
auto output_dims = output_info->shape;
// prepare output blob
auto output_data_type = output_info->data_type;
auto output_blob = std::make_shared<Blob>(output_data_type, output_dims);
auto output_data = output_blob->GetHandle().base;
// get output blob data
auto output_size = DimsVectorUtils::Count(output_dims);
auto output_data_size = DataTypeUtils::GetBytesSize(output_data_type) * output_size;
std::memcpy(output_data, output_info->resource->data, output_data_size);
// postprocess
std::vector<float> output_data_vec(output_size);
ConvertFromRawData(output_data, output_data_vec.data(), output_data_type, output_size);
auto output_mat = Mat(output_dims[2], output_dims[3], CV_32FC1, output_data_vec.data());
cv::threshold(output_mat, output_mat, 0.5, 255, cv::THRESH_BINARY);
// save output
imwrite("output.jpg", output_mat);
return 0;
}
```
该示例中,使用一个给定的TNN模型进行图像分割。需要提供模型的 `.tnnproto` 和 `.tnnmodel` 文件。然后使用 OpenCV 加载输入图像,将其转换为输入 Blob,并在网络上执行正向传递。最后,将输出 Blob 转换为 OpenCV Mat,进行二值化并保存为输出图像。
linux环境下 基于C++的TNN分割代码,并将分割结果保持成图片
以下是基于C++的TNN分割代码,并将分割结果保存为图片:
```cpp
#include "tnn/core/common.hpp"
#include "tnn/core/context.hpp"
#include "tnn/core/profile.h"
#include "tnn/device/cpu/cpu_device.h"
#include "tnn/device/cpu/cpu_context.h"
#include "tnn/utils/blob_converter.h"
#include "tnn/utils/dims_vector_utils.h"
#include "tnn/utils/naive_compute.h"
#include "tnn/utils/omp_utils.h"
#include "tnn/utils/cpu_utils.h"
#include "tnn/utils/omp_utils.h"
#include "tnn/network/tensorrt/tensorrt_network.h"
#include "tnn/network/tensorrt/tensorrt_common.h"
#include "opencv2/opencv.hpp"
using namespace TNN_NS;
int main(int argc, char** argv) {
// 定义输入图像大小
int input_width = 224;
int input_height = 224;
// 创建网络实例
auto proto_content = fdLoadFile("model.tnnproto");
auto model_content = fdLoadFile("model.tnnmodel");
auto network = std::make_shared<TensorRTNetwork>();
auto status = network->LoadFromBuffer(proto_content, model_content);
CHECK_TNN_OK(status);
// 获取输入输出节点
auto input_name = "input";
auto output_name = "output";
auto input_dims = network->GetInputShape(input_name);
auto output_dims = network->GetOutputShape(output_name);
// 创建输入输出blob
std::shared_ptr<RawBuffer> input_buffer(new RawBuffer(input_dims[1] * input_dims[2] * input_dims[3] * sizeof(float)));
std::shared_ptr<Mat> input_mat = std::make_shared<Mat>(input_dims, input_buffer);
std::shared_ptr<RawBuffer> output_buffer(new RawBuffer(output_dims[1] * output_dims[2] * output_dims[3] * sizeof(float)));
std::shared_ptr<Mat> output_mat = std::make_shared<Mat>(output_dims, output_buffer);
// 创建CPU设备和上下文
std::shared_ptr<CpuDevice> cpu_device(new CpuDevice(DEVICE_NAIVE));
CpuContext cpu_context;
cpu_context.device = cpu_device.get();
// 初始化输入数据
MatUtils::InitRandom(*input_mat);
auto input_data = input_mat->GetData();
// 执行推理
status = network->Forward({ input_mat }, { output_mat }, &cpu_context);
CHECK_TNN_OK(status);
// 输出结果
auto output_data = output_mat->GetData();
cv::Mat seg_map(output_dims[2], output_dims[3], CV_32FC1, output_data);
cv::Mat seg_map_8u;
seg_map.convertTo(seg_map_8u, CV_8UC1, 255.0);
// 保存结果为图片
cv::imwrite("seg_result.png", seg_map_8u);
return 0;
}
```
注意需要替换 `model.tnnproto` 和 `model.tnnmodel` 为你自己的模型文件路径,以及根据你的模型定义输入和输出节点的名称。此外,需要安装 OpenCV 库来读写图片。
阅读全文