In this tutorial you will learn how to use the 'mcc' module to detect colorcharts in a image. Here we will only use the basic detection algorithm and an improved version that enhances accuracy using a neural network.
Run a movie on a standard macbeth chart:
/home/opencv/build/bin/example_cpp_macbeth_chart_detection -t=0 -v=mcc24.mp4
Or run on a vinyl macbeth chart from camera 0:
/home/opencv/build/bin/example_cpp_macbeth_chart_detection -t=2 --ci=0
Or run on a vinyl macbeth chart, detecting the best 5 charts(Detections can be less than 5 but never more):
/home/opencv/build/bin/example_cpp_macbeth_chart_detection -t=2 --ci=0 --nc=5
To run on GPU with neural network
/home/opencv/build/bin/example_cpp_macbeth_chart_detection -t=0 -m=/home/model.pb --pb=/home/model.pbtxt -v=mcc24.mp4 --use_gpu
To run on GPU with neural network and detect the best 5 charts (Detections can be less than 5 but not more than 5)
/home/opencv/build/bin/example_cpp_macbeth_chart_detection -t=0 -m=/home/model.pb --pb=/home/model.pbtxt -v=mcc24.mp4 --use_gpu --nc=5
6#include "../dnn/common.hpp"
18 "This sample demonstrates mcc checker detection with DNN based model and thresholding (default) techniques.\n\n"
20 "\t ./example_cpp_macbeth_chart_detection --input=path/to/your/input/image/or/video (don't give --input flag if want to use device camera)\n"
23 "\t ./example_cpp_macbeth_chart_detection mcc --input=path/to/your/input/image/or/video\n\n"
24 "Model path can also be specified using --model argument. And config path can be specified using --config. Download it using python download_models.py mcc from dnn samples directory\n\n"
26 "Note: DNN-based detection is not available in this build.\n\n"
30const string param_keys =
31 "{ help h | | Print help message. }"
33 "{ @alias | | An alias name of model to extract preprocessing parameters from models.yml file. }"
34 "{ zoo | ../dnn/models.yml | An optional path to file with preprocessing parameters }"
36 "{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera.}"
37 "{ type | 0 | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl, default:0 }"
38 "{ num_charts | 1 | Maximum number of charts in the image }"
40 "{ model | | Path to the model file for using dnn model. }";
46const string backend_keys = format(
47 "{ backend | default | Choose one of computation backends: "
48 "default: automatically (by default), "
49 "openvino: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
50 "opencv: OpenCV implementation, "
55const string target_keys = format(
56 "{ target | cpu | Choose one of target computation devices: "
57 "cpu: CPU target (by default), "
59 "opencl_fp16: OpenCL fp16 (half-float precision), "
63 "cuda_fp16: CUDA fp16 (half-float preprocess) }");
67string keys = param_keys;
68static void initKeys() {
70 keys += backend_keys + target_keys;
76 if (!detector->process(frame, nc))
80 vector<Ptr<CChecker>> checkers = detector->getListColorChecker();
81 detector->draw(checkers, frame);
82 src = checkers[0]->getChartsRGB(
false);
83 tgt = detector->getRefColors();
85 imshow(
"Image result", frame);
86 imshow(
"Original", imageCopy);
91int main(
int argc,
char *argv[])
97 if (parser.has(
"help"))
99 cout << about << endl;
100 parser.printMessage();
104#ifdef HAVE_OPENCV_DNN
105 string modelName = parser.get<
String>(
"@alias");
106 string zooFile = parser.get<
String>(
"zoo");
107 const char* path = getenv(
"OPENCV_SAMPLES_DATA_PATH");
108 if ((path != NULL) || parser.has(
"@alias") || (parser.get<
String>(
"model") !=
"")) {
112 cout<<
"[WARN] set the environment variables or pass the arguments --model, --config and models.yml file using --zoo for using dnn based detector. Continuing with default detector.\n\n";
115 keys += genPreprocArguments(modelName, zooFile);
119 int t = parser.get<
int>(
"type");
124#ifdef HAVE_OPENCV_DNN
125 const string sha1 = parser.get<
String>(
"sha1");
126 const string model_path = findModel(parser.get<
string>(
"model"), sha1);
127 const string config_sha1 = parser.get<
String>(
"config_sha1");
128 const string pbtxt_path = findModel(parser.get<
string>(
"config"), config_sha1);
130 const string target = parser.get<
String>(
"target");
133 int nc = parser.get<
int>(
"num_charts");
136#ifdef HAVE_OPENCV_DNN
137 if (model_path !=
"" && pbtxt_path !=
""){
139 if (backend !=
"default" || target !=
"cpu"){
146 detector = CCheckerDetector::create(net);
147 cout<<
"Detecting checkers using neural network."<<endl;
150 detector = CCheckerDetector::create();
153 detector = CCheckerDetector::create();
155 detector->setColorChartType(chartType);
161 if (parser.has(
"input")){
162 const string inputFile = parser.get<
String>(
"input");
174 cout <<
"[ERROR] Could not open file as an image or video: " << inputFile << endl;
185 cout<<
"To print the actual colors and reference colors for current frame press SPACEBAR. To resume press SPACEBAR again"<<endl;
192 found = processFrame(frame, detector, src, tgt, nc);
197 cout<<
"Reference colors: "<<tgt<<endl<<
"--------------------"<<endl;
198 cout<<
"Actual colors: "<<src<<endl<<endl;
199 cout<<
"Press spacebar to resume."<<endl;
202 cout <<
"Resumed! Processing continues..." << endl;
205 cout<<
"No color chart detected!!"<<endl;
208 else if (key == 27) exit(0);
211 cout<<
"Reference colors: "<<tgt<<endl<<
"--------------------"<<endl;
212 cout<<
"Actual colors: "<<src<<endl<<endl;
216 found = processFrame(image, detector, src, tgt, nc);
218 cout<<
"Reference colors: "<<tgt<<endl<<
"--------------------"<<endl;
219 cout<<
"Actual colors: "<<src<<endl<<endl;
223 cout<<
"No chart detected!!"<<endl;
Designed for command line parsing.
Definition utility.hpp:890
n-dimensional dense array class
Definition mat.hpp:952
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:764
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.
virtual bool retrieve(OutputArray image, int flag=0)
Decodes and returns the grabbed video frame.
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
virtual bool grab()
Grabs the next frame from video file or capturing device.
This class allows to create and manipulate comprehensive artificial neural networks.
Definition dnn.hpp:535
void setPreferableBackend(int backendId)
Ask network to use specific computation backend where it supported.
void setPreferableTarget(int targetId)
Ask network to make computations on specific target device.
std::string String
Definition cvstd.hpp:151
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition exception.hpp:198
Net readNetFromTensorflow(CV_WRAP_FILE_PATH const String &model, CV_WRAP_FILE_PATH const String &config=String(), int engine=ENGINE_AUTO, const std::vector< String > &extraOutputs=std::vector< String >())
Reads a network model stored in TensorFlow framework's format.
EngineType
Definition dnn.hpp:1020
@ ENGINE_CLASSIC
Force use the old dnn engine similar to 4.x branch.
Definition dnn.hpp:1021
@ ENGINE_AUTO
Try to use the new engine and then fall back to the classic version.
Definition dnn.hpp:1023
cv::gapi::GBackend backend()
Get a reference to CPU (OpenCV) backend.
int waitKey(int delay=0)
Waits for a pressed key.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
ColorChart
enum to hold the type of the checker
Definition mcc_checker_detector.hpp:53
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition all_layers.hpp:47