OpenCV  5.0.0alpha
Open Source Computer Vision
Loading...
Searching...
No Matches
samples/dnn/colorization.cpp
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html
// To download the onnx model, see: https://storage.googleapis.com/ailia-models/colorization/colorizer.onnx
#include <opencv2/dnn.hpp>
#include "common.hpp"
#include <iostream>
using namespace cv;
using namespace std;
using namespace cv::dnn;
int main(int argc, char** argv) {
const string about =
"This sample demonstrates recoloring grayscale images with dnn.\n"
"This program is based on:\n"
" http://richzhang.github.io/colorization\n"
" https://github.com/richzhang/colorization\n"
"To download the onnx model:\n"
" https://storage.googleapis.com/ailia-models/colorization/colorizer.onnx\n";
const string param_keys =
"{ help h | | Print help message. }"
"{ input i | baboon.jpg | Path to the input image }"
"{ onnx_model_path | | Path to the ONNX model. Required. }";
const string backend_keys = format(
"{ backend | 0 | Choose one of computation backends: "
"%d: automatically (by default), "
"%d: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
"%d: OpenCV implementation, "
"%d: VKCOM, "
"%d: CUDA, "
"%d: WebNN }",
const string target_keys = format(
"{ target | 0 | Choose one of target computation devices: "
"%d: CPU target (by default), "
"%d: OpenCL, "
"%d: OpenCL fp16 (half-float precision), "
"%d: VPU, "
"%d: Vulkan, "
"%d: CUDA, "
"%d: CUDA fp16 (half-float preprocess) }",
const string keys = param_keys + backend_keys + target_keys;
CommandLineParser parser(argc, argv, keys);
parser.about(about);
if (parser.has("help")) {
parser.printMessage();
return 0;
}
string inputImagePath = parser.get<string>("input");
string onnxModelPath = parser.get<string>("onnx_model_path");
int backendId = parser.get<int>("backend");
int targetId = parser.get<int>("target");
if (onnxModelPath.empty()) {
cerr << "The path to the ONNX model is required!" << endl;
return -1;
}
Mat imgGray = imread(samples::findFile(inputImagePath), IMREAD_GRAYSCALE);
if (imgGray.empty()) {
cerr << "Could not read the image: " << inputImagePath << endl;
return -1;
}
Mat imgL = imgGray;
imgL.convertTo(imgL, CV_32F, 100.0/255.0);
Mat imgLResized;
resize(imgL, imgLResized, Size(256, 256), 0, 0, INTER_CUBIC);
// Prepare the model
EngineType engine = ENGINE_AUTO;
if (backendId != 0 || targetId != 0){
engine = ENGINE_CLASSIC;
}
dnn::Net net = dnn::readNetFromONNX(onnxModelPath, engine);
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
// Create blob from the image
Mat blob = dnn::blobFromImage(imgLResized, 1.0, Size(256, 256), Scalar(), false, false);
net.setInput(blob);
// Run inference
Mat result = net.forward();
Size siz(result.size[2], result.size[3]);
Mat a(siz, CV_32F, result.ptr(0,0));
Mat b(siz, CV_32F, result.ptr(0,1));
resize(a, a, imgGray.size());
resize(b, b, imgGray.size());
// merge, and convert back to BGR
Mat color, chn[] = {imgL, a, b};
// Proc
Mat lab;
merge(chn, 3, lab);
cvtColor(lab, color, COLOR_Lab2BGR);
imshow("input image", imgGray);
imshow("output image", color);
waitKey();
return 0;
}
Designed for command line parsing.
Definition utility.hpp:890
T get(const String &name, bool space_delete=true) const
Access arguments by name.
Definition utility.hpp:956
void about(const String &message)
Set the about message.
void printMessage() const
Print help message.
bool has(const String &name) const
Check if field was provided in the command line.
n-dimensional dense array class
Definition mat.hpp:951
MatSize size
Definition mat.hpp:2448
uchar * ptr(int i0=0)
Returns a pointer to the specified matrix row.
bool empty() const
Returns true if the array has no elements.
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
Template class for specifying the size of an image or rectangle.
Definition types.hpp:338
This class allows to create and manipulate comprehensive artificial neural networks.
Definition dnn.hpp:535
void setInput(CV_ND InputArray blob, const String &name="", double scalefactor=1.0, const Scalar &mean=Scalar())
Sets the new input value for the network.
void setPreferableBackend(int backendId)
Ask network to use specific computation backend where it supported.
Mat forward(const String &outputName=String())
Runs forward pass to compute output of layer with name outputName.
void setPreferableTarget(int targetId)
Ask network to make computations on specific target device.
#define CV_32F
Definition interface.h:81
EngineType
Definition dnn.hpp:1020
@ DNN_BACKEND_DEFAULT
DNN_BACKEND_DEFAULT equals to OPENCV_DNN_BACKEND_DEFAULT, which can be defined using CMake or a confi...
Definition dnn.hpp:74
@ DNN_BACKEND_INFERENCE_ENGINE
Definition dnn.hpp:75
@ DNN_BACKEND_OPENCV
Definition dnn.hpp:77
@ DNN_BACKEND_VKCOM
Definition dnn.hpp:78
@ DNN_BACKEND_CUDA
Definition dnn.hpp:79
@ DNN_BACKEND_WEBNN
Definition dnn.hpp:80
@ DNN_TARGET_CPU
Definition dnn.hpp:97
@ DNN_TARGET_MYRIAD
Definition dnn.hpp:100
@ DNN_TARGET_CUDA_FP16
Definition dnn.hpp:104
@ DNN_TARGET_OPENCL
Definition dnn.hpp:98
@ DNN_TARGET_CUDA
Definition dnn.hpp:103
@ DNN_TARGET_VULKAN
Definition dnn.hpp:101
@ DNN_TARGET_OPENCL_FP16
Definition dnn.hpp:99
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition all_layers.hpp:47
Definition core.hpp:107
STL namespace.