paella/Code/src/Extern/main.cpp

212 lines
7.5 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Paella
// Copyright (C) 2015 - Thomas FORGIONE, Emilie JALRAS, Marion LENFANT, Thierry MALON, Amandine PAILLOUX
// Authors :
// Thomas FORGIONE
// Emilie JALRAS
// Marion LENFANT
// Thierry MALON
// Amandine PAILLOUX
// Simone GASPARINI
//
// This file is part of the project Paella
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
////////////////////////////////////////////////////////////////////////////////
#include "Extern/ChessboardCameraTracker.hpp"
#include "Extern/utility.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <time.h>
// Display the help for the programm
void help( const char* programName );
// parse the input command line arguments
bool parseArgs( int argc, char**argv, cv::Size &boardSize, std::string &inputFilename );
int main( int argc, char** argv )
{
/******************************************************************/
/* CONSTANTS to use */
/******************************************************************/
// the name of the window
const std::string WINDOW_NAME = "Image View";
/******************************************************************/
/* VARIABLES TO use */
/******************************************************************/
cv::Mat view; // it will contain the original image loaded from file
Camera cam;
cam.init("/home/thomas/Paella/Code/data/xml/calib.xml");
ChessboardCameraTracker tracker;
cv::Mat pose;
std::vector<cv::Point2f> pointbuf; // it will contain the detected corners on the chessboard
// it will contain the size in terms of corners (width X height) of the chessboard
cv::Size boardSize;
// it will contains the filename of the image file
std::string inputFilename;
/******************************************************************/
/* READ THE INPUT PARAMETERS - DO NOT MODIFY */
/******************************************************************/
if( !parseArgs( argc, argv, boardSize, inputFilename) )
{
std::cerr << "Aborting..." << std::endl;
return EXIT_FAILURE;
}
/******************************************************************/
/* PART TO DEVELOP */
/******************************************************************/
/******************************************************************/
// create a window to display the image --> see namedWindow
/******************************************************************/
cv::namedWindow(WINDOW_NAME, CV_WINDOW_AUTOSIZE );
/******************************************************************/
// read the input image from file into "view" --> see imread
/******************************************************************/
view = cv::imread(inputFilename, CV_LOAD_IMAGE_COLOR);
//Measure the execution time, get time before function call
// double t = (double)cv::getTickCount();
/******************************************************************/
// call the function that detects the chessboard on the image
// found = detectChessboard...
/******************************************************************/
// found = detectChessboard(view, pointbuf, boardSize, pattern);
// // get time after function call and display info
// t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
// std::cout << ( (!found ) ? ( "No " ) : ("") ) << "chessboard detected!" << std::endl;
// std::cout << "Chessboard detection took " << t*1000 << "ms" << std::endl;
// /******************************************************************/
// // if the chessboard is found draw the cornerns on top of it
// // --> see drawChessboardCorners
// /******************************************************************/
// cv::drawChessboardCorners(view, boardSize, pointbuf, found);
if(tracker.process(view, pose, cam, boardSize))
{
std::cout << "Drawing Reference system"<< std::endl;
drawReferenceSystem(view,cam,pose,1,200,false );
}
else
{
std::cout << "Unable to process" << std::endl;
}
/******************************************************************/
// show the image inside the window --> see imshow
/******************************************************************/
// cv::resize(view, view, cv::Size{view.cols/5, view.rows/5});
cv::imshow(WINDOW_NAME, view);
// wait for user input before ending --> see waitKey
cv::waitKey( -1 );
return EXIT_SUCCESS;
}
// Display the help for the programm
void help( const char* programName )
{
std::cout << "Detect a chessboard in a given image" << std::endl
<< "Usage: " << programName << std::endl
<< " -w <board_width> # the number of inner corners per one of board dimension" << std::endl
<< " -h <board_height> # the number of inner corners per another board dimension" << std::endl
<< " [-pt <pattern=[circles|acircles|chess]>] # the type of pattern: chessboard or circles' grid" << std::endl
<< " <image file> " << std::endl
<< std::endl;
}
// parse the input command line arguments
bool parseArgs( int argc, char**argv, cv::Size &boardSize, std::string &inputFilename )
{
// check the minimum number of arguments
if( argc < 3 )
{
help( argv[0] );
return false;
}
// Read the input arguments
for( int i = 1; i < argc; i++ )
{
const char* s = argv[i];
if( strcmp( s, "-w" ) == 0 )
{
if( sscanf( argv[++i], "%u", &boardSize.width ) != 1 || boardSize.width <= 0 )
{
std::cerr << "Invalid board width" << std::endl;
return false;
}
}
else if( strcmp( s, "-h" ) == 0 )
{
if( sscanf( argv[++i], "%u", &boardSize.height ) != 1 || boardSize.height <= 0 )
{
std::cerr << "Invalid board height" << std::endl;
return false;
}
}
else if( s[0] != '-' )
{
inputFilename.assign(s);
}
else
{
std::cerr << "Unknown option " << s << std::endl;
return false;
}
}
return true;
}