27
27
28
28
#include " opencv2/mcc/ccm.hpp"
29
29
#include " linearize.hpp"
30
+
30
31
namespace cv {
31
32
namespace ccm {
32
33
class ColorCorrectionModel ::Impl
33
34
{
34
35
public:
35
36
Mat src;
37
+
36
38
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
+
37
46
Mat dist;
47
+
38
48
RGBBase_& cs;
49
+ // Track initialization parameters for serialization
50
+ COLOR_SPACE cs_enum;
51
+
39
52
Mat mask;
40
53
41
54
// RGBl of detected data and the reference
@@ -138,6 +151,7 @@ class ColorCorrectionModel::Impl
138
151
139
152
ColorCorrectionModel::Impl::Impl ()
140
153
: cs(*GetCS::getInstance ().get_rgb(COLOR_SPACE_sRGB))
154
+ , cs_enum(COLOR_SPACE_sRGB)
141
155
, ccm_type(CCM_3x3)
142
156
, distance(DISTANCE_CIE2000)
143
157
, linear_type(LINEARIZATION_GAMMA)
@@ -282,6 +296,10 @@ void ColorCorrectionModel::Impl::fitting(void)
282
296
loss = pow ((res / masked_len), 0.5 );
283
297
}
284
298
299
+ ColorCorrectionModel::ColorCorrectionModel ()
300
+ : p(std::make_shared<Impl>())
301
+ {}
302
+
285
303
Mat ColorCorrectionModel::infer (const Mat& img, bool islinear)
286
304
{
287
305
if (!p->ccm .data )
@@ -300,14 +318,24 @@ Mat ColorCorrectionModel::infer(const Mat& img, bool islinear)
300
318
301
319
void ColorCorrectionModel::Impl::getColor (CONST_COLOR constcolor)
302
320
{
321
+ dst_use_constcolor = true ;
322
+ dst_constcolor = constcolor;
303
323
dst = (GetColor::getColor (constcolor));
304
324
}
305
325
void ColorCorrectionModel::Impl::getColor (Mat colors_, COLOR_SPACE ref_cs_)
306
326
{
327
+ dst_use_constcolor = false ;
328
+ dst_colors = colors_;
329
+ dst_cs_enum = ref_cs_;
330
+ dst_colored = Mat ();
307
331
dst.reset (new Color (colors_, *GetCS::getInstance ().get_cs (ref_cs_)));
308
332
}
309
333
void ColorCorrectionModel::Impl::getColor (Mat colors_, COLOR_SPACE cs_, Mat colored_)
310
334
{
335
+ dst_use_constcolor = false ;
336
+ dst_colors = colors_;
337
+ dst_cs_enum = cs_;
338
+ dst_colored = colored_;
311
339
dst.reset (new Color (colors_, *GetCS::getInstance ().get_cs (cs_), colored_));
312
340
}
313
341
ColorCorrectionModel::ColorCorrectionModel (const Mat& src_, CONST_COLOR constcolor)
@@ -331,6 +359,7 @@ ColorCorrectionModel::ColorCorrectionModel(const Mat& src_, Mat colors_, COLOR_S
331
359
332
360
void ColorCorrectionModel::setColorSpace (COLOR_SPACE cs_)
333
361
{
362
+ p->cs_enum = cs_;
334
363
p->cs = *GetCS::getInstance ().get_rgb (cs_);
335
364
}
336
365
void ColorCorrectionModel::setCCM_TYPE (CCM_TYPE ccm_type_)
@@ -433,5 +462,134 @@ Mat ColorCorrectionModel::getMask() const{
433
462
Mat ColorCorrectionModel::getWeights () const {
434
463
return p->weights ;
435
464
}
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
+
436
594
}
437
595
} // namespace cv::ccm
0 commit comments