#include "pch.h"
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace cv::dnn;
using namespace std;
int main(int argc, char **argv)
{
string modelBin = "E:/未来项目/炼数成金/(录制中)端到端/L5/models/instance_norm/starry_night.t7";
string imageFile = "E:/template/library.jpg";
float scale = 1.0;
cv::Scalar mean{ 103.939, 116.779, 123.68 };
bool swapRB = false;
bool crop = false;
bool useOpenCL = false;
Mat img = imread(imageFile);
pyrDown(img, img);
if (img.empty()) {
cout << "Can't read image from file: " << imageFile << endl;
return 2;
}
// Load model
Net net = dnn::readNetFromTorch(modelBin);
if (useOpenCL)
net.setPreferableTarget(DNN_TARGET_OPENCL);
// Create a 4D blob from a frame.
Mat inputBlob = blobFromImage(img, scale, img.size(), mean, swapRB, crop);
// forward netword
net.setInput(inputBlob);
Mat output = net.forward();
// process output
Mat(output.size[2], output.size[3], CV_32F, output.ptr<float>(0, 0)) += 103.939;
Mat(output.size[2], output.size[3], CV_32F, output.ptr<float>(0, 1)) += 116.779;
Mat(output.size[2], output.size[3], CV_32F, output.ptr<float>(0, 2)) += 123.68;
std::vector<cv::Mat> ress;
imagesFromBlob(output, ress);
// show res
Mat res;
ress[0].convertTo(res, CV_8UC3);
imshow("reslut", res);
imshow("origin", img);
waitKey();
return 0;
}
结果: