/*!
 Find and return markers
 */
void MarkerDetector::findMarkers( cv::Mat& src, std::vector<Marker>& markers)
{
    
    // convert image to a grayscale image.
    cv::cvtColor(src, _grayscale, CV_BGRA2GRAY);

    
    // Make it binary
    performThreshold(_grayscale, _threshold_image);
    
    _contours.clear();
    
    // Detect contours
    findContours(_threshold_image, _contours, _grayscale.cols / 5);
    
    // Find closed contours that can be approximated with 4 points
    findCandidates(_contours, detectedMarkers);
    
    // Find is them are markers
    recognizeMarkers(_grayscale, detectedMarkers);
    
    // Calculate their poses
    estimatePosition(detectedMarkers);
    
    //sort by id
    std::sort(detectedMarkers.begin(), detectedMarkers.end());

    markers = detectedMarkers;
}
bool MarkerDetector::findMarkers(const BGRAVideoFrame& frame, std::vector<Marker>& detectedMarkers)
{
  cv::Mat bgraMat(frame.height, frame.width, CV_8UC3, frame.data, frame.stride);
  
  // Convert the image to grayscale
  prepareImage(bgraMat, m_grayscaleImage);
  
  // Make it binary
  performThreshold(m_grayscaleImage, m_thresholdImg);
  
  // Detect contours
  findContours(m_thresholdImg, m_contours, m_grayscaleImage.cols / 20);
  
  // Find closed contours that can be approximated with 4 points
  findMarkerCandidates(m_contours, detectedMarkers);
  
  // Find is them are markers
  detectMarkers(m_grayscaleImage, detectedMarkers);

  // Calcualte their poses
  estimatePosition(detectedMarkers);

  //sort by id
  std::sort(detectedMarkers.begin(), detectedMarkers.end());
  return false;
}
bool MarkerDetector::findMarkers(const cv::Mat& grayscale, std::vector<Marker>& detectedMarkers)
{

	if (grayscale.type()!=CV_8UC1) 
		throw cv::Exception(9000,"input.type()!=CV_8UC1","MarkerDetector::findMarkers",__FILE__,__LINE__);
    // Prepare the image for processing
    //prepareImage(frame, m_grayscaleImage);

    // Make it binary
    performThreshold(grayscale, m_thresholdImg);
	//cv::imshow("threshold", m_thresholdImg);
	//cv::waitKey(1);
	//cv::imshow("resul2", m_thresholdImg);
	//cv::waitKey(1);

    // Detect contours
	
	findCandidates(m_thresholdImg, detectedMarkers, m_thresholdImg.cols*0.16,m_thresholdImg.cols*2);

	
	/*
	for (size_t i=0; i<m_contours.size(); i++)
    {
			drawContours( frame, m_contours, i,  cv::Scalar(0,0,255), CV_FILLED, 8);
		
    }
	cv::imshow("resul2", frame);
	cv::waitKey(0);*/

    // Find closed contours that can be approximated with 4 points
   
    // Find is them are markers
    recognizeMarkers(grayscale, detectedMarkers);
	
    // Calculate their poses
    //estimatePosition(detectedMarkers);

    //sort by id
    //std::sort(detectedMarkers.begin(), detectedMarkers.end());
    return false;
}
bool MarkerDetector::findMarkers(const cv::Mat& frame, std::vector<Marker>& detectedMarkers)
{
    // Convert the image to grayscale
    prepareImage(frame, m_grayscaleImage);

    // Make it binary
    performThreshold(m_grayscaleImage, m_thresholdImg);

    // Detect contours
    findContours(m_thresholdImg, m_contours, m_grayscaleImage.cols / 5);

    // Find closed contours that can be approximated with 4 points
    findCandidates(m_contours, detectedMarkers);

    // Find is them are markers
    recognizeMarkers(m_grayscaleImage, detectedMarkers);

    // Calculate their poses
    estimatePosition(detectedMarkers);

    //sort by id
    std::sort(detectedMarkers.begin(), detectedMarkers.end());
    return false;
}