Set stricter warning rules for gcc
This commit is contained in:
@@ -55,12 +55,12 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
void icvGetQuadrangleHypotheses(CvSeq* contours, std::vector<std::pair<float, int> >& quads, int class_id)
|
||||
static void icvGetQuadrangleHypotheses(CvSeq* contours, std::vector<std::pair<float, int> >& quads, int class_id)
|
||||
{
|
||||
const float min_aspect_ratio = 0.3f;
|
||||
const float max_aspect_ratio = 3.0f;
|
||||
const float min_box_size = 10.0f;
|
||||
|
||||
|
||||
for(CvSeq* seq = contours; seq != NULL; seq = seq->h_next)
|
||||
{
|
||||
CvBox2D box = cvMinAreaRect2(seq);
|
||||
@@ -75,12 +75,12 @@ void icvGetQuadrangleHypotheses(CvSeq* contours, std::vector<std::pair<float, in
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
quads.push_back(std::pair<float, int>(box_size, class_id));
|
||||
}
|
||||
}
|
||||
|
||||
void countClasses(const std::vector<std::pair<float, int> >& pairs, size_t idx1, size_t idx2, std::vector<int>& counts)
|
||||
static void countClasses(const std::vector<std::pair<float, int> >& pairs, size_t idx1, size_t idx2, std::vector<int>& counts)
|
||||
{
|
||||
counts.assign(2, 0);
|
||||
for(size_t i = idx1; i != idx2; i++)
|
||||
@@ -89,36 +89,36 @@ void countClasses(const std::vector<std::pair<float, int> >& pairs, size_t idx1,
|
||||
}
|
||||
}
|
||||
|
||||
bool less_pred(const std::pair<float, int>& p1, const std::pair<float, int>& p2)
|
||||
inline bool less_pred(const std::pair<float, int>& p1, const std::pair<float, int>& p2)
|
||||
{
|
||||
return p1.first < p2.first;
|
||||
}
|
||||
|
||||
// does a fast check if a chessboard is in the input image. This is a workaround to
|
||||
// does a fast check if a chessboard is in the input image. This is a workaround to
|
||||
// a problem of cvFindChessboardCorners being slow on images with no chessboard
|
||||
// - src: input image
|
||||
// - size: chessboard size
|
||||
// Returns 1 if a chessboard can be in this image and findChessboardCorners should be called,
|
||||
// Returns 1 if a chessboard can be in this image and findChessboardCorners should be called,
|
||||
// 0 if there is no chessboard, -1 in case of error
|
||||
int cvCheckChessboard(IplImage* src, CvSize size)
|
||||
{
|
||||
if(src->nChannels > 1)
|
||||
{
|
||||
cvError(CV_BadNumChannels, "cvCheckChessboard", "supports single-channel images only",
|
||||
cvError(CV_BadNumChannels, "cvCheckChessboard", "supports single-channel images only",
|
||||
__FILE__, __LINE__);
|
||||
}
|
||||
|
||||
|
||||
if(src->depth != 8)
|
||||
{
|
||||
cvError(CV_BadDepth, "cvCheckChessboard", "supports depth=8 images only",
|
||||
cvError(CV_BadDepth, "cvCheckChessboard", "supports depth=8 images only",
|
||||
__FILE__, __LINE__);
|
||||
}
|
||||
|
||||
|
||||
const int erosion_count = 1;
|
||||
const float black_level = 20.f;
|
||||
const float white_level = 130.f;
|
||||
const float black_white_gap = 70.f;
|
||||
|
||||
|
||||
#if defined(DEBUG_WINDOWS)
|
||||
cvNamedWindow("1", 1);
|
||||
cvShowImage("1", src);
|
||||
@@ -126,46 +126,46 @@ int cvCheckChessboard(IplImage* src, CvSize size)
|
||||
#endif //DEBUG_WINDOWS
|
||||
|
||||
CvMemStorage* storage = cvCreateMemStorage();
|
||||
|
||||
|
||||
IplImage* white = cvCloneImage(src);
|
||||
IplImage* black = cvCloneImage(src);
|
||||
|
||||
|
||||
cvErode(white, white, NULL, erosion_count);
|
||||
cvDilate(black, black, NULL, erosion_count);
|
||||
IplImage* thresh = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
|
||||
|
||||
|
||||
int result = 0;
|
||||
for(float thresh_level = black_level; thresh_level < white_level && !result; thresh_level += 20.0f)
|
||||
{
|
||||
cvThreshold(white, thresh, thresh_level + black_white_gap, 255, CV_THRESH_BINARY);
|
||||
|
||||
|
||||
#if defined(DEBUG_WINDOWS)
|
||||
cvShowImage("1", thresh);
|
||||
cvWaitKey(0);
|
||||
#endif //DEBUG_WINDOWS
|
||||
|
||||
|
||||
CvSeq* first = 0;
|
||||
std::vector<std::pair<float, int> > quads;
|
||||
cvFindContours(thresh, storage, &first, sizeof(CvContour), CV_RETR_CCOMP);
|
||||
cvFindContours(thresh, storage, &first, sizeof(CvContour), CV_RETR_CCOMP);
|
||||
icvGetQuadrangleHypotheses(first, quads, 1);
|
||||
|
||||
|
||||
cvThreshold(black, thresh, thresh_level, 255, CV_THRESH_BINARY_INV);
|
||||
|
||||
|
||||
#if defined(DEBUG_WINDOWS)
|
||||
cvShowImage("1", thresh);
|
||||
cvWaitKey(0);
|
||||
#endif //DEBUG_WINDOWS
|
||||
|
||||
|
||||
cvFindContours(thresh, storage, &first, sizeof(CvContour), CV_RETR_CCOMP);
|
||||
icvGetQuadrangleHypotheses(first, quads, 0);
|
||||
|
||||
|
||||
const size_t min_quads_count = size.width*size.height/2;
|
||||
std::sort(quads.begin(), quads.end(), less_pred);
|
||||
|
||||
|
||||
// now check if there are many hypotheses with similar sizes
|
||||
// do this by floodfill-style algorithm
|
||||
const float size_rel_dev = 0.4f;
|
||||
|
||||
|
||||
for(size_t i = 0; i < quads.size(); i++)
|
||||
{
|
||||
size_t j = i + 1;
|
||||
@@ -176,7 +176,7 @@ int cvCheckChessboard(IplImage* src, CvSize size)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(j + 1 > min_quads_count + i)
|
||||
{
|
||||
// check the number of black and white squares
|
||||
@@ -194,12 +194,12 @@ int cvCheckChessboard(IplImage* src, CvSize size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
cvReleaseImage(&thresh);
|
||||
cvReleaseImage(&white);
|
||||
cvReleaseImage(&black);
|
||||
cvReleaseMemStorage(&storage);
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@@ -1223,7 +1223,7 @@ void computePredecessorMatrix(const Mat &dm, int verticesCount, Mat &predecessor
|
||||
}
|
||||
}
|
||||
|
||||
void computeShortestPath(Mat &predecessorMatrix, size_t v1, size_t v2, vector<size_t> &path)
|
||||
static void computeShortestPath(Mat &predecessorMatrix, size_t v1, size_t v2, vector<size_t> &path)
|
||||
{
|
||||
if (predecessorMatrix.at<int> ((int)v1, (int)v2) < 0)
|
||||
{
|
||||
@@ -1403,7 +1403,7 @@ void CirclesGridFinder::getHoles(vector<Point2f> &outHoles) const
|
||||
}
|
||||
}
|
||||
|
||||
bool areIndicesCorrect(Point pos, vector<vector<size_t> > *points)
|
||||
static bool areIndicesCorrect(Point pos, vector<vector<size_t> > *points)
|
||||
{
|
||||
if (pos.y < 0 || pos.x < 0)
|
||||
return false;
|
||||
|
@@ -42,11 +42,11 @@
|
||||
#ifndef __OPENCV_PRECOMP_H__
|
||||
#define __OPENCV_PRECOMP_H__
|
||||
|
||||
#if _MSC_VER >= 1200
|
||||
#if defined _MSC_VER && _MSC_VER >= 1200
|
||||
#pragma warning( disable: 4251 4710 4711 4514 4996 )
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CVCONFIG_H
|
||||
#ifdef HAVE_CVCONFIG_H
|
||||
#include "cvconfig.h"
|
||||
#endif
|
||||
|
||||
|
@@ -52,41 +52,41 @@
|
||||
#undef max
|
||||
|
||||
namespace cv {
|
||||
|
||||
|
||||
void drawCircles(Mat& img, const vector<Point2f>& corners, const vector<float>& radius)
|
||||
{
|
||||
for(size_t i = 0; i < corners.size(); i++)
|
||||
{
|
||||
circle(img, corners[i], cvRound(radius[i]), CV_RGB(255, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
int histQuantile(const Mat& hist, float quantile)
|
||||
{
|
||||
if(hist.dims > 1) return -1; // works for 1D histograms only
|
||||
|
||||
float cur_sum = 0;
|
||||
float total_sum = (float)sum(hist).val[0];
|
||||
float quantile_sum = total_sum*quantile;
|
||||
for(int j = 0; j < hist.size[0]; j++)
|
||||
{
|
||||
cur_sum += (float)hist.at<float>(j);
|
||||
if(cur_sum > quantile_sum)
|
||||
{
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
||||
return hist.size[0] - 1;
|
||||
}
|
||||
|
||||
bool is_smaller(const std::pair<int, float>& p1, const std::pair<int, float>& p2)
|
||||
|
||||
|
||||
// static void drawCircles(Mat& img, const vector<Point2f>& corners, const vector<float>& radius)
|
||||
// {
|
||||
// for(size_t i = 0; i < corners.size(); i++)
|
||||
// {
|
||||
// circle(img, corners[i], cvRound(radius[i]), CV_RGB(255, 0, 0));
|
||||
// }
|
||||
// }
|
||||
|
||||
// static int histQuantile(const Mat& hist, float quantile)
|
||||
// {
|
||||
// if(hist.dims > 1) return -1; // works for 1D histograms only
|
||||
|
||||
// float cur_sum = 0;
|
||||
// float total_sum = (float)sum(hist).val[0];
|
||||
// float quantile_sum = total_sum*quantile;
|
||||
// for(int j = 0; j < hist.size[0]; j++)
|
||||
// {
|
||||
// cur_sum += (float)hist.at<float>(j);
|
||||
// if(cur_sum > quantile_sum)
|
||||
// {
|
||||
// return j;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return hist.size[0] - 1;
|
||||
// }
|
||||
|
||||
inline bool is_smaller(const std::pair<int, float>& p1, const std::pair<int, float>& p2)
|
||||
{
|
||||
return p1.second < p2.second;
|
||||
}
|
||||
|
||||
void orderContours(const vector<vector<Point> >& contours, Point2f point, vector<std::pair<int, float> >& order)
|
||||
static void orderContours(const vector<vector<Point> >& contours, Point2f point, vector<std::pair<int, float> >& order)
|
||||
{
|
||||
order.clear();
|
||||
size_t i, j, n = contours.size();
|
||||
@@ -101,58 +101,58 @@ void orderContours(const vector<vector<Point> >& contours, Point2f point, vector
|
||||
}
|
||||
order.push_back(std::pair<int, float>((int)i, (float)min_dist));
|
||||
}
|
||||
|
||||
|
||||
std::sort(order.begin(), order.end(), is_smaller);
|
||||
}
|
||||
|
||||
// fit second order curve to a set of 2D points
|
||||
void fitCurve2Order(const vector<Point2f>& /*points*/, vector<float>& /*curve*/)
|
||||
inline void fitCurve2Order(const vector<Point2f>& /*points*/, vector<float>& /*curve*/)
|
||||
{
|
||||
// TBD
|
||||
}
|
||||
|
||||
void findCurvesCross(const vector<float>& /*curve1*/, const vector<float>& /*curve2*/, Point2f& /*cross_point*/)
|
||||
|
||||
inline void findCurvesCross(const vector<float>& /*curve1*/, const vector<float>& /*curve2*/, Point2f& /*cross_point*/)
|
||||
{
|
||||
}
|
||||
|
||||
void findLinesCrossPoint(Point2f origin1, Point2f dir1, Point2f origin2, Point2f dir2, Point2f& cross_point)
|
||||
|
||||
static void findLinesCrossPoint(Point2f origin1, Point2f dir1, Point2f origin2, Point2f dir2, Point2f& cross_point)
|
||||
{
|
||||
float det = dir2.x*dir1.y - dir2.y*dir1.x;
|
||||
Point2f offset = origin2 - origin1;
|
||||
|
||||
|
||||
float alpha = (dir2.x*offset.y - dir2.y*offset.x)/det;
|
||||
cross_point = origin1 + dir1*alpha;
|
||||
}
|
||||
|
||||
void findCorner(const vector<Point>& contour, Point2f point, Point2f& corner)
|
||||
{
|
||||
// find the nearest point
|
||||
double min_dist = std::numeric_limits<double>::max();
|
||||
int min_idx = -1;
|
||||
|
||||
// find corner idx
|
||||
for(size_t i = 0; i < contour.size(); i++)
|
||||
{
|
||||
double dist = norm(Point2f((float)contour[i].x, (float)contour[i].y) - point);
|
||||
if(dist < min_dist)
|
||||
{
|
||||
min_dist = dist;
|
||||
min_idx = (int)i;
|
||||
}
|
||||
}
|
||||
assert(min_idx >= 0);
|
||||
|
||||
// temporary solution, have to make something more precise
|
||||
corner = contour[min_idx];
|
||||
return;
|
||||
}
|
||||
|
||||
void findCorner(const vector<Point2f>& contour, Point2f point, Point2f& corner)
|
||||
// static void findCorner(const vector<Point>& contour, Point2f point, Point2f& corner)
|
||||
// {
|
||||
// // find the nearest point
|
||||
// double min_dist = std::numeric_limits<double>::max();
|
||||
// int min_idx = -1;
|
||||
|
||||
// // find corner idx
|
||||
// for(size_t i = 0; i < contour.size(); i++)
|
||||
// {
|
||||
// double dist = norm(Point2f((float)contour[i].x, (float)contour[i].y) - point);
|
||||
// if(dist < min_dist)
|
||||
// {
|
||||
// min_dist = dist;
|
||||
// min_idx = (int)i;
|
||||
// }
|
||||
// }
|
||||
// assert(min_idx >= 0);
|
||||
|
||||
// // temporary solution, have to make something more precise
|
||||
// corner = contour[min_idx];
|
||||
// return;
|
||||
// }
|
||||
|
||||
static void findCorner(const vector<Point2f>& contour, Point2f point, Point2f& corner)
|
||||
{
|
||||
// find the nearest point
|
||||
double min_dist = std::numeric_limits<double>::max();
|
||||
int min_idx = -1;
|
||||
|
||||
|
||||
// find corner idx
|
||||
for(size_t i = 0; i < contour.size(); i++)
|
||||
{
|
||||
@@ -164,23 +164,23 @@ void findCorner(const vector<Point2f>& contour, Point2f point, Point2f& corner)
|
||||
}
|
||||
}
|
||||
assert(min_idx >= 0);
|
||||
|
||||
|
||||
// temporary solution, have to make something more precise
|
||||
corner = contour[min_idx];
|
||||
return;
|
||||
}
|
||||
|
||||
int segment_hist_max(const Mat& hist, int& low_thresh, int& high_thresh)
|
||||
|
||||
static int segment_hist_max(const Mat& hist, int& low_thresh, int& high_thresh)
|
||||
{
|
||||
Mat bw;
|
||||
//const double max_bell_width = 20; // we expect two bells with width bounded above
|
||||
//const double min_bell_width = 5; // and below
|
||||
|
||||
|
||||
double total_sum = sum(hist).val[0];
|
||||
//double thresh = total_sum/(2*max_bell_width)*0.25f; // quarter of a bar inside a bell
|
||||
|
||||
|
||||
// threshold(hist, bw, thresh, 255.0, CV_THRESH_BINARY);
|
||||
|
||||
|
||||
double quantile_sum = 0.0;
|
||||
//double min_quantile = 0.2;
|
||||
double low_sum = 0;
|
||||
@@ -193,7 +193,7 @@ int segment_hist_max(const Mat& hist, int& low_thresh, int& high_thresh)
|
||||
{
|
||||
quantile_sum += hist.at<float>(x);
|
||||
if(quantile_sum < 0.2*total_sum) continue;
|
||||
|
||||
|
||||
if(quantile_sum - low_sum > out_of_bells_fraction*total_sum)
|
||||
{
|
||||
if(max_segment_length < x - start_x)
|
||||
@@ -207,7 +207,7 @@ int segment_hist_max(const Mat& hist, int& low_thresh, int& high_thresh)
|
||||
start_x = x;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(start_x == -1)
|
||||
{
|
||||
return 0;
|
||||
@@ -219,9 +219,9 @@ int segment_hist_max(const Mat& hist, int& low_thresh, int& high_thresh)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size region_size)
|
||||
{
|
||||
Mat img = _img.getMat(), cornersM = _corners.getMat();
|
||||
@@ -232,22 +232,22 @@ bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size
|
||||
float ranges[] = {0, 256};
|
||||
const float* _ranges = ranges;
|
||||
Mat hist;
|
||||
|
||||
|
||||
#if defined(_SUBPIX_VERBOSE)
|
||||
vector<float> radius;
|
||||
radius.assign(corners.size(), 0.0f);
|
||||
#endif //_SUBPIX_VERBOSE
|
||||
|
||||
|
||||
|
||||
|
||||
Mat black_comp, white_comp;
|
||||
for(int i = 0; i < ncorners; i++)
|
||||
{
|
||||
{
|
||||
int channels = 0;
|
||||
Rect roi(cvRound(corners[i].x - region_size.width), cvRound(corners[i].y - region_size.height),
|
||||
region_size.width*2 + 1, region_size.height*2 + 1);
|
||||
Mat img_roi = img(roi);
|
||||
calcHist(&img_roi, 1, &channels, Mat(), hist, 1, &nbins, &_ranges);
|
||||
|
||||
|
||||
#if 0
|
||||
int black_thresh = histQuantile(hist, 0.45f);
|
||||
int white_thresh = histQuantile(hist, 0.55f);
|
||||
@@ -255,10 +255,10 @@ bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size
|
||||
int black_thresh, white_thresh;
|
||||
segment_hist_max(hist, black_thresh, white_thresh);
|
||||
#endif
|
||||
|
||||
|
||||
threshold(img, black_comp, black_thresh, 255.0, CV_THRESH_BINARY_INV);
|
||||
threshold(img, white_comp, white_thresh, 255.0, CV_THRESH_BINARY);
|
||||
|
||||
|
||||
const int erode_count = 1;
|
||||
erode(black_comp, black_comp, Mat(), Point(-1, -1), erode_count);
|
||||
erode(white_comp, white_comp, Mat(), Point(-1, -1), erode_count);
|
||||
@@ -275,28 +275,28 @@ bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size
|
||||
imwrite("black.jpg", black_comp);
|
||||
imwrite("white.jpg", white_comp);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
vector<vector<Point> > white_contours, black_contours;
|
||||
vector<Vec4i> white_hierarchy, black_hierarchy;
|
||||
findContours(black_comp, black_contours, black_hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
|
||||
findContours(white_comp, white_contours, white_hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
|
||||
|
||||
|
||||
if(black_contours.size() < 5 || white_contours.size() < 5) continue;
|
||||
|
||||
|
||||
// find two white and black blobs that are close to the input point
|
||||
vector<std::pair<int, float> > white_order, black_order;
|
||||
orderContours(black_contours, corners[i], black_order);
|
||||
orderContours(white_contours, corners[i], white_order);
|
||||
|
||||
const float max_dist = 10.0f;
|
||||
if(black_order[0].second > max_dist || black_order[1].second > max_dist ||
|
||||
if(black_order[0].second > max_dist || black_order[1].second > max_dist ||
|
||||
white_order[0].second > max_dist || white_order[1].second > max_dist)
|
||||
{
|
||||
continue; // there will be no improvement in this corner position
|
||||
}
|
||||
|
||||
const vector<Point>* quads[4] = {&black_contours[black_order[0].first], &black_contours[black_order[1].first],
|
||||
|
||||
const vector<Point>* quads[4] = {&black_contours[black_order[0].first], &black_contours[black_order[1].first],
|
||||
&white_contours[white_order[0].first], &white_contours[white_order[1].first]};
|
||||
vector<Point2f> quads_approx[4];
|
||||
Point2f quad_corners[4];
|
||||
@@ -306,14 +306,14 @@ bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size
|
||||
vector<Point2f> temp;
|
||||
for(size_t j = 0; j < quads[k]->size(); j++) temp.push_back((*quads[k])[j]);
|
||||
approxPolyDP(Mat(temp), quads_approx[k], 0.5, true);
|
||||
|
||||
|
||||
findCorner(quads_approx[k], corners[i], quad_corners[k]);
|
||||
#else
|
||||
findCorner(*quads[k], corners[i], quad_corners[k]);
|
||||
#endif
|
||||
quad_corners[k] += Point2f(0.5f, 0.5f);
|
||||
}
|
||||
|
||||
|
||||
// cross two lines
|
||||
Point2f origin1 = quad_corners[0];
|
||||
Point2f dir1 = quad_corners[1] - quad_corners[0];
|
||||
@@ -321,12 +321,12 @@ bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size
|
||||
Point2f dir2 = quad_corners[3] - quad_corners[2];
|
||||
double angle = acos(dir1.dot(dir2)/(norm(dir1)*norm(dir2)));
|
||||
if(cvIsNaN(angle) || cvIsInf(angle) || angle < 0.5 || angle > CV_PI - 0.5) continue;
|
||||
|
||||
|
||||
findLinesCrossPoint(origin1, dir1, origin2, dir2, corners[i]);
|
||||
|
||||
|
||||
#if defined(_SUBPIX_VERBOSE)
|
||||
radius[i] = norm(corners[i] - ground_truth_corners[ground_truth_idx])*6;
|
||||
|
||||
|
||||
#if 1
|
||||
Mat test(img.size(), CV_32FC3);
|
||||
cvtColor(img, test, CV_GRAY2RGB);
|
||||
@@ -349,9 +349,9 @@ bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size
|
||||
waitKey(0);
|
||||
#endif
|
||||
#endif //_SUBPIX_VERBOSE
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#if defined(_SUBPIX_VERBOSE)
|
||||
Mat test(img.size(), CV_32FC3);
|
||||
cvtColor(img, test, CV_GRAY2RGB);
|
||||
@@ -361,6 +361,6 @@ bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size
|
||||
imshow("corners", test);
|
||||
waitKey();
|
||||
#endif //_SUBPIX_VERBOSE
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -52,48 +52,48 @@ bool cv::solvePnP( InputArray _opoints, InputArray _ipoints,
|
||||
{
|
||||
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat();
|
||||
int npoints = std::max(opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
|
||||
CV_Assert( npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) );
|
||||
CV_Assert( npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) );
|
||||
_rvec.create(3, 1, CV_64F);
|
||||
_tvec.create(3, 1, CV_64F);
|
||||
Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat();
|
||||
|
||||
if (flags == CV_EPNP)
|
||||
{
|
||||
cv::Mat undistortedPoints;
|
||||
cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
|
||||
epnp PnP(cameraMatrix, opoints, undistortedPoints);
|
||||
|
||||
cv::Mat undistortedPoints;
|
||||
cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
|
||||
epnp PnP(cameraMatrix, opoints, undistortedPoints);
|
||||
|
||||
cv::Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
|
||||
PnP.compute_pose(R, tvec);
|
||||
cv::Rodrigues(R, rvec);
|
||||
return true;
|
||||
}
|
||||
else if (flags == CV_P3P)
|
||||
{
|
||||
CV_Assert( npoints == 4);
|
||||
cv::Mat undistortedPoints;
|
||||
cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
|
||||
p3p P3Psolver(cameraMatrix);
|
||||
return true;
|
||||
}
|
||||
else if (flags == CV_P3P)
|
||||
{
|
||||
CV_Assert( npoints == 4);
|
||||
cv::Mat undistortedPoints;
|
||||
cv::undistortPoints(ipoints, undistortedPoints, cameraMatrix, distCoeffs);
|
||||
p3p P3Psolver(cameraMatrix);
|
||||
|
||||
cv::Mat R, rvec = _rvec.getMat(), tvec = _tvec.getMat();
|
||||
bool result = P3Psolver.solve(R, tvec, opoints, undistortedPoints);
|
||||
if (result)
|
||||
cv::Rodrigues(R, rvec);
|
||||
return result;
|
||||
}
|
||||
else if (flags == CV_ITERATIVE)
|
||||
{
|
||||
CvMat c_objectPoints = opoints, c_imagePoints = ipoints;
|
||||
CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs;
|
||||
CvMat c_rvec = _rvec.getMat(), c_tvec = _tvec.getMat();
|
||||
cvFindExtrinsicCameraParams2(&c_objectPoints, &c_imagePoints, &c_cameraMatrix,
|
||||
c_distCoeffs.rows*c_distCoeffs.cols ? &c_distCoeffs : 0,
|
||||
&c_rvec, &c_tvec, useExtrinsicGuess );
|
||||
return true;
|
||||
}
|
||||
else
|
||||
cv::Rodrigues(R, rvec);
|
||||
return result;
|
||||
}
|
||||
else if (flags == CV_ITERATIVE)
|
||||
{
|
||||
CvMat c_objectPoints = opoints, c_imagePoints = ipoints;
|
||||
CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs;
|
||||
CvMat c_rvec = _rvec.getMat(), c_tvec = _tvec.getMat();
|
||||
cvFindExtrinsicCameraParams2(&c_objectPoints, &c_imagePoints, &c_cameraMatrix,
|
||||
c_distCoeffs.rows*c_distCoeffs.cols ? &c_distCoeffs : 0,
|
||||
&c_rvec, &c_tvec, useExtrinsicGuess );
|
||||
return true;
|
||||
}
|
||||
else
|
||||
CV_Error(CV_StsBadArg, "The flags argument must be one of CV_ITERATIVE or CV_EPNP");
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
namespace cv
|
||||
@@ -101,8 +101,8 @@ namespace cv
|
||||
namespace pnpransac
|
||||
{
|
||||
const int MIN_POINTS_COUNT = 4;
|
||||
|
||||
void project3dPoints(const Mat& points, const Mat& rvec, const Mat& tvec, Mat& modif_points)
|
||||
|
||||
static void project3dPoints(const Mat& points, const Mat& rvec, const Mat& tvec, Mat& modif_points)
|
||||
{
|
||||
modif_points.create(1, points.cols, CV_32FC3);
|
||||
Mat R(3, 3, CV_64FC1);
|
||||
@@ -114,32 +114,32 @@ namespace cv
|
||||
tvec.copyTo(t);
|
||||
transform(points, modif_points, transformation);
|
||||
}
|
||||
|
||||
|
||||
class Mutex
|
||||
{
|
||||
public:
|
||||
Mutex() {
|
||||
}
|
||||
}
|
||||
void lock()
|
||||
{
|
||||
#ifdef HAVE_TBB
|
||||
resultsMutex.lock();
|
||||
resultsMutex.lock();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void unlock()
|
||||
{
|
||||
#ifdef HAVE_TBB
|
||||
resultsMutex.unlock();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
#ifdef HAVE_TBB
|
||||
tbb::mutex resultsMutex;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
struct CameraParameters
|
||||
{
|
||||
void init(Mat _intrinsics, Mat _distCoeffs)
|
||||
@@ -147,22 +147,22 @@ namespace cv
|
||||
_intrinsics.copyTo(intrinsics);
|
||||
_distCoeffs.copyTo(distortion);
|
||||
}
|
||||
|
||||
|
||||
Mat intrinsics;
|
||||
Mat distortion;
|
||||
};
|
||||
|
||||
|
||||
struct Parameters
|
||||
{
|
||||
int iterationsCount;
|
||||
float reprojectionError;
|
||||
int minInliersCount;
|
||||
bool useExtrinsicGuess;
|
||||
int flags;
|
||||
int flags;
|
||||
CameraParameters camera;
|
||||
};
|
||||
|
||||
void pnpTask(const vector<char>& pointsMask, const Mat& objectPoints, const Mat& imagePoints,
|
||||
|
||||
static void pnpTask(const vector<char>& pointsMask, const Mat& objectPoints, const Mat& imagePoints,
|
||||
const Parameters& params, vector<int>& inliers, Mat& rvec, Mat& tvec,
|
||||
const Mat& rvecInit, const Mat& tvecInit, Mutex& resultsMutex)
|
||||
{
|
||||
@@ -178,7 +178,7 @@ namespace cv
|
||||
colIndex = colIndex+1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//filter same 3d points, hang in solvePnP
|
||||
double eps = 1e-10;
|
||||
int num_same_points = 0;
|
||||
@@ -190,22 +190,22 @@ namespace cv
|
||||
}
|
||||
if (num_same_points > 0)
|
||||
return;
|
||||
|
||||
|
||||
Mat localRvec, localTvec;
|
||||
rvecInit.copyTo(localRvec);
|
||||
tvecInit.copyTo(localTvec);
|
||||
|
||||
solvePnP(modelObjectPoints, modelImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec,
|
||||
params.useExtrinsicGuess, params.flags);
|
||||
|
||||
|
||||
|
||||
solvePnP(modelObjectPoints, modelImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec,
|
||||
params.useExtrinsicGuess, params.flags);
|
||||
|
||||
|
||||
vector<Point2f> projected_points;
|
||||
projected_points.resize(objectPoints.cols);
|
||||
projectPoints(objectPoints, localRvec, localTvec, params.camera.intrinsics, params.camera.distortion, projected_points);
|
||||
|
||||
|
||||
Mat rotatedPoints;
|
||||
project3dPoints(objectPoints, localRvec, localTvec, rotatedPoints);
|
||||
|
||||
|
||||
vector<int> localInliers;
|
||||
for (int i = 0; i < objectPoints.cols; i++)
|
||||
{
|
||||
@@ -216,21 +216,21 @@ namespace cv
|
||||
localInliers.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (localInliers.size() > inliers.size())
|
||||
{
|
||||
resultsMutex.lock();
|
||||
|
||||
|
||||
inliers.clear();
|
||||
inliers.resize(localInliers.size());
|
||||
memcpy(&inliers[0], &localInliers[0], sizeof(int) * localInliers.size());
|
||||
localRvec.copyTo(rvec);
|
||||
localTvec.copyTo(tvec);
|
||||
|
||||
|
||||
resultsMutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class PnPSolver
|
||||
{
|
||||
public:
|
||||
@@ -262,18 +262,18 @@ namespace cv
|
||||
tvec.copyTo(initTvec);
|
||||
}
|
||||
private:
|
||||
PnPSolver& operator=(const PnPSolver&);
|
||||
|
||||
PnPSolver& operator=(const PnPSolver&);
|
||||
|
||||
const Mat& objectPoints;
|
||||
const Mat& imagePoints;
|
||||
const Parameters& parameters;
|
||||
Mat &rvec, &tvec;
|
||||
vector<int>& inliers;
|
||||
Mat initRvec, initTvec;
|
||||
|
||||
|
||||
static RNG generator;
|
||||
static Mutex syncMutex;
|
||||
|
||||
|
||||
void generateVar(vector<char>& mask) const
|
||||
{
|
||||
int size = (int)mask.size();
|
||||
@@ -287,10 +287,10 @@ namespace cv
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Mutex PnPSolver::syncMutex;
|
||||
RNG PnPSolver::generator;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,21 +302,21 @@ void cv::solvePnPRansac(InputArray _opoints, InputArray _ipoints,
|
||||
{
|
||||
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat();
|
||||
Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat();
|
||||
|
||||
|
||||
CV_Assert(opoints.isContinuous());
|
||||
CV_Assert(opoints.depth() == CV_32F);
|
||||
CV_Assert((opoints.rows == 1 && opoints.channels() == 3) || opoints.cols*opoints.channels() == 3);
|
||||
CV_Assert(ipoints.isContinuous());
|
||||
CV_Assert(ipoints.depth() == CV_32F);
|
||||
CV_Assert((ipoints.rows == 1 && ipoints.channels() == 2) || ipoints.cols*ipoints.channels() == 2);
|
||||
|
||||
|
||||
_rvec.create(3, 1, CV_64FC1);
|
||||
_tvec.create(3, 1, CV_64FC1);
|
||||
Mat rvec = _rvec.getMat();
|
||||
Mat tvec = _tvec.getMat();
|
||||
|
||||
|
||||
Mat objectPoints = opoints.reshape(3, 1), imagePoints = ipoints.reshape(2, 1);
|
||||
|
||||
|
||||
if (minInliersCount <= 0)
|
||||
minInliersCount = objectPoints.cols;
|
||||
cv::pnpransac::Parameters params;
|
||||
@@ -325,36 +325,36 @@ void cv::solvePnPRansac(InputArray _opoints, InputArray _ipoints,
|
||||
params.reprojectionError = reprojectionError;
|
||||
params.useExtrinsicGuess = useExtrinsicGuess;
|
||||
params.camera.init(cameraMatrix, distCoeffs);
|
||||
params.flags = flags;
|
||||
|
||||
params.flags = flags;
|
||||
|
||||
vector<int> localInliers;
|
||||
Mat localRvec, localTvec;
|
||||
rvec.copyTo(localRvec);
|
||||
tvec.copyTo(localTvec);
|
||||
|
||||
|
||||
if (objectPoints.cols >= pnpransac::MIN_POINTS_COUNT)
|
||||
{
|
||||
parallel_for(BlockedRange(0,iterationsCount), cv::pnpransac::PnPSolver(objectPoints, imagePoints, params,
|
||||
localRvec, localTvec, localInliers));
|
||||
}
|
||||
|
||||
|
||||
if (localInliers.size() >= (size_t)pnpransac::MIN_POINTS_COUNT)
|
||||
{
|
||||
if (flags != CV_P3P)
|
||||
{
|
||||
int i, pointsCount = (int)localInliers.size();
|
||||
Mat inlierObjectPoints(1, pointsCount, CV_32FC3), inlierImagePoints(1, pointsCount, CV_32FC2);
|
||||
for (i = 0; i < pointsCount; i++)
|
||||
{
|
||||
int index = localInliers[i];
|
||||
Mat colInlierImagePoints = inlierImagePoints(Rect(i, 0, 1, 1));
|
||||
imagePoints.col(index).copyTo(colInlierImagePoints);
|
||||
Mat colInlierObjectPoints = inlierObjectPoints(Rect(i, 0, 1, 1));
|
||||
objectPoints.col(index).copyTo(colInlierObjectPoints);
|
||||
}
|
||||
solvePnP(inlierObjectPoints, inlierImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec, true, flags);
|
||||
}
|
||||
localRvec.copyTo(rvec);
|
||||
if (flags != CV_P3P)
|
||||
{
|
||||
int i, pointsCount = (int)localInliers.size();
|
||||
Mat inlierObjectPoints(1, pointsCount, CV_32FC3), inlierImagePoints(1, pointsCount, CV_32FC2);
|
||||
for (i = 0; i < pointsCount; i++)
|
||||
{
|
||||
int index = localInliers[i];
|
||||
Mat colInlierImagePoints = inlierImagePoints(Rect(i, 0, 1, 1));
|
||||
imagePoints.col(index).copyTo(colInlierImagePoints);
|
||||
Mat colInlierObjectPoints = inlierObjectPoints(Rect(i, 0, 1, 1));
|
||||
objectPoints.col(index).copyTo(colInlierObjectPoints);
|
||||
}
|
||||
solvePnP(inlierObjectPoints, inlierImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec, true, flags);
|
||||
}
|
||||
localRvec.copyTo(rvec);
|
||||
localTvec.copyTo(tvec);
|
||||
if (_inliers.needed())
|
||||
Mat(localInliers).copyTo(_inliers);
|
||||
|
Reference in New Issue
Block a user