Visual Studio 2013 Express
 Visual C++( アンマネージドコード )
 OpenCV 3.1

■270.OpenCV 図形描画 Prev Top Next
関連ページ:

図形を描画します。


---main.cpp---

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

#pragma comment( lib, "opencv_world300d.lib" )

int main(int argc, const char* argv[])
{
   int hr = -1;

   try
   {
      // 画像領域を確保
      cv::Mat circle = cv::Mat::zeros(cvSize(512, 384), CV_8UC3);

      // 円を描画
      cv::circle(circle,
         cv::Point(circle.cols * 0.5, circle.rows * 0.5),   // 円の中心座標.
         100,                                         // 円の半径.
         cv::Scalar(255, 0, 0),                       // 円の色.
         5,                                           // 円の枠線の太さ.負の値の場合,円が塗りつぶされる
         cv::LINE_8                                   // 円の枠線の種類
         );

      cv::namedWindow("circle", 1);
      cv::imshow("circle", circle);

      // ************************************************************************************************

      // 画像領域を確保
      cv::Mat ellipse = cv::Mat::zeros(cvSize(512, 384), CV_8UC3);

      // 楕円,楕円弧,塗りつぶされた楕円,塗りつぶされた扇形を描画
      cv::ellipse(ellipse,
         cv::Point(ellipse.cols * 0.5, ellipse.rows * 0.5),   // 楕円の中心座標.
         cv::Size(100, 50),        // 楕円の長径と短径.
         0,                        // 楕円を右回りで回転( 度単位 )
         0,                        // 楕円の開始角度( 度単位 )
         270,                      // 楕円の終了角度( 度単位 )
         cv::Scalar(255, 0, 0),    // 楕円の色
         5,                        // 楕円の枠線の太さ
         cv::LINE_8                // 円の枠線の種類
         );

      cv::namedWindow("ellipse", 1);
      cv::imshow("ellipse", ellipse);

      // ************************************************************************************************

      // 画像領域を確保
      cv::Mat fillConvexPoly = cv::Mat::zeros(cvSize(512, 384), CV_8UC3);

      cv::Point pts[] =
      { 
         cv::Point(100, 100),
         cv::Point(120, 180),
         cv::Point(190, 250),
         cv::Point(320, 120),
         cv::Point(220, 50),
      };

      // 塗りつぶされた凸ポリゴンを描画
      cv::fillConvexPoly(fillConvexPoly,
         pts,                     // ポリゴンの頂点座標
         _countof(pts),           // 頂点数
         cv::Scalar( 255, 0, 0),  // ポリゴンの色
         cv::LINE_8
         );

      cv::namedWindow("fillConvexPoly", 1);
      cv::imshow("fillConvexPoly", fillConvexPoly);

      // ************************************************************************************************

      // 画像領域を確保
      cv::Mat fillPoly = cv::Mat::zeros(cvSize(512, 384), CV_8UC3);

      cv::Point point2[][3] =
      {
         { cv::Point(200, 200), cv::Point(120, 300), cv::Point(280, 300) },
         { cv::Point(280, 300), cv::Point(350, 150), cv::Point(200, 200) },
      };

      const cv::Point *pts2[] = { point2[0], point2[1] };

      int npts2[] = { 3, 3 };

      // 1つ,または複数のポリゴンを描画
      cv::fillPoly(fillPoly,
         pts2,                  // ポリゴンの頂点座標
         npts2,                 // 各ポリゴンの頂点数の配列.
         _countof(npts2),       // ポリゴン数
         cv::Scalar(255, 0, 0), // ポリゴンの色
         cv::LINE_8
         );

      cv::namedWindow("fillPoly", 1);
      cv::imshow("fillPoly", fillPoly);

      // ************************************************************************************************

      // 画像領域を確保
      cv::Mat putText = cv::Mat::zeros(cvSize(512, 384), CV_8UC3);

      int baseline = 0;

      std::string text = "abcdefg0123";
      int fontFace = cv::FONT_HERSHEY_SIMPLEX;
      double fontScale = 2;
      int thickness = 3;

      // テキストのサイズを取得
      cv::Size textSize = cv::getTextSize(
         text,
         fontFace,     // 利用するフォント
         fontScale,    // フォントのスケール
         thickness,    // テキストの描画に利用される線の太さ
         &baseline     // 出力パラメータ.文字列の最下点から見たベースラインの y 座標
         );

      // テキストをセンタリングします.
      cv::Point textOrg = cv::Point((putText.cols - textSize.width) / 2,
                                    (putText.rows + textSize.height) / 2);

      // 矩形を描画
      cv::rectangle(putText,
         textOrg + cv::Point(0, baseline),                        // 矩形の1つの頂点座標
         textOrg + cv::Point(textSize.width, -textSize.height),   // 矩形の反対側の頂点座標
         cv::Scalar(0, 0, 255),                                   // 色
         2,                                                       // 矩形の枠線の太さ
         cv::LINE_8                                               // 枠線の種類
         );

      // テキスト描画
      cv::putText(putText,
         text,                   // 文字列
         textOrg,                // 文字列の左下角の,画像中の座標.
         fontFace,               // フォントの種類
         fontScale,              // フォントのスケールファクタ
         cv::Scalar::all(255),   // 色
         thickness,              // 線の太さ
         cv::LINE_AA             // 線の種類( アンチエイリアス )
         );

      cv::namedWindow("putText", 1);
      cv::imshow("putText", putText);

      // ************************************************************************************************

      // 画像領域を確保
      cv::Mat line = cv::Mat::zeros(cvSize(512, 384), CV_8UC3);

      cv::line(line,
         cv::Point(200, 200),     // 1つ目の線分の座標
         cv::Point(300, 300),     // 2つ目の線分の座標
         cv::Scalar(0, 255, 0),   // 色
         2,                       // 太さ
         cv::LINE_8
         );

      cv::namedWindow("line", 1);
      cv::imshow("line", line);

      // ************************************************************************************************

      // 画像領域を確保
      cv::Mat polylines = cv::Mat::zeros(cvSize(512, 384), CV_8UC3);

      cv::Point point3[][2] =
      {
         { cv::Point(100, 100), cv::Point(200, 200) },
         { cv::Point(400, 300), cv::Point(200, 200) },
         { cv::Point(200, 350), cv::Point(200, 200) },
      };

      const cv::Point *pts3[] = { point3[0], point3[1], point3[2], point3[3] };

      int npts3[] = { 2, 2, 2 };

      cv::polylines(polylines,
         pts3,                    // 折れ線の配列.
         npts3,                   // 折れ線の頂点数の配列.
         _countof(npts3),         // 折れ線の数
         false,                   // 開いている線分
         cv::Scalar( 0, 0, 255),  // 色
         3,                       // 折れ線の太さ
         cv::LINE_8
         );

      cv::namedWindow("polylines", 1);
      cv::imshow("polylines", polylines);

      cv::waitKey(0);

      hr = 0;
   }

   catch (cv::Exception ex)
   {
      std::cout << ex.err << std::endl;
   }

   // ウィンドウの破棄
   cv::destroyAllWindows();

   return hr;
}


Prev Top Next
inserted by FC2 system