0

基本的に画像を取り込んでチェス盤を見つけようとするプログラムがあります。成功すると、画像が .jpg ファイルに書き込まれます。しかし、新しく作成された .jpg ファイルでチェス盤を見つけようとすると、失敗します。誰かが理由を知っていますか?

これが私のコードです:

#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 ;
} 
4

1 に答える 1