OpenCV 5.0.0
Open Source Computer Vision
読み込み中...
検索中...
見つかりません
🤖 AIによる機械翻訳(非公式) — これは OpenCV 5.0.0 公式リファレンス(英語)を AI (Claude) で自動翻訳したものです。訳に誤りを含む場合があります。正確な情報は 公式英語版(原文) を参照してください。
高速エッジ検出向けStructured forests

はじめに

このチュートリアルでは、画像中のエッジ検出を目的として、structured forests を使用する方法を学ぶ。

例

image
image
image
image
image
image
image
image
image
image
image
image
覚え書き
Canny エッジ検出器のような2値化手法は、両方のアルゴリズム(Sobel および StructuredEdgeDetection::detectEdges)が生成したエッジに適用できる。

ソースコード

1/**************************************************************************************
2The structured forests for fast edge detection demo requires you to provide a model.
3This model can be found at the opencv_extra repository on Github on the following link:
4https://github.com/opencv/opencv_extra/blob/master/testdata/cv/ximgproc/model.yml.gz
5***************************************************************************************/
6
8#include "opencv2/highgui.hpp"
9#include <iostream>
10
11using namespace cv;
12using namespace cv::ximgproc;
13
14const char* keys =
15{
16 "{i || input image file name}"
17 "{m || model file name}"
18 "{o || output image file name}"
19};
20
21int main( int argc, const char** argv )
22{
23 CommandLineParser parser(argc, argv, keys);
24 parser.about("This sample demonstrates usage of structured forests for fast edge detection");
25 parser.printMessage();
26
27 if ( !parser.check() )
28 {
29 parser.printErrors();
30 return -1;
31 }
32
33 String modelFilename = parser.get<String>("m");
34 String inFilename = parser.get<String>("i");
35 String outFilename = parser.get<String>("o");
36
38 Mat image = imread(inFilename, IMREAD_COLOR);
39 if ( image.empty() )
40 CV_Error(Error::StsError, String("Cannot read image file: ") + inFilename);
42
43 if ( modelFilename.size() == 0)
44 CV_Error(Error::StsError, String("Empty model name"));
45
47 image.convertTo(image, DataType<float>::type, 1/255.0);
49
50 TickMeter tm;
51 tm.start();
54 createStructuredEdgeDetection(modelFilename);
56
57 tm.stop();
58 std::cout << "createStructuredEdgeDetection() time : " << tm << std::endl;
59
60 tm.reset();
61 tm.start();
63 Mat edges;
64 pDollar->detectEdges(image, edges);
66 tm.stop();
67 std::cout << "detectEdges() time : " << tm << std::endl;
68
69 tm.reset();
70 tm.start();
72 // computes orientation from edge map
73 Mat orientation_map;
74 pDollar->computeOrientation(edges, orientation_map);
75
76 // suppress edges
77 Mat edge_nms;
78 pDollar->edgesNms(edges, orientation_map, edge_nms, 2, 0, 1, true);
80
81 tm.stop();
82 std::cout << "nms time : " << tm << std::endl;
83
85 if ( outFilename.size() == 0 )
86 {
87 imshow("edges", edges);
88 imshow("edges nms", edge_nms);
89 waitKey(0);
90 }
91 else
92 imwrite(outFilename, 255*edges);
94
95 return 0;
96}
Designed for command line parsing.
Definition utility.hpp:915
Template "trait" class for OpenCV primitive data types.
Definition traits.hpp:113
Comma-separated Matrix Initializer.
Definition mat.hpp:964
a Class to measure passing time.
Definition utility.hpp:351
void start()
starts counting ticks.
Definition utility.hpp:360
void stop()
stops counting ticks.
Definition utility.hpp:366
void reset()
resets internal values.
Definition utility.hpp:455
std::string String
Definition cvstd.hpp:151
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
#define CV_Error(code, msg)
Call the error handler.
Definition exception.hpp:174
bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition ximgproc.hpp:149
Definition core.hpp:107

解説

  1. ソースのカラー画像を読み込む

    Mat image = imread(inFilename, IMREAD_COLOR);
    if ( image.empty() )
    CV_Error(Error::StsError, String("Cannot read image file: ") + inFilename);
  2. ソース画像を float の [0;1] 範囲に変換する

    image.convertTo(image, DataType<float>::type, 1/255.0);
  3. メインアルゴリズムを実行する

    Mat edges;
    pDollar->detectEdges(image, edges);
    // computes orientation from edge map
    Mat orientation_map;
    pDollar->computeOrientation(edges, orientation_map);
    // suppress edges
    Mat edge_nms;
    pDollar->edgesNms(edges, orientation_map, edge_nms, 2, 0, 1, true);
  4. 結果を表示する

    if ( outFilename.size() == 0 )
    {
    imshow("edges", edges);
    imshow("edges nms", edge_nms);
    waitKey(0);
    }
    else
    imwrite(outFilename, 255*edges);

参考文献

詳細については、以下の論文を参照のこと : [74] [173]