ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Unable to re-find chessboard corners

asked 2013-05-07 12:05:30 -0500

joshualan gravatar image

updated 2014-01-28 17:16:28 -0500

ngrennan gravatar image

I have a program that essentially takes in images and attempts to find a chessboard. If successful, it writes the image into a .jpg file. However, when I re-try to find the chessboard on the newly created .jpg file, I fail. Does anyone know why?

Here is my code:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/opencv.hpp>
#include <vector>

namespace enc = sensor_msgs::image_encodings;

using namespace cv ;

int board_w ;
int board_h ;

int findChess(cv::Mat image) {
  cv::Size board_sz( board_w, board_h );
  int numSquares = board_w * board_h ;
  std::vector<cv::Point2f> corners ;

  std::vector<cv::Point3f> obj ;  
  for ( int i = 0 ; i < numSquares ; i++ ) {
    obj.push_back( cv::Point3f( i / board_w, i % board_w, 0 ) ) ;
  } 

  // find the inner corners of the chessboard
  int found = cv::findChessboardCorners( image, board_sz, corners,
                     cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE |
                     cv::CALIB_CB_FAST_CHECK ) ;

  if ( !found ) {
    ROS_ERROR( "Couldn't find the corners, re-trying" ) ;
    return -1 ;
  }

  // draw the found corners on a copy of the original image
  if (found) {
    ROS_INFO( "Yeah, we found the board." ) ;

    cv::Mat tmp = image.clone() ;
    cv::Mat generatedImage ;

    cv::drawChessboardCorners( tmp, board_sz, corners, 1) ;
    cv::namedWindow( "FOUND IMAGE" ) ;
    cv::imshow( "FOUND IMAGE" , tmp ) ;
    cv::imwrite( "found.jpeg", image ) ;

    generatedImage = imread( "found.jpeg", -1 ) ;
      // find the inner corners of the chessboard

    int found = cv::findChessboardCorners( generatedImage, board_sz, corners,
                       cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE |
                       cv::CALIB_CB_FAST_CHECK ) ;

    if (found) {
      ROS_INFO( "Corners are refound." ) ;
    }
    else {
      ROS_INFO( "Corners are not refound." ) ;
    }

    waitKey(0) ;
  }

  return 0 ;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-05-07 15:55:36 -0500

joshualan gravatar image

If you save it as a .png file instead of a .jpeg, the quality of the image increases and findChessboardCorners works fine.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-05-07 12:05:30 -0500

Seen: 747 times

Last updated: May 07 '13