#include <iostream>
#include <fstream>
#include <numeric>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
//#include <cuda_provider_factory.h> ///nvidia-cuda加速
#include <onnxruntime_cxx_api.h>
using namespace cv;
using namespace std;
using namespace Ort;
class CodeFormer
{
public:
CodeFormer(string modelpath);
Mat detect(Mat cv_image);
private:
void preprocess(Mat srcimg);
vector<float> input_image_;
vector<double> input2_tensor;
int inpWidth;
int inpHeight;
int outWidth;
int outHeight;
float min_max[2] = { -1,1 };
//存储初始化获得的可执行网络
Env env = Env(ORT_LOGGING_LEVEL_ERROR, "CodeFormer");
Ort::Session* ort_session = nullptr;
SessionOptions sessionOptions = SessionOptions();
vector<char*> input_names;
vector<char*> output_names;
vector<vector<int64_t>> input_node_dims; // >=1 outputs
vector<vector<int64_t>> output_node_dims; // >=1 outputs
};
CodeFormer::CodeFormer(string model_path)
{
//OrtStatus* status = OrtSessionOptionsAppendExecutionProvider_CUDA(sessionOptions, 0); ///nvidia-cuda加速
sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
std::wstring widestr = std::wstring(model_path.begin(), model_path.end()); ///如果在windows系统就这么写
ort_session = new Session(env, widestr.c_str(), sessionOptions); ///如果在windows系统就这么写
///ort_session = new Session(env, model_path.c_str(), sessionOptions); ///如果在linux系统,就这么写
size_t numInputNodes = ort_session->GetInputCount();
size_t numOutputNodes = ort_session->GetOutputCount();
AllocatorWithDefaultOptions allocator;
for (int i = 0; i < numInputNodes; i++)
{
input_names.push_back(ort_session->GetInputName(i, allocator));
Ort::TypeInfo input_type_info = ort_session->GetInputTypeInfo(i);
auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
auto input_dims = input_tensor_info.GetShape();
input_node_dims.push_back(input_dims);
}
for (int i = 0; i < numOutputNodes; i++)
{
output_names.push_back(ort_session->GetOutputName(i, allocator));
Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i);
auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
auto output_dims = output_tensor_info.GetShape();
output_node_dims.push_back(output_dims);
}
this->inpHeight = input_node_dims[0][2];
this->inpWidth = input_node_dims[0][3];
this->outHeight = output_node_dims[0][2];
this->outWidth = output_node_dims[0][3];
input2_tensor.push_back(0.5);
}
void CodeFormer::preprocess(Mat srcimg)
{
Mat dstimg;
cvtColor(srcimg, dstimg, COLOR_BGR2RGB);
resize(dstimg, dstimg, Size(this->inpWidth, this->inpHeight), INTER_LINEAR);
this->input_image_.resize(this->inpWidth * this->inpHeight * dstimg.channels());
int k = 0;
for (int c = 0; c < 3; c++)
{
for (int i = 0; i < this->inpHeight; i++)
{
for (int j = 0; j < this->inpWidth; j++)
{
float pix = dstimg.ptr<uchar>(i)[j * 3 + c];
this->input_image_[k] = (pix / 255.0 - 0.5) / 0.5;
k++;
}
}
}
}
Mat CodeFormer::detect(Mat srcimg)
{
int im_h = srcimg.rows;
int im_w = srcimg.cols;
this->preprocess(srcimg);
array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth };
vector<int64_t> input2_shape_ = { 1 };
auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
vector<Value> ort_inputs;
ort_inputs.push_back(Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size()));
ort_inputs.push_back(Value::CreateTensor<double>(allocator_info, input2_tensor.data(), input2_tensor.size(), input2_shape_.data(), input2_shape_.size()));
vector<Value> ort_outputs = ort_session->Run(RunOptions{ nullptr }, input_names.data(), ort_inputs.data(), ort_inputs.size(), output_names.data(), output_names.size());
////post_process
float* pred = ort_outputs[0].GetTensorMutableData<float>();
//////Mat mask(outHeight, outWidth, CV_32FC3, pred); /////经过试验,直接这样赋值,是不行的
const unsigned int channel_step = outHeight * outWidth;
vector<Mat> channel_mats;
Mat rmat(outHeight, outWidth, CV_32FC1, pred); // R
Mat gmat(outHeight, outWidth, CV_32FC1, pred + channel_step); // G
Mat bmat(outHeight, outWidth, CV_32FC1, pred + 2 * channel_step); // B
channel_mats.push_back(rmat);
channel_mats.push_back(gmat);
channel_mats.push_back(bmat);
Mat mask;
merge(channel_mats, mask); // CV_32FC3 allocated
///不用for循环遍历Mat里的每个像素值,实现numpy.clip函数
mask.setTo(this->min_max[0], mask < this->min_max[0]);
mask.setTo(this->min_max[1], mask > this->min_max[1]); ////也可以用threshold函数,阈值类型THRESH_TOZERO_INV
mask = (mask - this->min_max[0]) / (this->min_max[1] - this->min_max[0]);
mask *= 255.0;
mask.convertTo(mask, CV_8UC3);
cvtColor(mask, mask, COLOR_BGR2RGB);
return mask;
}
int main()
{
CodeFormer mynet("../ONNX_Photo_Restoration/codeformer.onnx");
string imgpath = "../ONNX_Photo_Restoration/image/4.png";
Mat srcimg = imread(imgpath);
Mat dstimg = mynet.detect(srcimg);
resize(dstimg, dstimg, Size(srcimg.cols, srcimg.rows), INTER_LINEAR);
imwrite("../ONNX_Photo_Restoration/result/result.jpg", dstimg);
namedWindow("srcimg", WINDOW_NORMAL);
imshow("srcimg", srcimg);
namedWindow("dstimg", WINDOW_NORMAL);
imshow("dstimg", dstimg);
waitKey(0);
destroyAllWindows();
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于CodeFormer使用C++实现图片模糊变清晰,去除马赛克等效果(代码完整)
共390个文件
hpp:291个
h:62个
tlog:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 9 下载量 78 浏览量
2023-10-14
01:15:45
上传
评论 2
收藏 371.15MB RAR 举报
温馨提示
基于CodeFormer使用C++实现图片模糊变清晰,去除马赛克等效果(代码完整,内含项目所需依赖库文件和模型文件,不需要你再额外配置,真正的开箱即用)。 CodeFormer是一种基于AI技术深度学习的人脸复原模型,由南洋理工大学和商汤科技联合研究中心联合开发。该模型通过结合了VQGAN和Transformer等技术,可以通过提供模糊或马赛克图像来生成清晰的原始图像。可以实现老照片修复、照片马赛克修复、黑白照片彩色化、低码率视频增强,增加细节等效果。它采用了最先进神经网络AI技术,可以精准修复各种老旧、模糊、残缺、黑白的照片和视频,效果惊艳!感觉还是有两把刷子的,确实很强!被网友称为“地表最强AI马赛克去除神器”,感兴趣的朋友,快去试试吧。
资源推荐
资源详情
资源评论
收起资源包目录
基于CodeFormer使用C++实现图片模糊变清晰,去除马赛克等效果(代码完整) (390个子文件)
demo.cpp 5KB
Browse.VC.db 24.27MB
opencv_world480.dll 60.23MB
onnxruntime.dll 5.9MB
onnxruntime.dll 5.9MB
ONNX_Photo_Restoration.exe 37KB
ONNX_Photo_Restoration.vcxproj.filters 957B
core_c.h 129KB
msa_macros.h 82KB
types_c.h 72KB
kmeans_index.h 68KB
onnxruntime_c_api.h 57KB
imgproc_c.h 51KB
dist.h 42KB
cvdef.h 37KB
onnxruntime_cxx_inline.h 34KB
constants_c.h 31KB
cv_cpu_helper.h 30KB
hierarchical_clustering_index.h 27KB
onnxruntime_cxx_api.h 25KB
autotuned_index.h 21KB
kdtree_single_index.h 21KB
kdtree_index.h 21KB
lsh_table.h 19KB
types_c.h 18KB
lsh_index.h 16KB
result_set.h 15KB
index_testing.h 11KB
highgui_c.h 10KB
any.h 9KB
cv_cpu_dispatch.h 9KB
hdf5.h 7KB
heap.h 7KB
allocator.h 6KB
all_indices.h 6KB
composite_index.h 6KB
nn_index.h 6KB
saving.h 6KB
simplex_downhill.h 6KB
videoio_c.h 6KB
calib3d_c.h 5KB
cap_ios.h 5KB
interface.h 5KB
dynamic_bitset.h 5KB
defines.h 5KB
random.h 4KB
params.h 4KB
logger.h 4KB
linear_index.h 4KB
ground_truth.h 3KB
matrix.h 3KB
cvconfig.h 3KB
object_factory.h 3KB
sampling.h 3KB
ios.h 3KB
timer.h 3KB
onnxruntime_session_options_config_keys.h 2KB
general.h 2KB
config.h 2KB
constants_c.h 2KB
interface.h 1KB
macosx.h 751B
interface.h 584B
constants_c.h 478B
constants_c.h 412B
cpu_provider_factory.h 404B
cuda_provider_factory.h 388B
dummy.h 213B
imgcodecs_c.h 146B
intrin_rvv_010_compat_non-policy.hpp 1.74MB
imgproc.hpp 246KB
calib3d.hpp 221KB
color_detail.hpp 221KB
mat.hpp 165KB
intrin_avx512.hpp 160KB
core.hpp 154KB
intrin_sse.hpp 135KB
intrin_avx.hpp 134KB
intrin_lasx.hpp 134KB
intrin_wasm.hpp 110KB
intrin_rvv.hpp 108KB
intrin_cpp.hpp 104KB
intrin_rvv071.hpp 100KB
intrin_neon.hpp 100KB
dnn.hpp 95KB
intrin_rvv_scalable.hpp 93KB
ml.hpp 92KB
mat.inl.hpp 90KB
intrin_rvv_010_compat_overloaded-non-policy.hpp 82KB
core.hpp 80KB
imgproc.hpp 80KB
types.hpp 74KB
features2d.hpp 73KB
intrin_msa.hpp 73KB
intrin_vsx.hpp 68KB
videoio.hpp 67KB
quaternion.hpp 67KB
opencl_clblas.hpp 63KB
vsx_utils.hpp 52KB
vec_math.hpp 50KB
共 390 条
- 1
- 2
- 3
- 4
金戈鐡馬
- 粉丝: 1w+
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页