はじめに
本稿では、チェスボードの「かど」を捜すプログラムを下記の手法で解説します。
- プログラムについて、ひとこと…。
- Source Code
それでは、参りましょう。
プログラムについて、ひとこと…。
基本的な方針としては、findChessboardCorners() Functionにループの中でチェスボードパターンの「かど」を捜してもらって、見つかったらループから抜けてもらって、画像に表示してもらいます。
プログラムの構成は3部。Parameterの宣言と画像の取得、ループ内で「かど」を探索、そしてループを出てから表示という形になります。
Source Code
Source Code は下にリストアップしておきます。参考のため… 。
void CMFC_OpenCV_StereoDoc::OnImageprocessChessboardcornerdetect()
{
// TODO: ここにコマンド ハンドラー コードを追加します。
bool found;
Size boardSize(6, 6);
vector<Point2f> corners;
// Input images from a camere
VideoCapture cap(0);
// The "for(;;)" loop searches for the chess board corners. Exit from the loop when these are found or press "Esc" key.
//
for (;;)
{
// Now "cap" is the image that shall be directed to "frame."
cap >> frame0;
namedWindow("frame", 1); // Name the "frame" window
imshow("frame", frame0); // Display the "frame" buffer.
cv::cvtColor(frame0, frame1, COLOR_BGR2GRAY); // This line converts the color image "frame" to the grey scale image "frame1."
// The command below finds the chess board corners. The numbers of corners (width and height in size) must be specified. Please refer to "OpenCV Reference Manual" or "Tutorial."
found = findChessboardCorners(frame1, boardSize, corners/*, CALIB_CB_FAST_CHECK*/ /*CALIB_CB_NORMALIZE_IMAGE*/ /*, CALIB_CB_ADAPTIVE_THRESH*/);
/* Exit from loop if chessboard corners are found. */
if (found != 0) break;
/* Press "Esc" key to exit from this loop. */
if (waitKey(27) >= 0) break;
} // End of "for{;;} roop"
// The function below draws the chessboard corners which are found on the "frame1" image. Be careful this command does not display the "frame1" image.
drawChessboardCorners(frame1, boardSize, corners, found);
namedWindow("frame1", 1); // Name the "frame1" window.
imshow("frame1", frame1); // Now, I display "frame1" image.
}
まとめ
如何でしたか。本稿ではチェスボードの「かど」を捜すプログラムの手法について解説し、Source Codeを開示しています。最後までお読みいただき、ありがとうございました。