我有一个程序,它基本上接收图像并尝试找到一个棋盘。如果成功,它将图像写入 .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 ;
}