このチュートリアルでは、'mcc'モジュールを使って画像中のカラーチャートを検出する方法を学ぶ。ここでは基本的な検出アルゴリズムのみを使用する。次のチュートリアルでは、ニューラルネットワークを用いて検出精度を向上させる方法を見ていく。
ビルド
OpenCVをビルドする際、contribモジュールをすべてビルドするには次のコマンドを実行する。
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/
あるいはmccモジュールのみをビルドする。
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules/mcc
あるいはCMakeのGUI版 cmake-gui でmccモジュールにチェックを入れていることを確認する。
サンプルのソースコード
run
<path_of_your_opencv_build_directory>/bin/example_mcc_chart_detection -t=<type_of_chart> -v=<optional_path_to_video_if_not_provided_webcam_will_be_used.mp4> --ci=<optional_camera_id_needed_only_if_video_not_provided> --nc=<optional_maximum_number_of_charts_to_look_for>
- -t=# はチャートの種類で、0 (Standard)、1 (DigitalSG)、2 (Vinyl)
- –ci=# はカメラIDで、0 (デフォルトはメインカメラ)、1 (セカンダリカメラ) など
- –nc=# デフォルト値は1で、最良のチャートのみを検出することを意味する
例:
Run a movie on a standard macbeth chart:
/home/opencv/build/bin/example_mcc_chart_detection -t=0 -v=mcc24.mp4
Or run on a vinyl macbeth chart from camera 0:
/home/opencv/build/bin/example_mcc_chart_detection -t=2 --ci=0
Or run on a vinyl macbeth chart, detecting the best 5 charts(Detections can be less than 5 but never more):
/home/opencv/build/bin/example_mcc_chart_detection -t=2 --ci=0 --nc=5
11const char *about =
"Basic chart detection";
13 "{ help h usage ? | | show this message }"
14 "{t | | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl }"
15 "{v | | Input from video file, if ommited, input comes from camera }"
16 "{ci | 0 | Camera id if input doesnt come from video (-v) }"
17 "{nc | 1 | Maximum number of charts in the image }"};
19int main(
int argc,
char *argv[])
29 int t = parser.get<
int>(
"t");
32 TYPECHART chartType = TYPECHART(t);
34 int camId = parser.get<
int>(
"ci");
35 int nc = parser.get<
int>(
"nc");
38 video = parser.get<
String>(
"v");
50 inputVideo.
open(video);
55 inputVideo.
open(camId);
63 while (inputVideo.
grab())
68 imageCopy = image.
clone();
71 if (!detector->process(image, chartType, nc))
73 printf(
"ChartColor not detected \n");
79 std::vector<Ptr<mcc::CChecker>> checkers = detector->getListColorChecker();
88 imshow(
"image result | q or esc to quit", image);
89 imshow(
"original", imageCopy);
90 char key = (char)
waitKey(waitTime);
Designed for command line parsing.
Definition utility.hpp:890
n-dimensional dense array class
Definition mat.hpp:840
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:786
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.
virtual bool retrieve(OutputArray image, int flag=0)
Decodes and returns the grabbed video frame.
virtual bool grab()
Grabs the next frame from video file or capturing device.
std::string String
Definition cvstd.hpp:151
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition base.hpp:423
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
解説
ヘッダと名前空間を設定する
必要であれば、上記のコードのように名前空間を設定できる。
検出器オブジェクトを生成する
これは単にオブジェクトを生成するだけである。
検出器を実行する
detector->process(image, chartType);
検出器が少なくとも1つのチャートを検出できた場合はtrueを返し、そうでなければfalseを返す。上記のコードでは、チャートが検出されなかった場合に失敗メッセージを表示している。検出に成功した場合は、カラーチャートのリストが検出器自身の内部に格納される。次のステップでその抽出方法を説明する。デフォルトでは最大1つのチャートを検出するが、第3引数のnc(チャートの最大数)を調整することで、より多くのチャートを検出できる。
ColorCheckerの一覧を取得する
std::vector<cv::Ptr<mcc::CChecker>> checkers;
detector->getListColorChecker(checkers);
検出されたすべてのcolorcheckerが、これで 'checkers' ベクトルに格納される。
colorcheckerを画像に描き戻す
すべてのcheckerを1つずつループで処理し、描画する。