如果有任何建议意见或组织社团老师同学有意共同建设git,请联系20666093@tongji.edu.cn

未验证 提交 54d878da 编辑于 作者: Gursimar Singh's avatar Gursimar Singh 提交者: GitHub
浏览文件

Removed MCC detector from OpenCV Contrib for migration to OpenCV Main (#3899)

* Separated mcc and ccm modules

* Removed mcc dependency

* Fixed unused variables

* Renamed ccm to mcc, and removed mcc

* Fixed documentation
上级 4942f711
显示 115 个添加3325 个删除
+115 -3325
set(the_description "Macbeth Chart Detection")
set(the_description "Color Correction Module")
ocv_define_module(mcc
opencv_core
opencv_imgproc
......
......@@ -25,8 +25,8 @@
// Jinheng Zhang <zhangjinheng1@huawei.com>
// Chenqi Shan <shanchenqi@huawei.com>
#ifndef __OPENCV_MCC_CCM_HPP__
#define __OPENCV_MCC_CCM_HPP__
#ifndef __OPENCV_CCM_HPP__
#define __OPENCV_CCM_HPP__
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
......@@ -35,7 +35,8 @@ namespace cv
{
namespace ccm
{
/** @addtogroup color_correction
/** @defgroup ccm Color Correction module
@{
Introduction
......
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __OPENCV_MCC_HPP__
#define __OPENCV_MCC_HPP__
#include "mcc/checker_detector.hpp"
#include "mcc/checker_model.hpp"
#include "mcc/ccm.hpp"
/** @defgroup mcc Macbeth Chart module
@{
@defgroup color_correction Color Correction Model
@}
@addtogroup mcc
Introduction
------------
ColorCharts are a tool for calibrating the color profile of camera, which not
only depends on the intrinsic and extrinsic parameters of camera but also on the
lighting conditions. This is done by taking the image of a chart, such that the
value of its colors present in it known, in the image the color values changes
depeding on many variables, this gives us the colors initially present and the
colors that are present in the image, based on this information we can apply any
suitable algorithm to find the actual color of all the objects present in the
image.
*/
#endif
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __OPENCV_MCC_CHECKER_DETECTOR_HPP__
#define __OPENCV_MCC_CHECKER_DETECTOR_HPP__
#include <opencv2/core.hpp>
#include "checker_model.hpp"
#include <opencv2/dnn.hpp>
//----------To view debugging output-----------------------------
//Read the tutorial on how to use debugging in this module
//It can be found in the documentation of 'mcc' modules,
//Then uncomment the following line to view debugging output
//---------------------------------------------------------------
// #define MCC_DEBUG
//---------------------------------------------------------------
namespace cv
{
namespace mcc
{
//! @addtogroup mcc
//! @{
/**
* @brief Parameters for the detectMarker process:
* - int adaptiveThreshWinSizeMin : minimum window size for adaptive
* thresholding before finding contours
* (default 23).
* - int adaptiveThreshWinSizeMax : maximum window size for adaptive
* thresholding before finding contours
* (default 153).
* - int adaptiveThreshWinSizeStep : increments from adaptiveThreshWinSizeMin to
* adaptiveThreshWinSizeMax during the
* thresholding (default 16).
* - double adaptiveThreshConstant : constant for adaptive thresholding before
* finding contours (default 7)
* - double minContoursAreaRate : determine minimum area for marker contour to
* be detected. This is defined as a rate respect
* to the area of the input image. Used only if
* neural network is used (default 0.003).
* - double minContoursArea : determine minimum area for marker contour to be
* detected. This is defined as the actual area. Used
* only if neural network is not used (default 100).
* - double confidenceThreshold : minimum confidence for a bounding box detected
* by neural network to classify as
* detection.(default 0.5)
* (0<=confidenceThreshold<=1)
* - double minContourSolidity : minimum solidity of a contour for it be
* detected as a square in the chart. (default
* 0.9).
* - double findCandidatesApproxPolyDPEpsMultiplier : multipler to be used in
* cv::ApproxPolyDP function
* (default 0.05)
* - int borderWidth : width of the padding used to pass the inital neural
* network detection in the succeeding system.(default 0)
* - float B0factor : distance between two neighbours squares of the same chart.
* Defined as the ratio between distance and large dimension
* of square (default 1.25)
* - float maxError : maximum allowed error in the detection of a chart.
* default(0.1)
* - int minContourPointsAllowed : minium points in a detected contour.
* default(4)
* - int minContourLengthAllowed : minimum length of a countour. default(100)
* - int minInterContourDistance : minimum distance between two contours.
* default(100)
* - int minInterCheckerDistance : minimum distance between two checkers.
* default(10000)
* - int minImageSize : minimum size of the smaller dimension of the image.
* default(1000)
* - unsigned minGroupSize : minimum number of a squared of a chart that must be
* detected. default(4)
*/
struct CV_EXPORTS_W DetectorParameters
{
DetectorParameters();
CV_WRAP static Ptr<DetectorParameters> create();
CV_PROP_RW int adaptiveThreshWinSizeMin;
CV_PROP_RW int adaptiveThreshWinSizeMax;
CV_PROP_RW int adaptiveThreshWinSizeStep;
CV_PROP_RW double adaptiveThreshConstant;
CV_PROP_RW double minContoursAreaRate;
CV_PROP_RW double minContoursArea;
CV_PROP_RW double confidenceThreshold;
CV_PROP_RW double minContourSolidity;
CV_PROP_RW double findCandidatesApproxPolyDPEpsMultiplier;
CV_PROP_RW int borderWidth;
CV_PROP_RW float B0factor;
CV_PROP_RW float maxError;
CV_PROP_RW int minContourPointsAllowed;
CV_PROP_RW int minContourLengthAllowed;
CV_PROP_RW int minInterContourDistance;
CV_PROP_RW int minInterCheckerDistance;
CV_PROP_RW int minImageSize;
CV_PROP_RW unsigned minGroupSize;
};
/** @brief A class to find the positions of the ColorCharts in the image.
*/
class CV_EXPORTS_W CCheckerDetector : public Algorithm
{
public:
/** \brief Set the net which will be used to find the approximate
* bounding boxes for the color charts.
*
* It is not necessary to use this, but this usually results in
* better detection rate.
*
* \param net the neural network, if the network in empty, then
* the function will return false.
* \return true if it was able to set the detector's network,
* false otherwise.
*/
CV_WRAP virtual bool setNet(dnn::Net net) = 0;
/** \brief Find the ColorCharts in the given image.
*
* The found charts are not returned but instead stored in the
* detector, these can be accessed later on using getBestColorChecker()
* and getListColorChecker()
* \param image image in color space BGR
* \param chartType type of the chart to detect
* \param regionsOfInterest regions of image to look for the chart, if
* it is empty, charts are looked for in the
* entire image
* \param nc number of charts in the image, if you don't know the exact
* then keeping this number high helps.
* \param useNet if it is true the network provided using the setNet()
* is used for preliminary search for regions where chart
* could be present, inside the regionsOfInterest provied.
* \param params parameters of the detection system. More information
* about them can be found in the struct DetectorParameters.
* \return true if atleast one chart is detected otherwise false
*/
CV_WRAP_AS(processWithROI) virtual bool
process(InputArray image, const TYPECHART chartType,
const std::vector<Rect> &regionsOfInterest,
const int nc = 1, bool useNet = false,
const Ptr<DetectorParameters> &params = DetectorParameters::create()) = 0;
/** \brief Find the ColorCharts in the given image.
*
* Differs from the above one only in the arguments.
*
* This version searches for the chart in the full image.
*
* The found charts are not returned but instead stored in the
* detector, these can be accessed later on using getBestColorChecker()
* and getListColorChecker()
* \param image image in color space BGR
* \param chartType type of the chart to detect
* \param nc number of charts in the image, if you don't know the exact
* then keeping this number high helps.
* \param useNet if it is true the network provided using the setNet()
* is used for preliminary search for regions where chart
* could be present, inside the regionsOfInterest provied.
* \param params parameters of the detection system. More information
* about them can be found in the struct DetectorParameters.
* \return true if atleast one chart is detected otherwise false
*/
CV_WRAP virtual bool
process(InputArray image, const TYPECHART chartType,
const int nc = 1, bool useNet = false,
const Ptr<DetectorParameters> &params = DetectorParameters::create()) = 0;
/** \brief Get the best color checker. By the best it means the one
* detected with the highest confidence.
* \return checker A single colorchecker, if atleast one colorchecker
* was detected, 'nullptr' otherwise.
*/
CV_WRAP virtual Ptr<mcc::CChecker> getBestColorChecker() = 0;
/** \brief Get the list of all detected colorcheckers
* \return checkers vector of colorcheckers
*/
CV_WRAP virtual std::vector<Ptr<CChecker>> getListColorChecker() = 0;
/** \brief Returns the implementation of the CCheckerDetector.
*
*/
CV_WRAP static Ptr<CCheckerDetector> create();
};
//! @} mcc
} // namespace mcc
} // namespace cv
#endif
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __OPENCV_MCC_CHECKER_MODEL_HPP__
#define __OPENCV_MCC_CHECKER_MODEL_HPP__
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
namespace cv
{
namespace mcc
{
//! @addtogroup mcc
//! @{
/** TYPECHART
*
* \brief enum to hold the type of the checker
*
*/
enum TYPECHART
{
MCC24 = 0, ///< Standard Macbeth Chart with 24 squares
SG140, ///< DigitalSG with 140 squares
VINYL18, ///< DKK color chart with 12 squares and 6 rectangle
};
/** CChecker
*
* \brief checker object
*
* This class contains the information about the detected checkers,i.e, their
* type, the corners of the chart, the color profile, the cost, centers chart,
* etc.
*
*/
class CV_EXPORTS_W CChecker
{
public:
CChecker() {}
virtual ~CChecker() {}
/** \brief Create a new CChecker object.
* \return A pointer to the implementation of the CChecker
*/
CV_WRAP static Ptr<CChecker> create();
public:
// CV_PROP_RW TYPECHART target; ///< type of checkercolor
// CV_PROP_RW std::vector<cv::Point2f> box; ///< positions of the corners
// CV_PROP_RW cv::Mat charts_rgb; ///< charts profile in rgb color space
// CV_PROP_RW cv::Mat charts_ycbcr; ///< charts profile in YCbCr color space
// CV_PROP_RW float cost; ///< cost to aproximate
// CV_PROP_RW cv::Point2f center; ///< center of the chart.
CV_WRAP virtual void setTarget(TYPECHART _target) = 0;
CV_WRAP virtual void setBox(std::vector<Point2f> _box) = 0;
CV_WRAP virtual void setChartsRGB(Mat _chartsRGB) = 0;
CV_WRAP virtual void setChartsYCbCr(Mat _chartsYCbCr) = 0;
CV_WRAP virtual void setCost(float _cost) = 0;
CV_WRAP virtual void setCenter(Point2f _center) = 0;
CV_WRAP virtual TYPECHART getTarget() = 0;
CV_WRAP virtual std::vector<Point2f> getBox() = 0;
/** @brief Computes and returns the coordinates of the central parts of the charts modules.
*
* This method computes transformation matrix from the checkers's coordinates (`cv::mcc::CChecker::getBox()`)
* and find by this the coordinates of the central parts of the charts modules.
* It is used in `cv::mcc::CCheckerDraw::draw()` and in `ChartsRGB` calculation.
*/
CV_WRAP virtual std::vector<Point2f> getColorCharts() = 0;
CV_WRAP virtual Mat getChartsRGB() = 0;
CV_WRAP virtual Mat getChartsYCbCr() = 0;
CV_WRAP virtual float getCost() = 0;
CV_WRAP virtual Point2f getCenter() = 0;
};
/** \brief checker draw
*
* This class contains the functions for drawing a detected chart. This class
* expects a pointer to the checker which will be drawn by this object in the
* constructor and then later on whenever the draw function is called the
* checker will be drawn. Remember that it is not possible to change the
* checkers which will be draw by a given object, as it is decided in the
* constructor itself. If you want to draw some other object you can create a
* new CCheckerDraw instance.
*
* The reason for this type of design is that in some videos we can assume that
* the checker is always in the same position, even if the image changes, so
* the drawing will always take place at the same position.
*/
class CV_EXPORTS_W CCheckerDraw
{
public:
virtual ~CCheckerDraw() {}
/** \brief Draws the checker to the given image.
* \param img image in color space BGR
*/
CV_WRAP virtual void draw(InputOutputArray img) = 0;
/** \brief Create a new CCheckerDraw object.
* \param pChecker The checker which will be drawn by this object.
* \param color The color by with which the squares of the checker
* will be drawn
* \param thickness The thickness with which the sqaures will be
* drawn
* \return A pointer to the implementation of the CCheckerDraw
*/
CV_WRAP static Ptr<CCheckerDraw> create(Ptr<CChecker> pChecker,
cv::Scalar color = CV_RGB(0, 250, 0),
int thickness = 2);
};
//! @} mcc
} // namespace mcc
} // namespace cv
#endif
#include "opencv2/mcc.hpp"
template <>
struct pyopencvVecConverter<Ptr<mcc::CChecker>>
{
static bool to(PyObject *obj, std::vector<Ptr<mcc::CChecker>> &value,
const ArgInfo &info)
{
return pyopencv_to_generic_vec(obj, value, info);
}
static PyObject *from(const std::vector<Ptr<mcc::CChecker>> &value)
{
return pyopencv_from_generic_vec(value);
}
};
typedef std::vector<cv::Ptr<mcc::CChecker>> vector_Ptr_CChecker;
typedef dnn::Net dnn_Net;
......@@ -3,6 +3,7 @@
// of this distribution and at http://opencv.org/license.html.
#include "perf_precomp.hpp"
#include "opencv2/ccm.hpp"
namespace opencv_test
{
......@@ -10,18 +11,7 @@ namespace
{
using namespace std;
PERF_TEST(CV_mcc_perf, detect) {
string path = cvtest::findDataFile("cv/mcc/mcc_ccm_test.jpg");
Mat img = imread(path, IMREAD_COLOR);
Ptr<CCheckerDetector> detector = CCheckerDetector::create();
// detect MCC24 board
TEST_CYCLE() {
ASSERT_TRUE(detector->process(img, MCC24, 1, false));
}
SANITY_CHECK_NOTHING();
}
using namespace cv::ccm;
PERF_TEST(CV_mcc_perf, infer) {
// read gold chartsRGB
......
#include "perf_precomp.hpp"
CV_PERF_TEST_MAIN(mcc)
CV_PERF_TEST_MAIN(ccm)
......@@ -2,11 +2,10 @@
#define __OPENCV_PERF_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/mcc.hpp"
#include "opencv2/ccm.hpp"
namespace opencv_test
{
using namespace cv::mcc;
using namespace cv::ccm;
using namespace perf;
}
......
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/mcc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
using namespace mcc;
const char *about = "Basic chart detection";
const char *keys = {
"{ help h usage ? | | show this message }"
"{t | | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl }"
"{v | | Input from video file, if ommited, input comes from camera }"
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
"{nc | 1 | Maximum number of charts in the image }"};
int main(int argc, char *argv[])
{
// ----------------------------------------------------------
// Scroll down a bit (~40 lines) to find actual relevant code
// ----------------------------------------------------------
CommandLineParser parser(argc, argv, keys);
parser.about(about);
int t = parser.get<int>("t");
CV_Assert(0 <= t && t <= 2);
TYPECHART chartType = TYPECHART(t);
int camId = parser.get<int>("ci");
int nc = parser.get<int>("nc");
String video;
if (parser.has("v"))
video = parser.get<String>("v");
if (!parser.check())
{
parser.printErrors();
return 0;
}
VideoCapture inputVideo;
int waitTime;
if (!video.empty())
{
inputVideo.open(video);
waitTime = 10;
}
else
{
inputVideo.open(camId);
waitTime = 10;
}
//--------------------------------------------------------------------------
//-------------------------Actual Relevant Code-----------------------------
//--------------------------------------------------------------------------
while (inputVideo.grab())
{
Mat image, imageCopy;
inputVideo.retrieve(image);
imageCopy = image.clone();
Ptr<CCheckerDetector> detector = CCheckerDetector::create();
// Marker type to detect
if (!detector->process(image, chartType, nc))
{
printf("ChartColor not detected \n");
}
else
{
// get checker
std::vector<Ptr<mcc::CChecker>> checkers = detector->getListColorChecker();
for (Ptr<mcc::CChecker> checker : checkers)
{
// current checker
Ptr<CCheckerDraw> cdraw = CCheckerDraw::create(checker);
cdraw->draw(image);
}
}
imshow("image result | q or esc to quit", image);
imshow("original", imageCopy);
char key = (char)waitKey(waitTime);
if (key == 27)
break;
}
return 0;
}
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/mcc.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
using namespace std;
using namespace cv;
using namespace mcc;
const char *about = "Basic chart detection using neural network";
const char *keys = {
"{ help h usage ? | | show this message }"
"{t | 0 | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl, default:0}"
"{m | | File path of model, if you don't have the model you can \
find the link in the documentation}"
"{pb | | File path of pbtxt file, available along with with the model \
file }"
"{v | | Input from video file, if ommited, input comes from camera }"
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
"{nc | 1 | Maximum number of charts in the image }"
"{use_gpu | | Add this flag if you want to use gpu}"};
int main(int argc, char *argv[])
{
// ----------------------------------------------------------
// Scroll down a bit (~50 lines) to find actual relevant code
// ----------------------------------------------------------
CommandLineParser parser(argc, argv, keys);
parser.about(about);
if (parser.has("help"))
{
parser.printMessage();
return -1;
}
int t = parser.get<int>("t");
CV_Assert(0 <= t && t <= 2);
TYPECHART chartType = TYPECHART(t);
string model_path = parser.get<string>("m");
string pbtxt_path = parser.get<string>("pb");
int camId = parser.get<int>("ci");
int nc = parser.get<int>("nc");
String video;
if (parser.has("v"))
video = parser.get<String>("v");
bool use_gpu = parser.has("use_gpu");
if (!parser.check())
{
parser.printErrors();
return 0;
}
VideoCapture inputVideo;
int waitTime;
if (!video.empty())
{
inputVideo.open(video);
waitTime = 10;
}
else
{
inputVideo.open(camId);
waitTime = 10;
}
//--------------------------------------------------------------------------
//-------------------------Actual Relevant Code-----------------------------
//--------------------------------------------------------------------------
//load the network
cv::dnn::Net net = cv::dnn::readNetFromTensorflow(model_path, pbtxt_path);
if (use_gpu)
{
net.setPreferableBackend(dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(dnn::DNN_TARGET_CUDA);
}
Ptr<CCheckerDetector> detector = CCheckerDetector::create();
if (!detector->setNet(net))
{
cout << "Loading Model failed: Aborting" << endl;
return 0;
}
while (inputVideo.grab())
{
Mat image, imageCopy;
inputVideo.retrieve(image);
imageCopy = image.clone();
// Marker type to detect
if (!detector->process(image, chartType, nc, true))
{
printf("ChartColor not detected \n");
}
else
{
// get checker
std::vector<Ptr<mcc::CChecker>> checkers = detector->getListColorChecker();
for (Ptr<mcc::CChecker> checker : checkers)
{
// current checker
Ptr<CCheckerDraw> cdraw = CCheckerDraw::create(checker);
cdraw->draw(image);
}
}
imshow("image result | q or esc to quit", image);
imshow("original", imageCopy);
char key = (char)waitKey(waitTime);
if (key == 27)
break;
}
return 0;
}
......@@ -3,23 +3,17 @@
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/mcc.hpp>
#include <opencv2/ccm.hpp>
#include <iostream>
using namespace std;
using namespace cv;
using namespace mcc;
using namespace ccm;
using namespace std;
const char *about = "Basic chart detection";
const char *keys =
"{ help h | | show this message }"
"{t | | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl }"
"{v | | Input from video file, if ommited, input comes from camera }"
"{ci | 0 | Camera id if input doesnt come from video (-v) }"
"{f | 1 | Path of the file to process (-v) }"
"{nc | 1 | Maximum number of charts in the image }";
"{ f | 1 | Path of the file to process (-v) }";
int main(int argc, char *argv[])
{
......@@ -37,14 +31,8 @@ int main(int argc, char *argv[])
return 0;
}
int t = parser.get<int>("t");
int nc = parser.get<int>("nc");
string filepath = parser.get<string>("f");
CV_Assert(0 <= t && t <= 2);
TYPECHART chartType = TYPECHART(t);
if (!parser.check())
{
parser.printErrors();
......@@ -59,99 +47,111 @@ int main(int argc, char *argv[])
}
//! [get_messages_of_image]
Mat imageCopy = image.clone();
Ptr<CCheckerDetector> detector = CCheckerDetector::create();
// Marker type to detect
if (!detector->process(image, chartType, nc))
{
printf("ChartColor not detected \n");
return 2;
Mat src(24, 1, CV_64FC3);
// Hardcoded values. Image used: opencv_extra/testdata/cv/mcc/mcc_ccm_test.jpg
double values[24][3] = {
{0.380463, 0.31696, 0.210053},
{0.649781, 0.520561, 0.452553},
{0.323114, 0.37593, 0.50123},
{0.314785, 0.396522, 0.258116},
{0.452971, 0.418602, 0.578767},
{0.34908, 0.608649, 0.652283},
{0.691127, 0.517818, 0.144984},
{0.208668, 0.224391, 0.485851},
{0.657849, 0.378126, 0.304115},
{0.285762, 0.229671, 0.31913},
{0.513422, 0.685031, 0.337381},
{0.786459, 0.676133, 0.246303},
{0.11751, 0.135079, 0.383441},
{0.190745, 0.470513, 0.296844},
{0.587832, 0.299132, 0.196117},
{0.783908, 0.746261, 0.294357},
{0.615481, 0.359983, 0.471403},
{0.107095, 0.370516, 0.573142},
{0.708598, 0.718936, 0.740915},
{0.593812, 0.612474, 0.63222},
{0.489774, 0.510077, 0.521757},
{0.380591, 0.398499, 0.393662},
{0.27461, 0.293267, 0.275244},
{0.180753, 0.194968, 0.145006}
};
// Assign values to src
for (int i = 0; i < 24; i++) {
src.at<cv::Vec3d>(i, 0) = cv::Vec3d(values[i][0], values[i][1], values[i][2]);
}
//! [get_color_checker]
vector<Ptr<mcc::CChecker>> checkers = detector->getListColorChecker();
//! [get_color_checker]
for (Ptr<mcc::CChecker> checker : checkers)
{
//! [create]
Ptr<CCheckerDraw> cdraw = CCheckerDraw::create(checker);
cdraw->draw(image);
Mat chartsRGB = checker->getChartsRGB();
Mat src = chartsRGB.col(1).clone().reshape(3, chartsRGB.rows/3);
src /= 255.0;
//! [create]
//compte color correction matrix
//! [get_ccm_Matrix]
ColorCorrectionModel model1(src, COLORCHECKER_Vinyl);
model1.run();
Mat ccm = model1.getCCM();
std::cout<<"ccm "<<ccm<<std::endl;
double loss = model1.getLoss();
std::cout<<"loss "<<loss<<std::endl;
//! [get_ccm_Matrix]
/* brief More models with different parameters, try it & check the document for details.
*/
// model1.setColorSpace(COLOR_SPACE_sRGB);
// model1.setCCM_TYPE(CCM_3x3);
// model1.setDistance(DISTANCE_CIE2000);
// model1.setLinear(LINEARIZATION_GAMMA);
// model1.setLinearGamma(2.2);
// model1.setLinearDegree(3);
// model1.setSaturatedThreshold(0, 0.98);
/* If you use a customized ColorChecker, you can use your own reference color values and corresponding color space in a way like:
*/
//! [reference_color_values]
// cv::Mat ref = (Mat_<Vec3d>(18, 1) <<
// Vec3d(100, 0.00520000001, -0.0104),
// Vec3d(73.0833969, -0.819999993, -2.02099991),
// Vec3d(62.493, 0.425999999, -2.23099995),
// Vec3d(50.4640007, 0.446999997, -2.32399988),
// Vec3d(37.7970009, 0.0359999985, -1.29700005),
// Vec3d(0, 0, 0),
// Vec3d(51.5880013, 73.5179977, 51.5690002),
// Vec3d(93.6989975, -15.7340002, 91.9420013),
// Vec3d(69.4079971, -46.5940018, 50.4869995),
// Vec3d(66.61000060000001, -13.6789999, -43.1720009),
// Vec3d(11.7110004, 16.9799995, -37.1759987),
// Vec3d(51.973999, 81.9440002, -8.40699959),
// Vec3d(40.5489998, 50.4399986, 24.8490009),
// Vec3d(60.8160019, 26.0690002, 49.4420013),
// Vec3d(52.2529984, -19.9500008, -23.9960003),
// Vec3d(51.2859993, 48.4700012, -15.0579996),
// Vec3d(68.70700069999999, 12.2959995, 16.2129993),
// Vec3d(63.6839981, 10.2930002, 16.7639999));
// ColorCorrectionModel model8(src,ref,COLOR_SPACE_Lab_D50_2);
// model8.run();
//! [reference_color_values]
//! [make_color_correction]
Mat img_;
cvtColor(image, img_, COLOR_BGR2RGB);
img_.convertTo(img_, CV_64F);
const int inp_size = 255;
const int out_size = 255;
img_ = img_ / inp_size;
Mat calibratedImage= model1.infer(img_);
Mat out_ = calibratedImage * out_size;
//! [make_color_correction]
//! [Save_calibrated_image]
// Save the calibrated image to {FILE_NAME}.calibrated.{FILE_EXT}
out_.convertTo(out_, CV_8UC3);
Mat img_out = min(max(out_, 0), out_size);
Mat out_img;
cvtColor(img_out, out_img, COLOR_RGB2BGR);
string filename = filepath.substr(filepath.find_last_of('/')+1);
size_t dotIndex = filename.find_last_of('.');
string baseName = filename.substr(0, dotIndex);
string ext = filename.substr(dotIndex+1, filename.length()-dotIndex);
string calibratedFilePath = baseName + ".calibrated." + ext;
imwrite(calibratedFilePath, out_img);
//! [Save_calibrated_image]
}
//compte color correction matrix
//! [get_ccm_Matrix]
ColorCorrectionModel model1(src, COLORCHECKER_Macbeth);
model1.run();
Mat ccm = model1.getCCM();
std::cout<<"ccm "<<ccm<<std::endl;
double loss = model1.getLoss();
std::cout<<"loss "<<loss<<std::endl;
//! [get_ccm_Matrix]
/* brief More models with different parameters, try it & check the document for details.
*/
// model1.setColorSpace(COLOR_SPACE_sRGB);
// model1.setCCM_TYPE(CCM_3x3);
// model1.setDistance(DISTANCE_CIE2000);
// model1.setLinear(LINEARIZATION_GAMMA);
// model1.setLinearGamma(2.2);
// model1.setLinearDegree(3);
// model1.setSaturatedThreshold(0, 0.98);
/* If you use a customized ColorChecker, you can use your own reference color values and corresponding color space in a way like:
*/
//! [reference_color_values]
// cv::Mat ref = (Mat_<Vec3d>(18, 1) <<
// Vec3d(100, 0.00520000001, -0.0104),
// Vec3d(73.0833969, -0.819999993, -2.02099991),
// Vec3d(62.493, 0.425999999, -2.23099995),
// Vec3d(50.4640007, 0.446999997, -2.32399988),
// Vec3d(37.7970009, 0.0359999985, -1.29700005),
// Vec3d(0, 0, 0),
// Vec3d(51.5880013, 73.5179977, 51.5690002),
// Vec3d(93.6989975, -15.7340002, 91.9420013),
// Vec3d(69.4079971, -46.5940018, 50.4869995),
// Vec3d(66.61000060000001, -13.6789999, -43.1720009),
// Vec3d(11.7110004, 16.9799995, -37.1759987),
// Vec3d(51.973999, 81.9440002, -8.40699959),
// Vec3d(40.5489998, 50.4399986, 24.8490009),
// Vec3d(60.8160019, 26.0690002, 49.4420013),
// Vec3d(52.2529984, -19.9500008, -23.9960003),
// Vec3d(51.2859993, 48.4700012, -15.0579996),
// Vec3d(68.70700069999999, 12.2959995, 16.2129993),
// Vec3d(63.6839981, 10.2930002, 16.7639999));
// ColorCorrectionModel model8(src,ref,COLOR_SPACE_Lab_D50_2);
// model8.run();
//! [reference_color_values]
//! [make_color_correction]
Mat img_;
cvtColor(image, img_, COLOR_BGR2RGB);
img_.convertTo(img_, CV_64F);
const int inp_size = 255;
const int out_size = 255;
img_ = img_ / inp_size;
Mat calibratedImage= model1.infer(img_);
Mat out_ = calibratedImage * out_size;
//! [make_color_correction]
//! [Save_calibrated_image]
// Save the calibrated image to {FILE_NAME}.calibrated.{FILE_EXT}
out_.convertTo(out_, CV_8UC3);
Mat img_out = min(max(out_, 0), out_size);
Mat out_img;
cvtColor(img_out, out_img, COLOR_RGB2BGR);
string filename = filepath.substr(filepath.find_last_of('/')+1);
size_t dotIndex = filename.find_last_of('.');
string baseName = filename.substr(0, dotIndex);
string ext = filename.substr(dotIndex+1, filename.length()-dotIndex);
string calibratedFilePath = baseName + ".calibrated." + ext;
imwrite(calibratedFilePath, out_img);
//! [Save_calibrated_image]
return 0;
}
......
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "precomp.hpp"
#include "bound_min.hpp"
namespace cv
{
namespace mcc
{
CBoundMin::CBoundMin()
{
}
CBoundMin::~CBoundMin()
{
}
void CBoundMin::calculate()
{
corners.clear();
size_t N = chart.size();
if (!N)
return;
std::vector<cv::Point2f> X(4 * N);
for (size_t i = 0; i < N; i++)
{
mcc::CChart cc = chart[i];
for (size_t j = 0; j < 4; j++)
{
X[i * 4 + j] = cc.corners[j];
}
}
// media
cv::Point2f mu(0, 0);
for (size_t i = 0; i < 4 * N; i++)
mu += X[i];
mu /= (4 * (int)N);
for (size_t i = 0; i < 4 * N; i++)
X[i] -= mu;
// calculate all line
std::vector<cv::Point3f> L;
L.resize(4 * N);
for (size_t i = 0; i < N; i++)
{
cv::Point3f v0, v1, v2, v3;
v0.x = X[4 * i + 0].x;
v0.y = X[4 * i + 0].y;
v0.z = 1;
v1.x = X[4 * i + 1].x;
v1.y = X[4 * i + 1].y;
v1.z = 1;
v2.x = X[4 * i + 2].x;
v2.y = X[4 * i + 2].y;
v2.z = 1;
v3.x = X[4 * i + 3].x;
v3.y = X[4 * i + 3].y;
v3.z = 1;
L[4 * i + 0] = v0.cross(v1);
L[4 * i + 1] = v1.cross(v2);
L[4 * i + 2] = v2.cross(v3);
L[4 * i + 3] = v3.cross(v0);
}
// line convex hull
std::vector<int> dist;
dist.resize(4 * N);
cv::Point2f n;
float d;
for (size_t i = 0; i < 4 * N; i++)
{
n.x = L[i].x;
n.y = L[i].y;
d = L[i].z;
int s = 0;
for (size_t j = 0; j < N; j++)
s += (X[j].dot(n) + d) <= 0;
dist[i] = s;
}
// sort
std::vector<int> idx;
std::vector<cv::Point3f> Ls;
Ls.resize(4 * N);
mcc::sort(dist, idx);
for (size_t i = 0; i < 4 * N; i++)
Ls[i] = L[idx[i]];
std::vector<cv::Point3f> Lc;
Lc.resize(4 * N);
Lc[0] = Ls[0];
cv::Point3f ln;
int j, k = 0;
for (size_t i = 0; i < 4 * N; i++)
{
ln = Ls[i]; //current line
if (!validateLine(Lc, ln, k, j))
{
Lc[k] = ln;
k++;
}
else if ((abs(Lc[j].z) < abs(ln.z)) && (abs(dist[i] - dist[j]) < 2))
{
Lc[j] = ln;
}
if (k == 4 && abs(dist[i] - dist[k - 1]) > 2)
break;
}
if (k < 4)
return;
std::vector<float> thetas;
thetas.resize(4);
for (size_t i = 0; i < 4; i++)
thetas[i] = atan2(Lc[i].y / Lc[i].z, Lc[i].x / Lc[i].z);
sort(thetas, idx, false);
std::vector<cv::Point3f> lines;
lines.resize(4);
for (size_t i = 0; i < 4; i++)
lines[i] = Lc[idx[i]];
cv::Point3f Vcart;
cv::Point2f Vhom;
std::vector<cv::Point2f> V;
V.resize(4);
for (size_t i = 0; i < 4; i++)
{
j = (i + 1) % 4;
Vcart = lines[i].cross(lines[j]);
Vhom.x = Vcart.x / Vcart.z;
Vhom.y = Vcart.y / Vcart.z;
V[i] = Vhom + mu;
}
corners = V;
}
} // namespace mcc
} // namespace cv
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _MCC_BOUND_MIN_HPP
#define _MCC_BOUND_MIN_HPP
#include "charts.hpp"
namespace cv
{
namespace mcc
{
class CBoundMin
{
public:
CBoundMin();
~CBoundMin();
void setCharts(const std::vector<CChart> &chartIn) { chart = chartIn; }
void getCorners(std::vector<cv::Point2f> &cornersOut) { cornersOut = corners; }
void calculate();
private:
std::vector<CChart> chart;
std::vector<cv::Point2f> corners;
private:
bool validateLine(const std::vector<cv::Point3f> &Lc, cv::Point3f ln,
int k, int &j)
{
double theta;
cv::Point2d v0, v1;
for (j = 0; j < k; j++)
{
v0.x = Lc[j].x;
v0.y = Lc[j].y;
v1.x = ln.x;
v1.y = ln.y;
theta = v0.dot(v1) / (norm(v0) * norm(v1));
theta = acos(theta);
if (theta < 0.5)
return true;
}
return false;
}
};
} // namespace mcc
} // namespace cv
#endif //_MCC_BOUND_MIN_HPP
......@@ -25,7 +25,7 @@
// Jinheng Zhang <zhangjinheng1@huawei.com>
// Chenqi Shan <shanchenqi@huawei.com>
#include "opencv2/mcc/ccm.hpp"
#include "opencv2/ccm.hpp"
#include "linearize.hpp"
namespace cv {
namespace ccm {
......
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "precomp.hpp"
#include "charts.hpp"
namespace cv
{
namespace mcc
{
CChart::CChart()
: perimetro(0), area(0), large_side(0)
{
}
CChart::~CChart()
{
}
void CChart::
setCorners(std::vector<cv::Point2f> p)
{
cv::Point v1, v2;
if (p.empty())
return;
// copy
corners = p;
// Sort the corners in anti-clockwise order
polyanticlockwise(corners);
// Properties
area = cv::contourArea(corners);
perimetro = perimeter(corners);
center = mace_center(corners);
v1 = corners[2] - corners[0];
v2 = corners[3] - corners[1];
large_side = std::max(cv::norm(v1), cv::norm(v2));
}
//////////////////////////////////////////////////////////////////////////////////////////////
CChartDraw::
CChartDraw(CChart &pChart, InputOutputArray image)
: m_pChart(pChart), m_image(image.getMat())
{
}
void CChartDraw::
drawContour(cv::Scalar color /*= CV_RGB(0, 250, 0)*/) const
{
//Draw lines
int thickness = 2;
cv::line(m_image, (m_pChart).corners[0], (m_pChart).corners[1], color, thickness, LINE_AA);
cv::line(m_image, (m_pChart).corners[1], (m_pChart).corners[2], color, thickness, LINE_AA);
cv::line(m_image, (m_pChart).corners[2], (m_pChart).corners[3], color, thickness, LINE_AA);
cv::line(m_image, (m_pChart).corners[3], (m_pChart).corners[0], color, thickness, LINE_AA);
}
void CChartDraw::
drawCenter(cv::Scalar color /*= CV_RGB(0, 0, 255)*/) const
{
int radius = 3;
int thickness = 2;
cv::circle(m_image, (m_pChart).center, radius, color, thickness);
}
} // namespace mcc
} // namespace cv
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _MCC_CHARTS_HPP
#define _MCC_CHARTS_HPP
namespace cv
{
namespace mcc
{
/** \brief Chart model
*
* .--------. <- px,py
* | | one chart for checker color
* | RGB |
* | Lab |
* | |
* .--------.
*
* \author Pedro Marrero Fernndez
*/
class CChart
{
public:
CChart();
~CChart();
/**\brief set corners
*\param p[in] new corners
*/
void setCorners(std::vector<cv::Point2f> p);
public:
std::vector<cv::Point2f> corners;
cv::Point2f center;
double perimetro;
double area;
double large_side;
};
/** \brief Chart draw */
class CChartDraw
{
public:
/**\brief contructor */
CChartDraw(CChart &pChart, InputOutputArray image);
/**\brief draw the chart contour over the image */
void drawContour(cv::Scalar color = CV_RGB(0, 250, 0)) const;
/**\brief draw the chart center over the image */
void drawCenter(cv::Scalar color = CV_RGB(0, 0, 255)) const;
private:
CChart &m_pChart;
cv::Mat m_image;
};
} // namespace mcc
} // namespace cv
#endif //_MCC_CHARTS_HPP
此差异已折叠。
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _MCC_CHECKER_DETECTOR_HPP
#define _MCC_CHECKER_DETECTOR_HPP
#include "opencv2/mcc.hpp"
#include "charts.hpp"
namespace cv
{
namespace mcc
{
class CCheckerDetectorImpl : public CCheckerDetector
{
typedef std::vector<cv::Point> PointsVector;
typedef std::vector<PointsVector> ContoursVector;
public:
CCheckerDetectorImpl();
virtual ~CCheckerDetectorImpl();
bool setNet(cv::dnn::Net _net) CV_OVERRIDE;
bool process(InputArray image, const TYPECHART chartType,
const std::vector<cv::Rect> &regionsOfInterest,
const int nc = 1, bool use_net = false,
const Ptr<DetectorParameters> &params = DetectorParameters::create()) CV_OVERRIDE;
bool process(InputArray image, const TYPECHART chartType,
const int nc = 1, bool use_net = false,
const Ptr<DetectorParameters> &params = DetectorParameters::create()) CV_OVERRIDE;
Ptr<CChecker> getBestColorChecker() CV_OVERRIDE
{
if (m_checkers.size())
return m_checkers[0];
return nullptr;
}
std::vector<Ptr<CChecker>> getListColorChecker() CV_OVERRIDE
{
return m_checkers;
}
protected: // methods pipeline
bool _no_net_process(InputArray image, const TYPECHART chartType,
const int nc,
const Ptr<DetectorParameters> &params,
std::vector<cv::Rect> regionsOfInterest);
/// prepareImage
/** \brief Prepare Image
* \param[in] bgrMat image in color space BGR
* \param[out] grayOut gray scale
* \param[out] bgrOut rescale image
* \param[out] aspOut aspect ratio
* \param[in] max_size rescale factor in max dim
*/
virtual void
prepareImage(InputArray bgr, OutputArray grayOut, OutputArray bgrOut, float &aspOut, const Ptr<DetectorParameters> &params) const;
/// performThreshold
/** \brief Adaptative threshold
* \param[in] grayscaleImg gray scale image
* \param[in] thresholdImg binary image
* \param[in] wndx, wndy windows size
* \param[in] step
*/
virtual void
performThreshold(InputArray grayscaleImg, OutputArrayOfArrays thresholdImg, const Ptr<DetectorParameters> &params) const;
/// findContours
/** \brief find contour in the image
* \param[in] srcImg binary imagen
* \param[out] contours
* \param[in] minContourPointsAllowed
*/
virtual void
findContours(InputArray srcImg, ContoursVector &contours, const Ptr<DetectorParameters> &params) const;
/// findCandidates
/** \brief find posibel candidates
* \param[in] contours
* \param[out] detectedCharts
* \param[in] minContourLengthAllowed
*/
virtual void
findCandidates(const ContoursVector &contours, std::vector<CChart> &detectedCharts, const Ptr<DetectorParameters> &params);
/// clustersAnalysis
/** \brief clusters charts analysis
* \param[in] detectedCharts
* \param[out] groups
*/
virtual void
clustersAnalysis(const std::vector<CChart> &detectedCharts, std::vector<int> &groups, const Ptr<DetectorParameters> &params);
/// checkerRecognize
/** \brief checker color recognition
* \param[in] img
* \param[in] detectedCharts
* \param[in] G
* \param[out] colorChartsOut
*/
virtual void
checkerRecognize(InputArray img, const std::vector<CChart> &detectedCharts, const std::vector<int> &G,
const TYPECHART chartType, std::vector<std::vector<cv::Point2f>> &colorChartsOut,
const Ptr<DetectorParameters> &params);
/// checkerAnalysis
/** \brief evaluate checker
* \param[in] img
* \param[in] img_org
* \param[in] colorCharts
* \param[out] checker
* \param[in] asp
*/
virtual void
checkerAnalysis(InputArray img_rgb_f,
const TYPECHART chartType, const unsigned int nc,
const std::vector<std::vector<cv::Point2f>> &colorCharts,
std::vector<Ptr<CChecker>> &checkers, float asp,
const Ptr<DetectorParameters> &params,
const cv::Mat &img_rgb_org,
const cv::Mat &img_ycbcr_org,
std::vector<cv::Mat> &rgb_planes,
std::vector<cv::Mat> &ycbcr_planes);
virtual void
removeTooCloseDetections(const Ptr<DetectorParameters> &params);
protected:
std::vector<Ptr<CChecker>> m_checkers;
dnn::Net net;
bool net_used = false;
private: // methods aux
void get_subbox_chart_physical(
const std::vector<cv::Point2f> &points,
std::vector<cv::Point2f> &chartPhy);
void reduce_array(
const std::vector<float> &x,
std::vector<float> &x_new,
float tol);
void transform_points_inverse(
InputArray T,
const std::vector<cv::Point2f> &X,
std::vector<cv::Point2f> &Xt);
void get_profile(
const std::vector<cv::Point2f> &ibox,
const TYPECHART chartType,
OutputArray charts_rgb,
OutputArray charts_ycbcr,
InputArray im_rgb,
InputArray im_ycbcr,
std::vector<cv::Mat> &rgb_planes,
std::vector<cv::Mat> &ycbcr_planes);
/* \brief cost function
* e(p) = ||f(p)||^2 = \sum_k (mu_{k,p}*r_k')/||mu_{k,p}||||r_k|| + ...
* + \sum_k || \sigma_{k,p} ||^2
*/
float cost_function(InputArray img, InputOutputArray mask, InputArray lab,
const std::vector<cv::Point2f> &ibox,
const TYPECHART chartType);
};
} // namespace mcc
} // namespace cv
#endif //_MCC_CHECKER_DETECTOR_HPP
// 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.
/*
* MIT License
*
* Copyright (c) 2018 Pedro Diamel Marrero Fernández
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "precomp.hpp"
#include "checker_model.hpp"
#include "dictionary.hpp"
namespace cv
{
namespace mcc
{
///////////////////////////////////////////////////////////////////////////////
/// CChartModel
CChartModel::CChartModel(const TYPECHART chartType)
{
switch (chartType)
{
case MCC24: //Standard
size = cv::Size2i(4, 6);
boxsize = cv::Size2f(11.25, 16.75);
box.resize(4);
box[0] = cv::Point2f(0.00, 0.00);
box[1] = cv::Point2f(16.75, 0.00);
box[2] = cv::Point2f(16.75, 11.25);
box[3] = cv::Point2f(0.00, 11.25);
cellchart.assign(CChartClassicModelCellchart, CChartClassicModelCellchart + 4 * 24);
center.assign(CChartClassicModelCenter, CChartClassicModelCenter + 24);
chart.resize(size.area(), std::vector<float>(9));
for(int color = 0 ; color < (int)chart.size() ; color++)
chart[color].assign(CChartClassicModelColors[color] , CChartClassicModelColors[color] + 9 );
break;
case SG140: //DigitalSG
size = cv::Size2i(10, 14);
boxsize = cv::Size2f(27.75, 38.75);
box.resize(4);
box[0] = cv::Point2f(0.00, 0.00);
box[1] = cv::Point2f(38.75, 0.00);
box[2] = cv::Point2f(38.75, 27.75);
box[3] = cv::Point2f(0.00, 27.75);
cellchart.assign(CChartDigitalSGCellchart, CChartDigitalSGCellchart + 4 * 140);
center.assign(CChartDigitalSGCenter, CChartDigitalSGCenter + 140);
chart.resize(size.area(), std::vector<float>(9));
for(int color = 0 ; color < (int)chart.size() ; color++)
chart[color].assign(CChartDigitalSGColors[color] , CChartDigitalSGColors[color] + 9 );
break;
case VINYL18: //Vinyl
size = cv::Size2i(3, 6);
boxsize = cv::Size2f(12.50, 18.25);
box.resize(4);
box[0] = cv::Point2f(0.00, 0.00);
box[1] = cv::Point2f(18.25, 0.00);
box[2] = cv::Point2f(18.25, 12.50);
box[3] = cv::Point2f(0.00, 12.50);
cellchart.assign(CChartVinylCellchart, CChartVinylCellchart + 4 * 18);
center.assign(CChartVinylCenter, CChartVinylCenter + 18);
chart.resize(size.area(), std::vector<float>(9));
for(int color = 0 ; color < (int)chart.size() ; color++)
chart[color].assign(CChartVinylColors[color] , CChartVinylColors[color] + 9 );
break;
}
}
CChartModel::~CChartModel()
{
}
bool CChartModel::
evaluate(const SUBCCMModel &subModel, int &offset, int &iTheta, float &error)
{
float tError;
int tTheta, tOffset;
error = INFINITY;
bool beval = false;
// para todas las orientaciones
// min_{ theta,dt } | CC_e - CC |
for (tTheta = 0; tTheta < 8; tTheta++)
{
if (match(subModel, tTheta, tError, tOffset) && tError < error)
{
error = tError;
iTheta = tTheta;
offset = tOffset;
beval = true;
}
}
return beval;
}
void CChartModel::
copyToColorMat(OutputArray lab, int cs)
{
size_t N, M, k;
N = size.width;
M = size.height;
cv::Mat im_lab_org((int)N, (int)M, CV_32FC3);
int type_color = 3 * cs;
k = 0;
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < M; j++)
{
cv::Vec3f &lab_values = im_lab_org.at<cv::Vec3f>((int)i, (int)j);
lab_values[0] = chart[k][type_color + 0];
lab_values[1] = chart[k][type_color + 1];
lab_values[2] = chart[k][type_color + 2];
k++;
}
}
lab.assign(im_lab_org);
}
void mcc::CChartModel::
rotate90()
{
size = cv::Size2i(size.height, size.width);
//the matrix is roated clockwise 90 degree, so first row will become last column, second row second last column and so on
//doing this inplace will make the code a bit hard to read, so creating a temporary array
std::vector<cv::Point2f> _cellchart(cellchart.size());
std::vector<cv::Point2f> _center(center.size());
int k = 0;
for (int i = 0; i < size.width; i++)
{
for (int j = 0; j < size.height; j++)
{
//k contains the new coordintes,
int old_i = size.height - j - 1;
int old_j = i;
int old_k = (old_i)*size.width + old_j;
_cellchart[4 * k + 0] = cellchart[4 * old_k + 3];
_cellchart[4 * k + 1] = cellchart[4 * old_k + 0];
_cellchart[4 * k + 2] = cellchart[4 * old_k + 1];
_cellchart[4 * k + 3] = cellchart[4 * old_k + 2];
// center
_center[k] = center[old_k];
k++;
}
}
cellchart = _cellchart;
center = _center;
boxsize = cv::Size2f(boxsize.height, boxsize.width);
}
void mcc::CChartModel::
flip()
{
std::vector<cv::Point2f> _cellchart(cellchart.size());
std::vector<cv::Point2f> _center(center.size());
int k = 0;
for (int i = 0; i < size.width; i++)
{
for (int j = 0; j < size.height; j++)
{
//k contains the new coordintes,
int old_i = i;
int old_j = size.height - j - 1;
int old_k = (old_i)*size.height + old_j;
_cellchart[4 * k + 0] = cellchart[4 * old_k + 1];
_cellchart[4 * k + 1] = cellchart[4 * old_k + 0];
_cellchart[4 * k + 2] = cellchart[4 * old_k + 3];
_cellchart[4 * k + 3] = cellchart[4 * old_k + 2];
// center
_center[k] = center[old_k];
k++;
}
}
cellchart = _cellchart;
center = _center;
}
float CChartModel::
dist_color_lab(InputArray lab1, InputArray lab2)
{
int N = lab1.rows();
float dist = 0, dist_i;
Mat _lab1 = lab1.getMat(), _lab2 = lab2.getMat();
for (int i = 0; i < N; i++)
{
cv::Vec3f v1 = _lab1.at<cv::Vec3f>(i, 0);
cv::Vec3f v2 = _lab2.at<cv::Vec3f>(i, 0);
// v1[0] = 1;
// v2[0] = 1; // L <- 0
// euclidean
cv::Vec3f v = v1 - v2;
dist_i = v.dot(v);
dist += sqrt(dist_i);
// cosine
//float costh = v1.dot(v2) / (norm(v1)*norm(v2));
//dist += 1 - (1 + costh) / 2;
}
dist /= N;
return dist;
}
bool CChartModel::
match(const SUBCCMModel &subModel, int iTheta, float &error, int &ierror)
{
size_t N, M, k;
N = size.width;
M = size.height;
cv::Mat im_lab_org((int)N, (int)M, CV_32FC3);
int type_color = 3;
k = 0;
for (size_t i = 0; i < N; i++)
{
for (size_t j = 0; j < M; j++)
{
cv::Vec3f &lab = im_lab_org.at<cv::Vec3f>((int)i, (int)j);
lab[0] = chart[k][type_color + 0];
lab[1] = chart[k][type_color + 1];
lab[2] = chart[k][type_color + 2];
k++;
}
}
rot90(im_lab_org, iTheta);
N = im_lab_org.rows;
M = im_lab_org.cols;
size_t n, m;
n = subModel.color_size.height;
m = subModel.color_size.width;
// boundary condition
if (N < n || M < m)
return false;
// rgb to La*b*
cv::Mat rgb_est = subModel.sub_chart;
cv::Mat lab_est;
// RGB color space
//cv::cvtColor(rgb_est, lab_est, COLOR_BGR2RGB);
// Lab color space
//rgb_est *= 1/255;
cv::cvtColor(rgb_est, lab_est, COLOR_BGR2Lab);
size_t nN, mM;
nN = N - n + 1;
mM = M - m + 1;
std::vector<float> lEcm(nN * mM);
k = 0;
for (size_t i = 0; i < nN; i++)
{
for (size_t j = 0; j < mM; j++)
{
cv::Mat lab_curr, lab_roi;
lab_roi = im_lab_org(cv::Rect((int)j, (int)i, (int)m, (int)n));
lab_roi.copyTo(lab_curr);
lab_curr = lab_curr.t();
lab_curr = lab_curr.reshape(3, (int)n * (int)m);
// Mean squared error
// ECM = 1 / N sum_i(Y - Yp) ^ 2
lEcm[k] = dist_color_lab(lab_curr, lab_est) / (M * N);
k++;
}
}
// minimo
error = lEcm[0];
ierror = 0;
for (int i = 1; i < (int)lEcm.size(); i++)
if (error > lEcm[i])
{
error = lEcm[i];
ierror = i;
}
return true;
}
void CChartModel::
rot90(InputOutputArray mat, int itheta)
{
//1=CW, 2=CCW, 3=180
switch (itheta)
{
case 1: //transpose+flip(1)=CW
transpose(mat, mat);
cv::flip(mat, mat, 1);
break;
case 2: //flip(-1)=180
cv::flip(mat, mat, -1);
break;
case 3: //transpose+flip(0)=CCW
transpose(mat, mat);
cv::flip(mat, mat, 0);
break;
//flipped images start here
case 4: //flip(1)=no rotation, just flipped
cv::flip(mat, mat, 1);
break;
case 5: //flip(1)+transpose + flip(1)=CW
cv::flip(mat, mat, 1);
transpose(mat, mat);
cv::flip(mat, mat, 1);
break;
case 6: //flip(1)+flip(-1)=180
cv::flip(mat, mat, 1);
cv::flip(mat, mat, -1);
break;
case 7: //flip(1)+transpose+flip(0)=CCW
cv::flip(mat, mat, 1);
transpose(mat, mat);
cv::flip(mat, mat, 0);
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// // CChecker
Ptr<CChecker> CChecker::create()
{
return makePtr<CCheckerImpl>();
}
void CCheckerImpl::setTarget(TYPECHART _target)
{
target = _target;
}
void CCheckerImpl::setBox(std::vector<Point2f> _box)
{
box = _box;
}
void CCheckerImpl::setChartsRGB(Mat _chartsRGB)
{
chartsRGB = _chartsRGB;
}
void CCheckerImpl::setChartsYCbCr(Mat _chartsYCbCr)
{
chartsYCbCr = _chartsYCbCr;
}
void CCheckerImpl::setCost(float _cost)
{
cost = _cost;
}
void CCheckerImpl::setCenter(Point2f _center)
{
center = _center;
}
TYPECHART CCheckerImpl::getTarget()
{
return target;
}
std::vector<Point2f> CCheckerImpl::getBox()
{
return box;
}
std::vector<Point2f> CCheckerImpl::getColorCharts()
{
// color chart classic model
CChartModel cccm(getTarget());
Mat lab;
size_t N;
std::vector<Point2f> fbox = cccm.box;
std::vector<Point2f> cellchart = cccm.cellchart;
std::vector<Point2f> charts(cellchart.size());
// tranformation
Matx33f ccT = getPerspectiveTransform(fbox, getBox());
std::vector<Point2f> bch(4), bcht(4);
N = cellchart.size() / 4;
for (size_t i = 0, k; i < N; i++)
{
k = 4 * i;
for (size_t j = 0ull; j < 4ull; j++)
bch[j] = cellchart[k + j];
polyanticlockwise(bch);
transform_points_forward(ccT, bch, bcht);
Point2f c(0, 0);
for (size_t j = 0; j < 4; j++)
c += bcht[j];
c /= 4;
for (size_t j = 0ull; j < 4ull; j++)
charts[k+j] = ((bcht[j] - c) * 0.50) + c;
}
return charts;
}
Mat CCheckerImpl::getChartsRGB()
{
return chartsRGB;
}
Mat CCheckerImpl::getChartsYCbCr()
{
return chartsYCbCr;
}
float CCheckerImpl::getCost()
{
return cost;
}
Point2f CCheckerImpl::getCenter()
{
return center;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// CheckerDraw
Ptr<CCheckerDraw> CCheckerDraw::create(Ptr<CChecker> pChecker, cv::Scalar color /*= CV_RGB(0,250,0)*/, int thickness /*=2*/)
{
return makePtr<CCheckerDrawImpl>(pChecker, color, thickness);
}
void CCheckerDrawImpl::draw(InputOutputArray img)
{
std::vector<Point2f> charts = m_pChecker->getColorCharts();
size_t N = charts.size() / 4;
for (size_t i = 0, k; i < N; i++)
{
k = 4 * i;
for (size_t j = 0; j < 4; j++)
cv::line(img, charts[k+j], charts[k+((j + 1) % 4)], m_color, m_thickness, LINE_AA);
}
}
void transform_points_forward(const Matx33f& T, const std::vector<Point2f> &X, std::vector<Point2f> &Xt)
{
size_t N = X.size();
if (Xt.size() != N)
Xt.resize(N);
std::fill(Xt.begin(), Xt.end(), Point2f(0.f, 0.f));
if (N == 0)
return;
Matx31f p, xt;
Point2f pt;
for (size_t i = 0; i < N; i++)
{
p(0, 0) = X[i].x;
p(1, 0) = X[i].y;
p(2, 0) = 1;
xt = T * p;
pt.x = xt(0, 0) / xt(2, 0);
pt.y = xt(1, 0) / xt(2, 0);
Xt[i] = pt;
}
}
} // namespace mcc
} // namespace cv
支持 Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册