Skip to content

Commit 8565a10

Browse files
author
Kevin Blackburn-Matzen
committed
serialization for mcc
1 parent ce3c668 commit 8565a10

File tree

5 files changed

+447
-3
lines changed

5 files changed

+447
-3
lines changed

modules/mcc/include/opencv2/mcc/ccm.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ Produce a ColorCorrectionModel instance for inference
360360
class CV_EXPORTS_W ColorCorrectionModel
361361
{
362362
public:
363+
CV_WRAP ColorCorrectionModel();
364+
363365
/** @brief Color Correction Model
364366
365367
Supported list of color cards:
@@ -507,11 +509,17 @@ class CV_EXPORTS_W ColorCorrectionModel
507509
*/
508510
CV_WRAP Mat infer(const Mat& img, bool islinear = false);
509511

512+
CV_WRAP void write(cv::FileStorage& fs) const;
513+
CV_WRAP void read(const cv::FileNode& node);
514+
510515
class Impl;
511516
private:
512517
std::shared_ptr<Impl> p;
513518
};
514519

520+
CV_EXPORTS void write(cv::FileStorage& fs, const std::string&, const ColorCorrectionModel& ccm);
521+
CV_EXPORTS void read(const cv::FileNode& node, ColorCorrectionModel& ccm, const ColorCorrectionModel& default_value = ColorCorrectionModel());
522+
515523
//! @} ccm
516524
} // namespace ccm
517525
} // namespace cv

modules/mcc/src/ccm.cpp

+158
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,28 @@
2727

2828
#include "opencv2/mcc/ccm.hpp"
2929
#include "linearize.hpp"
30+
3031
namespace cv {
3132
namespace ccm {
3233
class ColorCorrectionModel::Impl
3334
{
3435
public:
3536
Mat src;
37+
3638
std::shared_ptr<Color> dst = std::make_shared<Color>();
39+
// Track initialization parameters for serialization
40+
Mat dst_colors;
41+
COLOR_SPACE dst_cs_enum;
42+
Mat dst_colored;
43+
CONST_COLOR dst_constcolor;
44+
bool dst_use_constcolor;
45+
3746
Mat dist;
47+
3848
RGBBase_& cs;
49+
// Track initialization parameters for serialization
50+
COLOR_SPACE cs_enum;
51+
3952
Mat mask;
4053

4154
// RGBl of detected data and the reference
@@ -138,6 +151,7 @@ class ColorCorrectionModel::Impl
138151

139152
ColorCorrectionModel::Impl::Impl()
140153
: cs(*GetCS::getInstance().get_rgb(COLOR_SPACE_sRGB))
154+
, cs_enum(COLOR_SPACE_sRGB)
141155
, ccm_type(CCM_3x3)
142156
, distance(DISTANCE_CIE2000)
143157
, linear_type(LINEARIZATION_GAMMA)
@@ -282,6 +296,10 @@ void ColorCorrectionModel::Impl::fitting(void)
282296
loss = pow((res / masked_len), 0.5);
283297
}
284298

299+
ColorCorrectionModel::ColorCorrectionModel()
300+
: p(std::make_shared<Impl>())
301+
{}
302+
285303
Mat ColorCorrectionModel::infer(const Mat& img, bool islinear)
286304
{
287305
if (!p->ccm.data)
@@ -300,14 +318,24 @@ Mat ColorCorrectionModel::infer(const Mat& img, bool islinear)
300318

301319
void ColorCorrectionModel::Impl::getColor(CONST_COLOR constcolor)
302320
{
321+
dst_use_constcolor = true;
322+
dst_constcolor = constcolor;
303323
dst = (GetColor::getColor(constcolor));
304324
}
305325
void ColorCorrectionModel::Impl::getColor(Mat colors_, COLOR_SPACE ref_cs_)
306326
{
327+
dst_use_constcolor = false;
328+
dst_colors = colors_;
329+
dst_cs_enum = ref_cs_;
330+
dst_colored = Mat();
307331
dst.reset(new Color(colors_, *GetCS::getInstance().get_cs(ref_cs_)));
308332
}
309333
void ColorCorrectionModel::Impl::getColor(Mat colors_, COLOR_SPACE cs_, Mat colored_)
310334
{
335+
dst_use_constcolor = false;
336+
dst_colors = colors_;
337+
dst_cs_enum = cs_;
338+
dst_colored = colored_;
311339
dst.reset(new Color(colors_, *GetCS::getInstance().get_cs(cs_), colored_));
312340
}
313341
ColorCorrectionModel::ColorCorrectionModel(const Mat& src_, CONST_COLOR constcolor)
@@ -331,6 +359,7 @@ ColorCorrectionModel::ColorCorrectionModel(const Mat& src_, Mat colors_, COLOR_S
331359

332360
void ColorCorrectionModel::setColorSpace(COLOR_SPACE cs_)
333361
{
362+
p->cs_enum = cs_;
334363
p->cs = *GetCS::getInstance().get_rgb(cs_);
335364
}
336365
void ColorCorrectionModel::setCCM_TYPE(CCM_TYPE ccm_type_)
@@ -433,5 +462,134 @@ Mat ColorCorrectionModel::getMask() const{
433462
Mat ColorCorrectionModel::getWeights() const{
434463
return p->weights;
435464
}
465+
466+
void ColorCorrectionModel::write(FileStorage& fs) const
467+
{
468+
fs << "{"
469+
<< "ccm" << p->ccm
470+
<< "loss" << p->loss
471+
<< "src" << p->src
472+
<< "dist" << p->dist
473+
<< "cs_enum" << p->cs_enum
474+
<< "src_rgbl" << p->src_rgbl
475+
<< "dst_rgbl" << p->dst_rgbl
476+
<< "mask" << p->mask
477+
<< "ccm_type" << p->ccm_type
478+
<< "shape" << p->shape
479+
<< "linear" << *p->linear
480+
<< "distance" << p->distance
481+
<< "linear_type" << p->linear_type
482+
<< "weights" << p->weights
483+
<< "weights_list" << p->weights_list
484+
<< "ccm0" << p->ccm0
485+
<< "gamma" << p->gamma
486+
<< "deg" << p->deg
487+
<< "saturated_threshold" << p->saturated_threshold
488+
<< "initial_method_type" << p->initial_method_type
489+
<< "weights_coeff" << p->weights_coeff
490+
<< "masked_len" << p->masked_len
491+
<< "max_count" << p->max_count
492+
<< "epsilon" << p->epsilon
493+
<< "dst_use_constcolor" << p->dst_use_constcolor;
494+
495+
if (p->dst_use_constcolor) {
496+
fs << "dst_constcolor" << p->dst_constcolor;
497+
} else {
498+
fs << "dst_colors" << p->dst_colors
499+
<< "dst_cs_enum" << p->dst_cs_enum
500+
<< "dst_colored" << p->dst_colored;
501+
}
502+
fs << "}";
503+
}
504+
505+
506+
void ColorCorrectionModel::read(const FileNode& node)
507+
{
508+
node["ccm"] >> p->ccm;
509+
node["loss"] >> p->loss;
510+
node["src"] >> p->src;
511+
node["dist"] >> p->dist;
512+
node["src_rgbl"] >> p->src_rgbl;
513+
node["dst_rgbl"] >> p->dst_rgbl;
514+
node["mask"] >> p->mask;
515+
node["ccm_type"] >> p->ccm_type;
516+
node["shape"] >> p->shape;
517+
node["distance"] >> p->distance;
518+
node["gamma"] >> p->gamma;
519+
node["deg"] >> p->deg;
520+
node["saturated_threshold"] >> p->saturated_threshold;
521+
node["initial_method_type"] >> p->initial_method_type;
522+
node["weights_coeff"] >> p->weights_coeff;
523+
node["weights"] >> p->weights;
524+
node["weights_list"] >> p->weights_list;
525+
node["ccm0"] >> p->ccm0;
526+
node["masked_len"] >> p->masked_len;
527+
node["max_count"] >> p->max_count;
528+
node["epsilon"] >> p->epsilon;
529+
530+
COLOR_SPACE cs_enum;
531+
node["cs_enum"] >> cs_enum;
532+
setColorSpace(cs_enum);
533+
534+
bool dst_use_constcolor;
535+
node["dst_use_constcolor"] >> dst_use_constcolor;
536+
if (dst_use_constcolor) {
537+
CONST_COLOR dst_constcolor;
538+
node["dst_constcolor"] >> dst_constcolor;
539+
p->getColor(dst_constcolor);
540+
} else {
541+
Mat dst_colors;
542+
node["dst_colors"] >> dst_colors;
543+
COLOR_SPACE dst_cs_enum;
544+
node["dst_cs_enum"] >> dst_cs_enum;
545+
Mat dst_colored;
546+
node["dst_colored"] >> dst_colored;
547+
if (dst_colored.empty()) {
548+
p->getColor(dst_colors, dst_cs_enum);
549+
} else {
550+
p->getColor(dst_colors, dst_cs_enum, dst_colored);
551+
}
552+
}
553+
554+
node["linear_type"] >> p->linear_type;
555+
switch (p->linear_type) {
556+
case cv::ccm::LINEARIZATION_GAMMA:
557+
p->linear = std::shared_ptr<Linear>(new LinearGamma());
558+
break;
559+
case cv::ccm::LINEARIZATION_COLORPOLYFIT:
560+
p->linear = std::shared_ptr<Linear>(new LinearColor<Polyfit>());
561+
break;
562+
case cv::ccm::LINEARIZATION_IDENTITY:
563+
p->linear = std::shared_ptr<Linear>(new LinearIdentity());
564+
break;
565+
case cv::ccm::LINEARIZATION_COLORLOGPOLYFIT:
566+
p->linear = std::shared_ptr<Linear>(new LinearColor<LogPolyfit>());
567+
break;
568+
case cv::ccm::LINEARIZATION_GRAYPOLYFIT:
569+
p->linear = std::shared_ptr<Linear>(new LinearGray<Polyfit>());
570+
break;
571+
case cv::ccm::LINEARIZATION_GRAYLOGPOLYFIT:
572+
p->linear = std::shared_ptr<Linear>(new LinearGray<LogPolyfit>());
573+
break;
574+
default:
575+
CV_Error(Error::StsBadArg, "Wrong linear_type!");
576+
break;
577+
}
578+
node["linear"] >> *p->linear;
579+
}
580+
581+
void write(FileStorage& fs, const std::string&, const cv::ccm::ColorCorrectionModel& ccm)
582+
{
583+
ccm.write(fs);
584+
}
585+
586+
void read(const cv::FileNode& node, cv::ccm::ColorCorrectionModel& ccm, const cv::ccm::ColorCorrectionModel& default_value)
587+
{
588+
if (node.empty())
589+
ccm = default_value;
590+
else
591+
ccm.read(node);
592+
}
593+
436594
}
437595
} // namespace cv::ccm

0 commit comments

Comments
 (0)