OpenCV 5.0.0
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
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);
return 0;
}
Designed for command line parsing.
Definition utility.hpp:915
T get(const String &name, bool space_delete=true) const
Access arguments by name.
Definition utility.hpp:981
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.
Comma-separated Matrix Initializer.
Definition mat.hpp:964
MatSize size
Definition mat.hpp:2511
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.
This class allows to create and manipulate comprehensive artificial neural networks.
Definition dnn.hpp:558
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.
void merge(const Mat *mv, size_t count, OutputArray dst)
Creates one multi-channel array out of several single-channel ones.
Size2i Size
Definition types.hpp:373
Scalar_< double > Scalar
Definition types.hpp:712
#define CV_32F
Definition interface.h:59
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
String format(const char *fmt,...)
Returns a text string formatted using the printf-like expression.
Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size &size=Size(), const Scalar &mean=Scalar(), bool swapRB=false, bool crop=false, int ddepth=CV_32F)
Creates 4-dimensional blob from image. Optionally resizes and crops image from center,...
Net readNetFromONNX(CV_WRAP_FILE_PATH const String &onnxFile, int engine=ENGINE_AUTO)
Reads a network model ONNX.
EngineType
Definition dnn.hpp:1085
@ 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
@ ENGINE_CLASSIC
Force use the old dnn engine similar to 4.x branch.
Definition dnn.hpp:1086
@ ENGINE_AUTO
Try to use the new engine and then fall back to the classic version.
Definition dnn.hpp:1088
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
@ IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image (codec internal conversion).
Definition imgcodecs.hpp:71
Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
@ COLOR_Lab2BGR
[8U/32F]
Definition imgproc.hpp:604
void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)
Resizes an image.
@ INTER_CUBIC
Definition imgproc.hpp:255
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition all_layers.hpp:47
Definition core.hpp:107
STL namespace.