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