前提・実現したいこと
私は画像認識プログラムを作成していました.
今回参考として以下のページ
(http://ishidate.my.coocan.jp/opencv_17/opencv_17.htm)
のプログラムをvisualstudio2010に書き,ビルドした結果エラーがでました.
プログラミング初心者であり参考書片手に問題を解決しようとしましたが,いくつかの解決策を試しましたが別のエラーが出たため結局動くことはありませんでした.
よろしければ解決方法をご教授いただきたいです.
よろしくお願いします.
該当のソースコード
C++
#include "stdafx.h" #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/calib3d/calib3d.hpp> #include <iostream> #include <iomanip> //#include <string> //#include <string.h> //#include <sstream> using namespace std; using namespace cv; /* string itos(int x) { stringstream y; y<<x; return y.str(); } */ int _tmain(int argc, _TCHAR* argv[]) { const int BOARD_W = 9; //チェスボードの横方向コーナーの数 const int BOARD_H = 6; //チェスボードの縦方向コーナーの数 const Size BOARD_SIZE = Size (BOARD_W, BOARD_H); const int N_CORNERS = BOARD_W*BOARD_H; const int N_BOARDS = 10; //チェスボードの撮影枚数 const float SCALE = 30; //チェスボードの正方形の辺の長さ (mm) const Size IM_SIZE = Size (512, 384); //撮影写真のピクセル数 //----------- 原画像を読み込む -------- vector<Mat> src_image (N_BOARDS); for (int i = 0; i <N_BOARDS; i++) { src_image[i] = imread ("cboard" + itos(i) + ".png"); } //--------- imagePointsを取得する---------- vector<vector<Point2f>>imagePoints; vector<Point2f> imageCorners; //このvectorがimagePoints Mat gray_image; bool found; Mat dst_image; for (int i = 0; i < N_BOARDS; i++) { //---------- コーナーを検出する ---------- found = findChessboardCorners(src_image[i], BOARD_SIZE , imageCorners); //------------ 精度を高める -------------- cvtColor(src_image[i], gray_image, CV_BGR2GRAY); cornerSubPix(gray_image, imageCorners, Size (9, 9), Size(-1, -1), TermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 30, 0.1)); imagePoints.push_back(imageCorners); } //---------object Pointsを設定する-------- vector<vector<Point3f>> objectPoints; vector<Point3f>objectCorners; //このvectorがobjectPoints for (int j = 0; j < BOARD_H; j++) for (int i = 0; i < BOARD_W; i++) { objectCorners.push_back (Point3f(i*SCALE, j*SCALE, 0.0f)); } for (int i = 0; i < N_BOARDS; i++) { objectPoints.push_back(objectCorners); } //-----カメラキャリブレーションを行う----- Mat cameraMatrix; Mat distCoeffs; vector<Mat> rvecs; vector<Mat> tvecs; double rms = calibrateCamera (objectPoints, imagePoints, Size(512, 384), cameraMatrix, distCoeffs, rvecs, tvecs); //------ キャリブレーション結果の表示 cout << fixed << right; cout << "Re-projection Error (unit: pixel)" << endl; cout <<" " << rms << endl; cout << endl; cout << "cameraMatrix (unit: pixel)" << endl; cout << " fx=" << cameraMatrix.at<double>(0, 0); cout << " fy=" << cameraMatrix.at<double>(1, 1); cout << " cX=" << cameraMatrix.at<double>(0, 2); cout << " cy=" << cameraMatrix.at<double>(1, 2); cout << endl << endl; cout << "distCoeffs" << endl; cout << " k1=" << distCoeffs.at<double> (0, 0); cout << " k2=" << distCoeffs.at<double> (0, 1); cout << " p1=" << distCoeffs.at<double> (0, 2); cout << " p1=" << distCoeffs.at<double> (0, 3); cout << " k3=" << distCoeffs.at<double> (0, 4); cout << endl << endl; //---- キャリブレーション結果を格納する ---- FileStorage fs("calibration.xml", FileStorage::WRITE); fs << "cameraMatrix" << cameraMatrix; fs << "distCoeffs" << distCoeffs; fs.release(); //------- rvecsとtvecsの表示(参考) --------- cout << "rotation vector" << endl; for (int i = 0; i < N_BOARDS; i++) { cout << "cboard" + itos(i); for (int j = 0; j < 3; j++) { cout << " " << setw(9) << setprecision(4) << rvecs[i].at<double>(j, 0); } cout << endl; } cout << endl; cout << "translation vector" << endl; for (int i = 0; i < N_BOARDS; i++) { cout << "cboard" + itos(i); for (int j = 0; j < 3; j++) { cout << " " << setw(9) << setprecision(4) << tvecs[i].at<double>(j, 0); } cout << endl; } cout << endl << endl; //----------画像を補正する (例) ---------- undistort(src_image [3], dst_image, cameraMatrix, distCoeffs); namedWindow("補正画像 "); imshow ("補正画像 ", dst_image); waitKey(0); destroyAllWindows(); return 0; }
試したこと
①「#include <string>, #include <string.h>, #include <sstream>」の追加
②to_string の定義文(?)の追加
③int から stringにしたいので 「itos」を作成してmain文内でitosを使用
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
・①だけではエラーは一個も減りませんでした.(当然ですが増えもしませんでした)
・②追加したソースコード↓
namespace patch
{
template < typename T > std::string itos( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
①と同様エラーに変化なし
・③上記の②の部分をコメントアウトしてitosを追加したところ本プログラムのobjファイルがエラーを吐きました.
*以下一部記載↓
エラー 1 error LNK2019: 未解決の外部シンボル "public: virtual __cdecl cv::FileStorage::~FileStorage(void)" (??1FileStorage@cv@@UEAA@XZ) が関数 wmain で参照されました。 \○○ファイル\abc.obj
エラー 2 error LNK2019: 未解決の外部シンボル "class cv::_OutputArray const & __cdecl cv::noArray(void)" (?noArray@cv@@YAAEBV_OutputArray@1@XZ) が関数 wmain で参照されました。 \○○ファイル\abc.obj
エラー 3 error LNK2019: 未解決の外部シンボル "public: virtual void __cdecl cv::FileStorage::release(void)" (?release@FileStorage@cv@@UEAAXXZ) が関数 wmain で参照されました。 \○○ファイル\abc.obj
エラー 4 error LNK2019: 未解決の外部シンボル "public: __cdecl cv::FileStorage::FileStorage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0FileStorage@cv@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H0@Z) が関数 wmain で参照されました。 \○○ファイル\abc.obj
エラー 5 error LNK2019: 未解決の外部シンボル "public: __cdecl cv::_OutputArray::_OutputArray(class std::vector<class cv::Mat,class std::allocator<class cv::Mat> > &)" (??0_OutputArray@cv@@QEAA@AEAV?$vector@VMat@cv@@V?$allocator@VMat@cv@@@std@@@std@@@Z) が関数 wmain で参照されました。 \○○ファイル\abc.obj
上記のようなエラー37個が発生.
発生している問題・エラーメッセージ
対策を講じる前のエラー(ソースコード内にto_stringが3つあるため下記エラーが3つ重複しています)
エラー 1 error C2668: 'std::to_string' : オーバーロード関数の呼び出しを解決することができません。(新機能 ; ヘルプを参照) \○○ファイル\abc.cpp 48
エラー 4 IntelliSense: オーバーロードされた関数 "to_string" の複数のインスタンスが引数リストと一致します: \○○ファイル\abc.cpp 48
補足情報(FW/ツールのバージョンなど)
VisualStudio 2010
OpenCV 2.4.10
回答1件
あなたの回答
tips
プレビュー