OpenCV 3.0に果敢に挑戦し続けてみる

個人的な趣味の一環として、OpenCV 3.0がらみの情報をまとめてみる。「引用はいぃんよー、無断転載はあかんよー」ということで。

一つだけ言える真理がある。「男は黒に染まれ」(OpenCV 3.0.0-dev decolorを使って見る)

実質、グレイスケールなんで灰色とか白とかあるんですけどね。

 

さて、OpenCVでグレイスケール化としては、cv::cvtColor(dst,src,BGR2GRAY); とかが主流だと思うのですが、今回photo moduleの下にdecolorという関数が追加されたのでそれを試して見ました。代表的な変換手段と比較しつつ。

 

http://docs.opencv.org/trunk/modules/photo/doc/decolor.html

にはアルゴリズムの詳細は記載なしか(白目)

 

元画像( http://upload.wikimedia.org/wikipedia/commons/6/66/SMPTE_Color_Bars.svg をリサイズして利用 )

f:id:anobiidae:20140302212957p:plain

 

decolor

f:id:anobiidae:20140302213246j:plain

 

平均値 Y ← (R + G + B ) / 3

f:id:anobiidae:20140302213304j:plain

 

cvtColor(COLOR_BGR2GRAY) 

Y ← 0.299 * R + 0.587 * G + 0.114 * B(NTSC)

f:id:anobiidae:20140302213313j:plain

 

 

cvtColor(COLOR_BGR2Lab) → L成分のみ抽出

f:id:anobiidae:20140302213359j:plain

 

 ソースコードは以下

続きを読む

OpenCV 3.0.0からmodule/oclがなくなりますよ(釣り記事)

OpenCV 3.0.0-devを弄って遊んでいるけど、そういえばmodule/oclなくなっちゃってますよ、っと。

 

もともとはこんな感じであったんですけどね。

http://docs.opencv.org/2.4.8/modules/ocl/doc/introduction.html

 

その代わりに、各モジュールの下におかれるようになった。多分、OpenCLが使える環境だったら自動的に切り替わるから気にする必要は無い、っていうことなのかなあ・・・?どんな環境で使えるのかの記載が無い、というのが一番困ったもんだーと思いつつ。

 

 

OpenCV 2.4.8の場合は(殆ど)oclの下に

find . -name "*.cl" | sort
./modules/nonfree/src/opencl/surf.cl
./modules/ocl/src/opencl/arithm_LUT.cl
./modules/ocl/src/opencl/arithm_absdiff_nonsaturate.cl
./modules/ocl/src/opencl/arithm_add.cl
./modules/ocl/src/opencl/arithm_addWeighted.cl
./modules/ocl/src/opencl/arithm_add_mask.cl
./modules/ocl/src/opencl/arithm_add_scalar.cl
./modules/ocl/src/opencl/arithm_add_scalar_mask.cl
:
:

OpenCV 3.0.0-devの場合はそれぞれのモジュールの下に

find . -name "*.cl" | sort
./modules/core/src/opencl/arithm.cl
./modules/core/src/opencl/convert.cl
./modules/core/src/opencl/copymakeborder.cl
./modules/core/src/opencl/copyset.cl
./modules/core/src/opencl/flip.cl
./modules/core/src/opencl/inrange.cl
./modules/core/src/opencl/lut.cl
./modules/core/src/opencl/mixchannels.cl
./modules/core/src/opencl/mulspectrums.cl
./modules/core/src/opencl/reduce.cl
:
:
:

 

というか、なんか一部まだoclMatの記載残ってるけど大丈夫なの?

 

grep oclMat . -r
./modules/nonfree/perf/perf_surf.ocl.cpp:    oclMat d_keypoints;
./modules/nonfree/perf/perf_surf.ocl.cpp:    oclMat d_descriptors;
./modules/nonfree/perf/perf_surf.ocl.cpp:        oclMat d_src(img);
./modules/nonfree/perf/perf_surf.ocl.cpp:        d_surf(d_src, oclMat(), d_keypoints, d_descriptors);
./modules/nonfree/perf/perf_surf.ocl.cpp:    oclMat d_keypoints;
./modules/nonfree/perf/perf_surf.ocl.cpp:    oclMat d_descriptors;
./modules/nonfree/perf/perf_surf.ocl.cpp:    oclMat d_src(img);
./modules/nonfree/perf/perf_surf.ocl.cpp:    TEST_CYCLE() d_surf(d_src, oclMat(), d_keypoints, d_descriptors);
./modules/nonfree/test/test_surf.ocl.cpp:    surf(cv::ocl::oclMat(image), cv::ocl::oclMat(), keypoints);
./modules/nonfree/test/test_surf.ocl.cpp:    cv::ocl::oclMat descriptors;
./modules/nonfree/test/test_surf.ocl.cpp:    surf(cv::ocl::oclMat(image), cv::ocl::oclMat(), keypoints, descriptors, true);
./buildTBB/doc/fake-root/samples/cpp/points_classifier.cpp:    cv::ocl::oclMat testSample_ocl, reslut_ocl;
./samples/cpp/points_classifier.cpp:    cv::ocl::oclMat testSample_ocl, reslut_ocl;

アニメじゃない(アニメじゃない)本当のこーとさー(OpenCV 3.0.0-dev Non-Photorealistic Renderingを使ってみる)

そもそも、Non-Photorealistic Renderingって何?ということならば、ここを見ると解るはず。

http://yokoya.naist.jp/paper/datas/1288/jmthesis.pdf

一言で言えば、「芸術的な表現をもつ画像生成」という話らしい。

 

よし、つかってみよう。

おおよそパラメータはSとRの2個があるけど、説明が無い…。

横がS={0,40,80,120,160,200} 、縦がR={0.0, 0.2,0.4,0.6,0.8,1.0}だよ!

 

edgePreservingFilter(NORMCONV_FILTER)

f:id:anobiidae:20140302102629j:plain

edgePreservingFilter(RECURS_FILTER)

f:id:anobiidae:20140302102803j:plain

detailEnhance

f:id:anobiidae:20140302102858j:plain

stylization

f:id:anobiidae:20140302102955j:plain

pencilSketch(shade_factor=0.02f)

f:id:anobiidae:20140302103033j:plain

f:id:anobiidae:20140302103125j:plain

 

ソースコードはこちらと。

続きを読む

ばらばら殺ぺんぎん事件(OpenCVでStitchingしてみるか)

はい、物騒なタイトルついてますが、stitchingになります。

ソースは下記のペンギンさんの画像の赤枠部分(256×256が20枚)です。

f:id:anobiidae:20140301132231p:plain

画像縮小しちゃったけど、こんな感じの20枚

f:id:anobiidae:20140301175701j:plain

 

結果は、こちら

f:id:anobiidae:20140301132334j:plain

 

ふむ、とてもよい感じ…パフォーマンスを除けば。

…と思ったら、あれ?右下とか復元できてないような?

 

ソースはこの下。

続きを読む

初音ミクさんをOpenCV上に召喚(仮)する(OpenCV 3.0.0-dev vizを使ってみる)

OpenCV 3.0.0からサポートしているvizモジュールを使ってみたよ!使ってみたけど、OpenCVとの関係性が全くないよ!釣記事だね!(嘘)

1.vizモジュールをinstallする

apt-get install libvtk5.8* でとにかく何でもかんでも入れて、cmakeしなおす。 

-   OpenCV modules:
--     To be built:                 core flann imgproc highgui features2d calib3d cudaarithm ml nonfree objdetect video contrib cudawarping cuda cudafilters cudaimgproc legacy cudabgsegm cudacodec cudafeatures2d cudaoptflow cudastereo optim photo shape softcascade stitching superres ts videostab viz
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 androidcamera cudalegacy cudev java matlab python
--
--   GUI:
--     QT:                          NO
--     GTK+ 2.x:                    YES (ver 2.24.20)
--     GThread :                    YES (ver 2.38.1)
--     GtkGlExt:                    NO
--     OpenGL support:              NO
--     VTK support:                 YES (ver 5.8.0)

なって、VTKが有効になる。

 

2.ミクさんを用意する

今回は、なんとなーく(点数が少ない方が楽なので)こちらのミクさんを。

http://tawaship.blog134.fc2.com/blog-entry-69.html#more

 

3.ミクさんをバラバラにする

バイナリフォーマットは、このあたりを参考にして、C言語(≠C++)で分解。

http://www.neo-tech-lab.co.uk/MikuMikuDance/PMD1.htm#PMD%E5%BD%A2%E5%BC%8F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E3%83%87%E3%83%BC%E3%82%BF%E6%A7%8B%E9%80%A0

音がなるので要注意っと。

 

4. VTKにぶちこむ

点情報をこうやってぶちこんだり

                points->InsertNextPoint(v.vertex[i].pos.x,

                                        v.vertex[i].pos.y,

                                        v.vertex[i].pos.z);

 

面情報をこうやってぶちこんだり

                vtkSmartPointer < vtkTriangle > triangle = vtkSmartPointer < vtkTriangle >::New();

                triangle->GetPointIds()->SetId(0, f.face_vert_index[i]);

                triangle->GetPointIds()->SetId(1,f.face_vert_index[i + 1]);

                triangle->GetPointIds()->SetId(2,f.face_vert_index[i + 2]);

                cells->InsertNextCell(triangle);

 

多分、材質情報を読み込んだり、色を変えたりするともっとそれらしくなるだろうけど、とりあえず”3D表示する"までできたので、満足してみる。

 

f:id:anobiidae:20140301091256p:plain

 

追記: Lat式ミクVer 2.31さん ( http://bowlroll.net/up/dl30199 ) もこの通り

f:id:anobiidae:20140301155320p:plain

 

続きを読む

OpenCVでTBBを有効にする(Linuxだけどね、ついでにOpenMPも)

OpenCVでTBBを有効にする(Linuxだけどね、ついでにOpenMPも)

OpenCV on linux でTBBによる並列化を有効にするための条件は、以下の2点。

  1. libtbb-dev をインストールする
  2. cmake 実行時に -D WITH_TBB = ONとする or cmake-gui でWITH_TBBをONにする

$ mkdir build 

$ cd build

$ sudo apt-get install libtbb-dev

$ sudo ldconfig 

$ cmake -D WITH_TBB=ON ../

$ make -j 8  

※BUILD_TBB=ONにするとダウンロードとかもしてくれるっぽい。

 

$ ldd lib/libopencv_core.so

linux-gate.so.1 =>  (0xb772b000)

libz.so.1 => /lib/i386-linux-gnu/libz.so.1 (0xb74c7000)

libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xb74ac000)

librt.so.1 => /lib/i386-linux-gnu/librt.so.1 (0xb74a2000)

libtbb.so.2 => /usr/lib/libtbb.so.2 (0xb7474000)

libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xb738b000)

libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb7348000)

libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb732b000)

libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7176000)

/lib/ld-linux.so.2 (0xb772c000) 

こんな感じで、TBBへの依存関係ができてれば多分オッケー。

 

TBBが使えない環境の場合には、OpenMPという選択肢もあり。

$ mkdir build 

$ cd build

$ cmake -D WITH_OPENMP=ON ../

$ make -j 8  

 

Scene Text Detectionしてみやう(OpenCV 3.0)

シーン内の文字列検出に興味あるよね?

http://docs.opencv.org/2.4.8/modules/objdetect/doc/objdetect.html

にはなくて、

http://docs.opencv.org/trunk/modules/objdetect/doc/objdetect.html

にある新機能として、「シーンテキスト検出」がありますと。

 

ざっくり翻訳

シーンテキスト検出アルゴリズムは、Lukás Neumann & Jiri Matas [Neumann12]によって最初に提案されました。Class-specific Extremal Regionsの基本的な考え方は、適切なExtremal Regions (ERs)を画像の全コンポーネント木から選択する点でMSERに似ています。しかしながら、この技術は、適切なERsを選択する際に文字検出の分類学習の連続性によって点でMSERと異なります。すなわち、MSERで求められていた安定性を犠牲にし、(必ずしも安定しない)領域のクラス分類を選択します。

 

イメージの構成要素の木は、0~255まで着実な増加する値によって二値化し、次に、それらの包含関係によって階層中の連続のレベルからの得られた連結成分をリンクすることにより構築されます:

 

コンポーネント木は前述したような単純な画像であっても多数の領域が含まれているだろう。この数は、1M pixelの画像でも1 × 10^6 領域に簡単に達する。効率的に適切な領域を検出するために、全てのERsに対して順次実行される2つの異なるステージの分類が用いられる。

 

In the first stage incrementally computable descriptors (area, perimeter, bounding box, and euler number) are computed (in O(1)) for each region r and used as features for a classifier which estimates the class-conditional probability p(r|character). Only the ERs which correspond to local maximum of the probability p(r|character) are selected (if their probability is above a global limit p_min and the difference between local maximum and local minimum is greater than a delta_min value).

In the second stage, the ERs that passed the first stage are classified into character and non-character classes using more informative but also more computationally expensive features. (Hole area ratio, convex hull ratio, and the number of outer boundary inflexion points).

This ER filtering process is done in different single-channel projections of the input image in order to increase the character localization recall.

After the ER filtering is done on each input channel, character candidates must be grouped in high-level text blocks (i.e. words, text lines, paragraphs, ...). The grouping algorithm used in this implementation has been proposed by Lluis Gomez and Dimosthenis Karatzas in [Gomez13] and basically consist in finding meaningful groups of regions using a perceptual organization based clustering analisys (see erGrouping()).

 

試すには

https://raw.github.com/Itseez/opencv/master/samples/cpp/textdetection.cpp

ここのコードと、

samples/cpp/trained_classifierNM1.xml

samples/cpp/trained_classifierNM2.xml

があればOK。

 

http://en.wikipedia.org/wiki/File:NYTimes-Page1-11-11-1918.jpg 

から、これが得られます。

f:id:anobiidae:20140223205903p:plain