parent
4c160acc35
commit
f2a3e7e312
10 changed files with 830 additions and 976 deletions
@ -1,80 +0,0 @@ |
|||||||
#include "opencv2/highgui/highgui.hpp" |
|
||||||
#include "opencv2/imgproc/imgproc_c.h" |
|
||||||
|
|
||||||
#include <stdio.h> |
|
||||||
|
|
||||||
IplImage* inpaint_mask = 0; |
|
||||||
IplImage* img0 = 0, *img = 0, *inpainted = 0; |
|
||||||
CvPoint prev_pt = {-1,-1}; |
|
||||||
|
|
||||||
void on_mouse( int event, int x, int y, int flags, void* ) |
|
||||||
{ |
|
||||||
if( !img ) |
|
||||||
return; |
|
||||||
|
|
||||||
if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) ) |
|
||||||
prev_pt = cvPoint(-1,-1); |
|
||||||
else if( event == CV_EVENT_LBUTTONDOWN ) |
|
||||||
prev_pt = cvPoint(x,y); |
|
||||||
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) ) |
|
||||||
{ |
|
||||||
CvPoint pt = cvPoint(x,y); |
|
||||||
if( prev_pt.x < 0 ) |
|
||||||
prev_pt = pt; |
|
||||||
cvLine( inpaint_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 ); |
|
||||||
cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 ); |
|
||||||
prev_pt = pt; |
|
||||||
cvShowImage( "image", img ); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
int main( int argc, char** argv ) |
|
||||||
{ |
|
||||||
char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg"; |
|
||||||
|
|
||||||
if( (img0 = cvLoadImage(filename,-1)) == 0 ) |
|
||||||
return 0; |
|
||||||
|
|
||||||
printf( "Hot keys: \n" |
|
||||||
"\tESC - quit the program\n" |
|
||||||
"\tr - restore the original image\n" |
|
||||||
"\ti or SPACE - run inpainting algorithm\n" |
|
||||||
"\t\t(before running it, paint something on the image)\n" ); |
|
||||||
|
|
||||||
cvNamedWindow( "image", 1 ); |
|
||||||
|
|
||||||
img = cvCloneImage( img0 ); |
|
||||||
inpainted = cvCloneImage( img0 ); |
|
||||||
inpaint_mask = cvCreateImage( cvGetSize(img), 8, 1 ); |
|
||||||
|
|
||||||
cvZero( inpaint_mask ); |
|
||||||
cvZero( inpainted ); |
|
||||||
cvShowImage( "image", img ); |
|
||||||
cvShowImage( "inpainted image", inpainted ); |
|
||||||
cvSetMouseCallback( "image", on_mouse, 0 ); |
|
||||||
|
|
||||||
for(;;) |
|
||||||
{ |
|
||||||
int c = cvWaitKey(0); |
|
||||||
|
|
||||||
if( (char)c == 27 ) |
|
||||||
break; |
|
||||||
|
|
||||||
if( (char)c == 'r' ) |
|
||||||
{ |
|
||||||
cvZero( inpaint_mask ); |
|
||||||
cvCopy( img0, img ); |
|
||||||
cvShowImage( "image", img ); |
|
||||||
} |
|
||||||
|
|
||||||
if( (char)c == 'i' || (char)c == ' ' ) |
|
||||||
{ |
|
||||||
cvNamedWindow( "inpainted image", 1 ); |
|
||||||
cvInpaint( img, inpaint_mask, inpainted, 3, CV_INPAINT_TELEA ); |
|
||||||
cvShowImage( "inpainted image", inpainted ); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return 1; |
|
||||||
} |
|
@ -1,323 +1,323 @@ |
|||||||
#include <iostream> |
#include <iostream> |
||||||
#include <vector> |
#include <vector> |
||||||
#include <algorithm> |
#include <algorithm> |
||||||
#include <iterator> |
#include <iterator> |
||||||
|
|
||||||
#include <opencv2/calib3d/calib3d.hpp> |
#include <opencv2/calib3d/calib3d.hpp> |
||||||
#include <opencv2/imgproc/imgproc.hpp> |
#include <opencv2/imgproc/imgproc.hpp> |
||||||
#include <opencv2/highgui/highgui.hpp> |
#include <opencv2/highgui/highgui.hpp> |
||||||
|
|
||||||
using namespace cv; |
using namespace cv; |
||||||
using namespace std; |
using namespace std; |
||||||
|
|
||||||
namespace cv |
namespace cv |
||||||
{ |
{ |
||||||
|
|
||||||
/* copy of class defines int tests/cv/chessboardgenerator.h */ |
/* copy of class defines int tests/cv/chessboardgenerator.h */ |
||||||
class ChessBoardGenerator |
class ChessBoardGenerator |
||||||
{ |
{ |
||||||
public: |
public: |
||||||
double sensorWidth;
|
double sensorWidth;
|
||||||
double sensorHeight;
|
double sensorHeight;
|
||||||
size_t squareEdgePointsNum; |
size_t squareEdgePointsNum; |
||||||
double min_cos; |
double min_cos; |
||||||
mutable double cov; |
mutable double cov; |
||||||
Size patternSize; |
Size patternSize; |
||||||
int rendererResolutionMultiplier; |
int rendererResolutionMultiplier; |
||||||
|
|
||||||
ChessBoardGenerator(const Size& patternSize = Size(8, 6)); |
ChessBoardGenerator(const Size& patternSize = Size(8, 6)); |
||||||
Mat operator()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, vector<Point2f>& corners) const;
|
Mat operator()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, vector<Point2f>& corners) const;
|
||||||
Size cornersSize() const; |
Size cornersSize() const; |
||||||
private: |
private: |
||||||
void generateEdge(const Point3f& p1, const Point3f& p2, vector<Point3f>& out) const; |
void generateEdge(const Point3f& p1, const Point3f& p2, vector<Point3f>& out) const; |
||||||
Mat generageChessBoard(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
|
Mat generageChessBoard(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
|
||||||
const Point3f& zero, const Point3f& pb1, const Point3f& pb2,
|
const Point3f& zero, const Point3f& pb1, const Point3f& pb2,
|
||||||
float sqWidth, float sqHeight, const vector<Point3f>& whole, vector<Point2f>& corners) const; |
float sqWidth, float sqHeight, const vector<Point3f>& whole, vector<Point2f>& corners) const; |
||||||
void generateBasis(Point3f& pb1, Point3f& pb2) const;
|
void generateBasis(Point3f& pb1, Point3f& pb2) const;
|
||||||
Point3f generateChessBoardCenter(const Mat& camMat, const Size& imgSize) const; |
Point3f generateChessBoardCenter(const Mat& camMat, const Size& imgSize) const; |
||||||
Mat rvec, tvec; |
Mat rvec, tvec; |
||||||
}; |
}; |
||||||
}; |
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const Size imgSize(800, 600); |
const Size imgSize(800, 600); |
||||||
const Size brdSize(8, 7); |
const Size brdSize(8, 7); |
||||||
const size_t brds_num = 20; |
const size_t brds_num = 20; |
||||||
|
|
||||||
template<class T> ostream& operator<<(ostream& out, const Mat_<T>& mat) |
template<class T> ostream& operator<<(ostream& out, const Mat_<T>& mat) |
||||||
{
|
{
|
||||||
for(int j = 0; j < mat.rows; ++j) |
for(int j = 0; j < mat.rows; ++j) |
||||||
for(int i = 0; i < mat.cols; ++i) |
for(int i = 0; i < mat.cols; ++i) |
||||||
out << mat(j, i) << " ";
|
out << mat(j, i) << " ";
|
||||||
return out; |
return out; |
||||||
} |
} |
||||||
|
|
||||||
int main() |
int main() |
||||||
{
|
{
|
||||||
cout << "Initializing background...";
|
cout << "Initializing background...";
|
||||||
Mat background(imgSize, CV_8UC3);
|
Mat background(imgSize, CV_8UC3);
|
||||||
randu(background, Scalar::all(32), Scalar::all(255));
|
randu(background, Scalar::all(32), Scalar::all(255));
|
||||||
GaussianBlur(background, background, Size(5, 5), 2); |
GaussianBlur(background, background, Size(5, 5), 2); |
||||||
cout << "Done" << endl; |
cout << "Done" << endl; |
||||||
|
|
||||||
cout << "Initializing chess board generator...";
|
cout << "Initializing chess board generator...";
|
||||||
ChessBoardGenerator cbg(brdSize); |
ChessBoardGenerator cbg(brdSize); |
||||||
cbg.rendererResolutionMultiplier = 4; |
cbg.rendererResolutionMultiplier = 4; |
||||||
cout << "Done" << endl; |
cout << "Done" << endl; |
||||||
|
|
||||||
/* camera params */ |
/* camera params */ |
||||||
Mat_<double> camMat(3, 3); |
Mat_<double> camMat(3, 3); |
||||||
camMat << 300., 0., background.cols/2., 0, 300., background.rows/2., 0., 0., 1.; |
camMat << 300., 0., background.cols/2., 0, 300., background.rows/2., 0., 0., 1.; |
||||||
|
|
||||||
Mat_<double> distCoeffs(1, 5); |
Mat_<double> distCoeffs(1, 5); |
||||||
distCoeffs << 1.2, 0.2, 0., 0., 0.; |
distCoeffs << 1.2, 0.2, 0., 0., 0.; |
||||||
|
|
||||||
cout << "Generating chessboards...";
|
cout << "Generating chessboards...";
|
||||||
vector<Mat> boards(brds_num); |
vector<Mat> boards(brds_num); |
||||||
vector<Point2f> tmp; |
vector<Point2f> tmp; |
||||||
for(size_t i = 0; i < brds_num; ++i) |
for(size_t i = 0; i < brds_num; ++i) |
||||||
cout << (boards[i] = cbg(background, camMat, distCoeffs, tmp), i) << " "; |
cout << (boards[i] = cbg(background, camMat, distCoeffs, tmp), i) << " "; |
||||||
cout << "Done" << endl;
|
cout << "Done" << endl;
|
||||||
|
|
||||||
vector<Point3f> chessboard3D; |
vector<Point3f> chessboard3D; |
||||||
for(int j = 0; j < cbg.cornersSize().height; ++j) |
for(int j = 0; j < cbg.cornersSize().height; ++j) |
||||||
for(int i = 0; i < cbg.cornersSize().width; ++i) |
for(int i = 0; i < cbg.cornersSize().width; ++i) |
||||||
chessboard3D.push_back(Point3i(i, j, 0)); |
chessboard3D.push_back(Point3i(i, j, 0)); |
||||||
|
|
||||||
/* init points */ |
/* init points */ |
||||||
vector< vector<Point3f> > objectPoints;
|
vector< vector<Point3f> > objectPoints;
|
||||||
vector< vector<Point2f> > imagePoints; |
vector< vector<Point2f> > imagePoints; |
||||||
|
|
||||||
cout << endl << "Finding chessboards' corners..."; |
cout << endl << "Finding chessboards' corners..."; |
||||||
for(size_t i = 0; i < brds_num; ++i) |
for(size_t i = 0; i < brds_num; ++i) |
||||||
{ |
{ |
||||||
cout << i; |
cout << i; |
||||||
namedWindow("Current chessboard"); imshow("Current chessboard", boards[i]); waitKey(100); |
namedWindow("Current chessboard"); imshow("Current chessboard", boards[i]); waitKey(100); |
||||||
bool found = findChessboardCorners(boards[i], cbg.cornersSize(), tmp); |
bool found = findChessboardCorners(boards[i], cbg.cornersSize(), tmp); |
||||||
if (found) |
if (found) |
||||||
{ |
{ |
||||||
imagePoints.push_back(tmp); |
imagePoints.push_back(tmp); |
||||||
objectPoints.push_back(chessboard3D);
|
objectPoints.push_back(chessboard3D);
|
||||||
cout<< "-found ";
|
cout<< "-found ";
|
||||||
} |
} |
||||||
else |
else |
||||||
cout<< "-not-found ";
|
cout<< "-not-found ";
|
||||||
|
|
||||||
drawChessboardCorners(boards[i], cbg.cornersSize(), Mat(tmp), found); |
drawChessboardCorners(boards[i], cbg.cornersSize(), Mat(tmp), found); |
||||||
imshow("Current chessboard", boards[i]); waitKey(1000); |
imshow("Current chessboard", boards[i]); waitKey(1000); |
||||||
} |
} |
||||||
cout << "Done" << endl; |
cout << "Done" << endl; |
||||||
cvDestroyAllWindows(); |
cvDestroyAllWindows(); |
||||||
|
|
||||||
Mat camMat_est; |
Mat camMat_est; |
||||||
Mat distCoeffs_est; |
Mat distCoeffs_est; |
||||||
vector<Mat> rvecs, tvecs; |
vector<Mat> rvecs, tvecs; |
||||||
|
|
||||||
cout << "Calibrating..."; |
cout << "Calibrating..."; |
||||||
double rep_err = calibrateCamera(objectPoints, imagePoints, imgSize, camMat_est, distCoeffs_est, rvecs, tvecs); |
double rep_err = calibrateCamera(objectPoints, imagePoints, imgSize, camMat_est, distCoeffs_est, rvecs, tvecs); |
||||||
cout << "Done" << endl; |
cout << "Done" << endl; |
||||||
|
|
||||||
cout << endl << "Average Reprojection error: " << rep_err/brds_num/cbg.cornersSize().area() << endl; |
cout << endl << "Average Reprojection error: " << rep_err/brds_num/cbg.cornersSize().area() << endl; |
||||||
cout << "==================================" << endl; |
cout << "==================================" << endl; |
||||||
cout << "Original camera matrix:\n" << camMat << endl; |
cout << "Original camera matrix:\n" << camMat << endl; |
||||||
cout << "Original distCoeffs:\n" << distCoeffs << endl; |
cout << "Original distCoeffs:\n" << distCoeffs << endl; |
||||||
cout << "==================================" << endl; |
cout << "==================================" << endl; |
||||||
cout << "Estiamted camera matrix:\n" << (Mat_<double>&)camMat_est << endl; |
cout << "Estiamted camera matrix:\n" << (Mat_<double>&)camMat_est << endl; |
||||||
cout << "Estiamted distCoeffs:\n" << (Mat_<double>&)distCoeffs_est << endl; |
cout << "Estiamted distCoeffs:\n" << (Mat_<double>&)distCoeffs_est << endl; |
||||||
|
|
||||||
return 0; |
return 0; |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
/////////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Copy of tests/cv/src/chessboardgenerator code. Just do not want to add dependency.
|
// Copy of tests/cv/src/chessboardgenerator code. Just do not want to add dependency.
|
||||||
|
|
||||||
|
|
||||||
ChessBoardGenerator::ChessBoardGenerator(const Size& _patternSize) : sensorWidth(32), sensorHeight(24), |
ChessBoardGenerator::ChessBoardGenerator(const Size& _patternSize) : sensorWidth(32), sensorHeight(24), |
||||||
squareEdgePointsNum(200), min_cos(sqrt(2.f)*0.5f), cov(0.5),
|
squareEdgePointsNum(200), min_cos(sqrt(2.f)*0.5f), cov(0.5),
|
||||||
patternSize(_patternSize), rendererResolutionMultiplier(4), tvec(Mat::zeros(1, 3, CV_32F)) |
patternSize(_patternSize), rendererResolutionMultiplier(4), tvec(Mat::zeros(1, 3, CV_32F)) |
||||||
{
|
{
|
||||||
Rodrigues(Mat::eye(3, 3, CV_32F), rvec); |
Rodrigues(Mat::eye(3, 3, CV_32F), rvec); |
||||||
} |
} |
||||||
|
|
||||||
void cv::ChessBoardGenerator::generateEdge(const Point3f& p1, const Point3f& p2, vector<Point3f>& out) const |
void cv::ChessBoardGenerator::generateEdge(const Point3f& p1, const Point3f& p2, vector<Point3f>& out) const |
||||||
{
|
{
|
||||||
Point3f step = (p2 - p1) * (1.f/squareEdgePointsNum);
|
Point3f step = (p2 - p1) * (1.f/squareEdgePointsNum);
|
||||||
for(size_t n = 0; n < squareEdgePointsNum; ++n) |
for(size_t n = 0; n < squareEdgePointsNum; ++n) |
||||||
out.push_back( p1 + step * (float)n); |
out.push_back( p1 + step * (float)n); |
||||||
}
|
}
|
||||||
|
|
||||||
Size cv::ChessBoardGenerator::cornersSize() const |
Size cv::ChessBoardGenerator::cornersSize() const |
||||||
{ |
{ |
||||||
return Size(patternSize.width-1, patternSize.height-1); |
return Size(patternSize.width-1, patternSize.height-1); |
||||||
} |
} |
||||||
|
|
||||||
struct Mult |
struct Mult |
||||||
{ |
{ |
||||||
float m; |
float m; |
||||||
Mult(int mult) : m((float)mult) {} |
Mult(int mult) : m((float)mult) {} |
||||||
Point2f operator()(const Point2f& p)const { return p * m; }
|
Point2f operator()(const Point2f& p)const { return p * m; }
|
||||||
}; |
}; |
||||||
|
|
||||||
void cv::ChessBoardGenerator::generateBasis(Point3f& pb1, Point3f& pb2) const |
void cv::ChessBoardGenerator::generateBasis(Point3f& pb1, Point3f& pb2) const |
||||||
{ |
{ |
||||||
RNG& rng = theRNG(); |
RNG& rng = theRNG(); |
||||||
|
|
||||||
Vec3f n; |
Vec3f n; |
||||||
for(;;) |
for(;;) |
||||||
{
|
{
|
||||||
n[0] = rng.uniform(-1.f, 1.f); |
n[0] = rng.uniform(-1.f, 1.f); |
||||||
n[1] = rng.uniform(-1.f, 1.f); |
n[1] = rng.uniform(-1.f, 1.f); |
||||||
n[2] = rng.uniform(-1.f, 1.f);
|
n[2] = rng.uniform(-1.f, 1.f);
|
||||||
float len = (float)norm(n);
|
float len = (float)norm(n);
|
||||||
n[0]/=len;
|
n[0]/=len;
|
||||||
n[1]/=len;
|
n[1]/=len;
|
||||||
n[2]/=len; |
n[2]/=len; |
||||||
|
|
||||||
if (fabs(n[2]) > min_cos) |
if (fabs(n[2]) > min_cos) |
||||||
break; |
break; |
||||||
} |
} |
||||||
|
|
||||||
Vec3f n_temp = n; n_temp[0] += 100; |
Vec3f n_temp = n; n_temp[0] += 100; |
||||||
Vec3f b1 = n.cross(n_temp);
|
Vec3f b1 = n.cross(n_temp);
|
||||||
Vec3f b2 = n.cross(b1); |
Vec3f b2 = n.cross(b1); |
||||||
float len_b1 = (float)norm(b1); |
float len_b1 = (float)norm(b1); |
||||||
float len_b2 = (float)norm(b2);
|
float len_b2 = (float)norm(b2);
|
||||||
|
|
||||||
pb1 = Point3f(b1[0]/len_b1, b1[1]/len_b1, b1[2]/len_b1); |
pb1 = Point3f(b1[0]/len_b1, b1[1]/len_b1, b1[2]/len_b1); |
||||||
pb2 = Point3f(b2[0]/len_b1, b2[1]/len_b2, b2[2]/len_b2); |
pb2 = Point3f(b2[0]/len_b1, b2[1]/len_b2, b2[2]/len_b2); |
||||||
} |
} |
||||||
|
|
||||||
Mat cv::ChessBoardGenerator::generageChessBoard(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
|
Mat cv::ChessBoardGenerator::generageChessBoard(const Mat& bg, const Mat& camMat, const Mat& distCoeffs,
|
||||||
const Point3f& zero, const Point3f& pb1, const Point3f& pb2,
|
const Point3f& zero, const Point3f& pb1, const Point3f& pb2,
|
||||||
float sqWidth, float sqHeight, const vector<Point3f>& whole, |
float sqWidth, float sqHeight, const vector<Point3f>& whole, |
||||||
vector<Point2f>& corners) const |
vector<Point2f>& corners) const |
||||||
{ |
{ |
||||||
vector< vector<Point> > squares_black;
|
vector< vector<Point> > squares_black;
|
||||||
for(int i = 0; i < patternSize.width; ++i) |
for(int i = 0; i < patternSize.width; ++i) |
||||||
for(int j = 0; j < patternSize.height; ++j) |
for(int j = 0; j < patternSize.height; ++j) |
||||||
if ( (i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0) )
|
if ( (i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0) )
|
||||||
{
|
{
|
||||||
vector<Point3f> pts_square3d; |
vector<Point3f> pts_square3d; |
||||||
vector<Point2f> pts_square2d; |
vector<Point2f> pts_square2d; |
||||||
|
|
||||||
Point3f p1 = zero + (i + 0) * sqWidth * pb1 + (j + 0) * sqHeight * pb2; |
Point3f p1 = zero + (i + 0) * sqWidth * pb1 + (j + 0) * sqHeight * pb2; |
||||||
Point3f p2 = zero + (i + 1) * sqWidth * pb1 + (j + 0) * sqHeight * pb2; |
Point3f p2 = zero + (i + 1) * sqWidth * pb1 + (j + 0) * sqHeight * pb2; |
||||||
Point3f p3 = zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2; |
Point3f p3 = zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2; |
||||||
Point3f p4 = zero + (i + 0) * sqWidth * pb1 + (j + 1) * sqHeight * pb2; |
Point3f p4 = zero + (i + 0) * sqWidth * pb1 + (j + 1) * sqHeight * pb2; |
||||||
generateEdge(p1, p2, pts_square3d); |
generateEdge(p1, p2, pts_square3d); |
||||||
generateEdge(p2, p3, pts_square3d); |
generateEdge(p2, p3, pts_square3d); |
||||||
generateEdge(p3, p4, pts_square3d); |
generateEdge(p3, p4, pts_square3d); |
||||||
generateEdge(p4, p1, pts_square3d);
|
generateEdge(p4, p1, pts_square3d);
|
||||||
|
|
||||||
projectPoints( Mat(pts_square3d), rvec, tvec, camMat, distCoeffs, pts_square2d); |
projectPoints( Mat(pts_square3d), rvec, tvec, camMat, distCoeffs, pts_square2d); |
||||||
squares_black.resize(squares_black.size() + 1);
|
squares_black.resize(squares_black.size() + 1);
|
||||||
vector<Point2f> temp;
|
vector<Point2f> temp;
|
||||||
approxPolyDP(Mat(pts_square2d), temp, 1.0, true);
|
approxPolyDP(Mat(pts_square2d), temp, 1.0, true);
|
||||||
transform(temp.begin(), temp.end(), back_inserter(squares_black.back()), Mult(rendererResolutionMultiplier));
|
transform(temp.begin(), temp.end(), back_inserter(squares_black.back()), Mult(rendererResolutionMultiplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* calculate corners */ |
/* calculate corners */ |
||||||
vector<Point3f> corners3d;
|
vector<Point3f> corners3d;
|
||||||
for(int j = 0; j < patternSize.height - 1; ++j) |
for(int j = 0; j < patternSize.height - 1; ++j) |
||||||
for(int i = 0; i < patternSize.width - 1; ++i) |
for(int i = 0; i < patternSize.width - 1; ++i) |
||||||
corners3d.push_back(zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2); |
corners3d.push_back(zero + (i + 1) * sqWidth * pb1 + (j + 1) * sqHeight * pb2); |
||||||
corners.clear(); |
corners.clear(); |
||||||
projectPoints( Mat(corners3d), rvec, tvec, camMat, distCoeffs, corners); |
projectPoints( Mat(corners3d), rvec, tvec, camMat, distCoeffs, corners); |
||||||
|
|
||||||
vector<Point3f> whole3d; |
vector<Point3f> whole3d; |
||||||
vector<Point2f> whole2d; |
vector<Point2f> whole2d; |
||||||
generateEdge(whole[0], whole[1], whole3d); |
generateEdge(whole[0], whole[1], whole3d); |
||||||
generateEdge(whole[1], whole[2], whole3d); |
generateEdge(whole[1], whole[2], whole3d); |
||||||
generateEdge(whole[2], whole[3], whole3d); |
generateEdge(whole[2], whole[3], whole3d); |
||||||
generateEdge(whole[3], whole[0], whole3d); |
generateEdge(whole[3], whole[0], whole3d); |
||||||
projectPoints( Mat(whole3d), rvec, tvec, camMat, distCoeffs, whole2d); |
projectPoints( Mat(whole3d), rvec, tvec, camMat, distCoeffs, whole2d); |
||||||
vector<Point2f> temp_whole2d;
|
vector<Point2f> temp_whole2d;
|
||||||
approxPolyDP(Mat(whole2d), temp_whole2d, 1.0, true);
|
approxPolyDP(Mat(whole2d), temp_whole2d, 1.0, true);
|
||||||
|
|
||||||
vector< vector<Point > > whole_contour(1); |
vector< vector<Point > > whole_contour(1); |
||||||
transform(temp_whole2d.begin(), temp_whole2d.end(),
|
transform(temp_whole2d.begin(), temp_whole2d.end(),
|
||||||
back_inserter(whole_contour.front()), Mult(rendererResolutionMultiplier));
|
back_inserter(whole_contour.front()), Mult(rendererResolutionMultiplier));
|
||||||
|
|
||||||
Mat result; |
Mat result; |
||||||
if (rendererResolutionMultiplier == 1) |
if (rendererResolutionMultiplier == 1) |
||||||
{
|
{
|
||||||
result = bg.clone(); |
result = bg.clone(); |
||||||
drawContours(result, whole_contour, -1, Scalar::all(255), CV_FILLED, CV_AA);
|
drawContours(result, whole_contour, -1, Scalar::all(255), CV_FILLED, CV_AA);
|
||||||
drawContours(result, squares_black, -1, Scalar::all(0), CV_FILLED, CV_AA); |
drawContours(result, squares_black, -1, Scalar::all(0), CV_FILLED, CV_AA); |
||||||
} |
} |
||||||
else |
else |
||||||
{ |
{ |
||||||
Mat tmp;
|
Mat tmp;
|
||||||
resize(bg, tmp, bg.size() * rendererResolutionMultiplier); |
resize(bg, tmp, bg.size() * rendererResolutionMultiplier); |
||||||
drawContours(tmp, whole_contour, -1, Scalar::all(255), CV_FILLED, CV_AA);
|
drawContours(tmp, whole_contour, -1, Scalar::all(255), CV_FILLED, CV_AA);
|
||||||
drawContours(tmp, squares_black, -1, Scalar::all(0), CV_FILLED, CV_AA); |
drawContours(tmp, squares_black, -1, Scalar::all(0), CV_FILLED, CV_AA); |
||||||
resize(tmp, result, bg.size(), 0, 0, INTER_AREA); |
resize(tmp, result, bg.size(), 0, 0, INTER_AREA); |
||||||
}
|
}
|
||||||
return result; |
return result; |
||||||
} |
} |
||||||
|
|
||||||
Mat cv::ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, vector<Point2f>& corners) const |
Mat cv::ChessBoardGenerator::operator ()(const Mat& bg, const Mat& camMat, const Mat& distCoeffs, vector<Point2f>& corners) const |
||||||
{
|
{
|
||||||
cov = min(cov, 0.8); |
cov = min(cov, 0.8); |
||||||
double fovx, fovy, focalLen; |
double fovx, fovy, focalLen; |
||||||
Point2d principalPoint; |
Point2d principalPoint; |
||||||
double aspect; |
double aspect; |
||||||
calibrationMatrixValues( camMat, bg.size(), sensorWidth, sensorHeight,
|
calibrationMatrixValues( camMat, bg.size(), sensorWidth, sensorHeight,
|
||||||
fovx, fovy, focalLen, principalPoint, aspect); |
fovx, fovy, focalLen, principalPoint, aspect); |
||||||
|
|
||||||
RNG& rng = theRNG(); |
RNG& rng = theRNG(); |
||||||
|
|
||||||
float d1 = static_cast<float>(rng.uniform(0.1, 10.0));
|
float d1 = static_cast<float>(rng.uniform(0.1, 10.0));
|
||||||
float ah = static_cast<float>(rng.uniform(-fovx/2 * cov, fovx/2 * cov) * CV_PI / 180); |
float ah = static_cast<float>(rng.uniform(-fovx/2 * cov, fovx/2 * cov) * CV_PI / 180); |
||||||
float av = static_cast<float>(rng.uniform(-fovy/2 * cov, fovy/2 * cov) * CV_PI / 180);
|
float av = static_cast<float>(rng.uniform(-fovy/2 * cov, fovy/2 * cov) * CV_PI / 180);
|
||||||
|
|
||||||
Point3f p; |
Point3f p; |
||||||
p.z = cos(ah) * d1; |
p.z = cos(ah) * d1; |
||||||
p.x = sin(ah) * d1; |
p.x = sin(ah) * d1; |
||||||
p.y = p.z * tan(av);
|
p.y = p.z * tan(av);
|
||||||
|
|
||||||
Point3f pb1, pb2;
|
Point3f pb1, pb2;
|
||||||
generateBasis(pb1, pb2); |
generateBasis(pb1, pb2); |
||||||
|
|
||||||
float cbHalfWidth = static_cast<float>(norm(p) * sin( min(fovx, fovy) * 0.5 * CV_PI / 180)); |
float cbHalfWidth = static_cast<float>(norm(p) * sin( min(fovx, fovy) * 0.5 * CV_PI / 180)); |
||||||
float cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width; |
float cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width; |
||||||
|
|
||||||
vector<Point3f> pts3d(4); |
vector<Point3f> pts3d(4); |
||||||
vector<Point2f> pts2d(4); |
vector<Point2f> pts2d(4); |
||||||
for(;;) |
for(;;) |
||||||
{
|
{
|
||||||
pts3d[0] = p + pb1 * cbHalfWidth + cbHalfHeight * pb2; |
pts3d[0] = p + pb1 * cbHalfWidth + cbHalfHeight * pb2; |
||||||
pts3d[1] = p + pb1 * cbHalfWidth - cbHalfHeight * pb2; |
pts3d[1] = p + pb1 * cbHalfWidth - cbHalfHeight * pb2; |
||||||
pts3d[2] = p - pb1 * cbHalfWidth - cbHalfHeight * pb2; |
pts3d[2] = p - pb1 * cbHalfWidth - cbHalfHeight * pb2; |
||||||
pts3d[3] = p - pb1 * cbHalfWidth + cbHalfHeight * pb2; |
pts3d[3] = p - pb1 * cbHalfWidth + cbHalfHeight * pb2; |
||||||
|
|
||||||
/* can remake with better perf */ |
/* can remake with better perf */ |
||||||
projectPoints( Mat(pts3d), rvec, tvec, camMat, distCoeffs, pts2d); |
projectPoints( Mat(pts3d), rvec, tvec, camMat, distCoeffs, pts2d); |
||||||
|
|
||||||
bool inrect1 = pts2d[0].x < bg.cols && pts2d[0].y < bg.rows && pts2d[0].x > 0 && pts2d[0].y > 0; |
bool inrect1 = pts2d[0].x < bg.cols && pts2d[0].y < bg.rows && pts2d[0].x > 0 && pts2d[0].y > 0; |
||||||
bool inrect2 = pts2d[1].x < bg.cols && pts2d[1].y < bg.rows && pts2d[1].x > 0 && pts2d[1].y > 0; |
bool inrect2 = pts2d[1].x < bg.cols && pts2d[1].y < bg.rows && pts2d[1].x > 0 && pts2d[1].y > 0; |
||||||
bool inrect3 = pts2d[2].x < bg.cols && pts2d[2].y < bg.rows && pts2d[2].x > 0 && pts2d[2].y > 0; |
bool inrect3 = pts2d[2].x < bg.cols && pts2d[2].y < bg.rows && pts2d[2].x > 0 && pts2d[2].y > 0; |
||||||
bool inrect4 = pts2d[3].x < bg.cols && pts2d[3].y < bg.rows && pts2d[3].x > 0 && pts2d[3].y > 0; |
bool inrect4 = pts2d[3].x < bg.cols && pts2d[3].y < bg.rows && pts2d[3].x > 0 && pts2d[3].y > 0; |
||||||
|
|
||||||
if ( inrect1 && inrect2 && inrect3 && inrect4) |
if ( inrect1 && inrect2 && inrect3 && inrect4) |
||||||
break; |
break; |
||||||
|
|
||||||
cbHalfWidth*=0.8f; |
cbHalfWidth*=0.8f; |
||||||
cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width;
|
cbHalfHeight = cbHalfWidth * patternSize.height / patternSize.width;
|
||||||
} |
} |
||||||
|
|
||||||
cbHalfWidth *= static_cast<float>(patternSize.width)/(patternSize.width + 1); |
cbHalfWidth *= static_cast<float>(patternSize.width)/(patternSize.width + 1); |
||||||
cbHalfHeight *= static_cast<float>(patternSize.height)/(patternSize.height + 1); |
cbHalfHeight *= static_cast<float>(patternSize.height)/(patternSize.height + 1); |
||||||
|
|
||||||
Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2; |
Point3f zero = p - pb1 * cbHalfWidth - cbHalfHeight * pb2; |
||||||
float sqWidth = 2 * cbHalfWidth/patternSize.width; |
float sqWidth = 2 * cbHalfWidth/patternSize.width; |
||||||
float sqHeight = 2 * cbHalfHeight/patternSize.height; |
float sqHeight = 2 * cbHalfHeight/patternSize.height; |
||||||
|
|
||||||
return generageChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2, sqWidth, sqHeight, pts3d, corners);
|
return generageChessBoard(bg, camMat, distCoeffs, zero, pb1, pb2, sqWidth, sqHeight, pts3d, corners);
|
||||||
} |
} |
||||||
|
|
@ -0,0 +1,78 @@ |
|||||||
|
#include "opencv2/highgui/highgui.hpp" |
||||||
|
#include "opencv2/imgproc/imgproc.hpp" |
||||||
|
|
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
using namespace cv; |
||||||
|
using namespace std; |
||||||
|
|
||||||
|
Mat img, inpaintMask; |
||||||
|
Point prevPt(-1,-1); |
||||||
|
|
||||||
|
void onMouse( int event, int x, int y, int flags, void* ) |
||||||
|
{ |
||||||
|
if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) ) |
||||||
|
prevPt = Point(-1,-1); |
||||||
|
else if( event == CV_EVENT_LBUTTONDOWN ) |
||||||
|
prevPt = Point(x,y); |
||||||
|
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) ) |
||||||
|
{ |
||||||
|
Point pt(x,y); |
||||||
|
if( prevPt.x < 0 ) |
||||||
|
prevPt = pt; |
||||||
|
line( inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0 ); |
||||||
|
line( img, prevPt, pt, Scalar::all(255), 5, 8, 0 ); |
||||||
|
prevPt = pt; |
||||||
|
imshow("image", img); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
int main( int argc, char** argv ) |
||||||
|
{ |
||||||
|
char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg"; |
||||||
|
Mat img0 = imread(filename, -1); |
||||||
|
if(img0.empty()) |
||||||
|
{ |
||||||
|
cout << "Usage: inpaint <image_name>\n"; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
cout << "Hot keys: \n" |
||||||
|
"\tESC - quit the program\n" |
||||||
|
"\tr - restore the original image\n" |
||||||
|
"\ti or SPACE - run inpainting algorithm\n" |
||||||
|
"\t\t(before running it, paint something on the image)\n"; |
||||||
|
|
||||||
|
namedWindow( "image", 1 ); |
||||||
|
|
||||||
|
img = img0.clone(); |
||||||
|
inpaintMask = Mat::zeros(img.size(), CV_8U); |
||||||
|
|
||||||
|
imshow("image", img); |
||||||
|
setMouseCallback( "image", onMouse, 0 ); |
||||||
|
|
||||||
|
for(;;) |
||||||
|
{ |
||||||
|
char c = (char)waitKey(); |
||||||
|
|
||||||
|
if( c == 27 ) |
||||||
|
break; |
||||||
|
|
||||||
|
if( c == 'r' ) |
||||||
|
{ |
||||||
|
inpaintMask = Scalar::all(0); |
||||||
|
img0.copyTo(img); |
||||||
|
imshow("image", img); |
||||||
|
} |
||||||
|
|
||||||
|
if( c == 'i' || c == ' ' ) |
||||||
|
{ |
||||||
|
Mat inpainted; |
||||||
|
inpaint(img, inpaintMask, inpainted, 3, CV_INPAINT_TELEA); |
||||||
|
imshow("inpainted image", inpainted); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
Loading…
Reference in new issue