Skip to content

Commit

Permalink
Merge pull request #3645 from AleksandrPanov:update_mcc_CCHecker
Browse files Browse the repository at this point in the history
added getColorCharts()
  • Loading branch information
asmorkalov authored Mar 26, 2024
2 parents 5e592c2 + 45f560b commit c8c750a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 79 deletions.
9 changes: 9 additions & 0 deletions modules/mcc/include/opencv2/mcc/checker_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class CV_EXPORTS_W CChecker

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;
Expand Down
25 changes: 0 additions & 25 deletions modules/mcc/src/checker_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1177,31 +1177,6 @@ void CCheckerDetectorImpl::
x_new.insert(x_new.begin() + idx + 1, (x_new[idx] + x_new[idx + 1]) / 2);
}

void CCheckerDetectorImpl::
transform_points_forward(InputArray T, const std::vector<cv::Point2f> &X, std::vector<cv::Point2f> &Xt)
{
size_t N = X.size();
if (N == 0)
return;

Xt.clear();
Xt.resize(N);
cv::Matx31f p, xt;
cv::Point2f pt;

cv::Matx33f _T = T.getMat();
for (int i = 0; i < (int)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;
}
}

void CCheckerDetectorImpl::
transform_points_inverse(InputArray T, const std::vector<cv::Point2f> &X, std::vector<cv::Point2f> &Xt)
{
Expand Down
5 changes: 0 additions & 5 deletions modules/mcc/src/checker_detector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,6 @@ class CCheckerDetectorImpl : public CCheckerDetector
std::vector<float> &x_new,
float tol);

void transform_points_forward(
InputArray T,
const std::vector<cv::Point2f> &X,
std::vector<cv::Point2f> &Xt);

void transform_points_inverse(
InputArray T,
const std::vector<cv::Point2f> &X,
Expand Down
87 changes: 45 additions & 42 deletions modules/mcc/src/checker_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,39 @@ 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;
Expand All @@ -435,70 +468,40 @@ Ptr<CCheckerDraw> CCheckerDraw::create(Ptr<CChecker> pChecker, cv::Scalar color
return makePtr<CCheckerDrawImpl>(pChecker, color, thickness);
}

void CCheckerDrawImpl::
draw(InputOutputArray img)
void CCheckerDrawImpl::draw(InputOutputArray img)
{

// color chart classic model
CChartModel cccm(m_pChecker->getTarget());
cv::Mat lab;
size_t N;
std::vector<cv::Point2f> fbox = cccm.box;
std::vector<cv::Point2f> cellchart = cccm.cellchart;

// tranformation
cv::Matx33f ccT = cv::getPerspectiveTransform(fbox, m_pChecker->getBox());

std::vector<cv::Point2f> bch(4), bcht(4);
N = cellchart.size() / 4;
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;
bch[0] = cellchart[k + 0];
bch[1] = cellchart[k + 1];
bch[2] = cellchart[k + 2];
bch[3] = cellchart[k + 3];

polyanticlockwise(bch);
transform_points_forward(ccT, bch, bcht);

cv::Point2f c(0, 0);
for (size_t j = 0; j < 4; j++)
c += bcht[j];
c /= 4;
for (size_t j = 0; j < 4; j++)
bcht[j] = ((bcht[j] - c) * 0.50) + c;

cv::line(img, bcht[0], bcht[1], m_color, m_thickness, LINE_AA);
cv::line(img, bcht[1], bcht[2], m_color, m_thickness, LINE_AA);
cv::line(img, bcht[2], bcht[3], m_color, m_thickness, LINE_AA);
cv::line(img, bcht[3], bcht[0], m_color, m_thickness, LINE_AA);
cv::line(img, charts[k+j], charts[k+((j + 1) % 4)], m_color, m_thickness, LINE_AA);
}
}

void CCheckerDrawImpl::
transform_points_forward(InputArray T, const std::vector<cv::Point2f> &X, std::vector<cv::Point2f> &Xt)
void transform_points_forward(const Matx33f& T, const std::vector<Point2f> &X, std::vector<Point2f> &Xt)
{

cv::Matx33f _T = T.getMat();
size_t N = X.size();
Xt.clear();
Xt.resize(N);
if (Xt.size() != N)
Xt.resize(N);
std::fill(Xt.begin(), Xt.end(), Point2f(0.f, 0.f));
if (N == 0)
return;

cv::Matx31f p, xt;
cv::Point2f pt;
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;
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
10 changes: 3 additions & 7 deletions modules/mcc/src/checker_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class CCheckerImpl : public CChecker

TYPECHART getTarget() CV_OVERRIDE;
std::vector<Point2f> getBox() CV_OVERRIDE;
std::vector<Point2f> getColorCharts() CV_OVERRIDE;
Mat getChartsRGB() CV_OVERRIDE;
Mat getChartsYCbCr() CV_OVERRIDE;
float getCost() CV_OVERRIDE;
Expand Down Expand Up @@ -173,16 +174,11 @@ class CCheckerDrawImpl : public CCheckerDraw
Ptr<CChecker> m_pChecker;
cv::Scalar m_color;
int m_thickness;

private:
/** \brief transformation perspetive*/
void transform_points_forward(
InputArray T,
const std::vector<cv::Point2f> &X,
std::vector<cv::Point2f> &Xt);
};
// @}

void transform_points_forward(const Matx33f& T, const std::vector<Point2f> &X, std::vector<Point2f> &Xt);

} // namespace mcc
} // namespace cv

Expand Down

0 comments on commit c8c750a

Please sign in to comment.