LoginSignup
15
15

More than 5 years have passed since last update.

opencv3で文字領域検出しようとしてハマった話「mac」

Last updated at Posted at 2017-01-08

opencvははじめはmakeしたりしたけどうまくいかないので、brewで入れた
拡張機能はcondaなどでも提供されてないのです。

ソースここ
https://github.com/miyamotok0105/opencv3_textdetection

結論から言うとチューニングが大変。もっと簡単にいくよとかありましたら意見お待ちしてます。
detect_2016-10-21 23.16.22.png

やりたかったことこいつ
文字領域検出用のERFilterの使い方

opencvで領域検出できるらしい。
opencvのバージョン3でさらに拡張機能なのですね。
まだちゃんとテストできてなかったりする機能はこっちに入ってるそうな。

公式
https://github.com/opencv/opencv_contrib

入れてみると

python textdetection.py scenetext_word01.jpg

textdetection.py
       A demo script of the Extremal Region Filter algorithm described in:
       Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012

Traceback (most recent call last):
  File "textdetection.py", line 28, in <module>
    channels = cv2.text.computeNMChannels(img)
AttributeError: module 'cv2.text' has no attribute 'computeNMChannels'

--with-contribオプションが拡張機能なわけなんだけども
C++
ムッチャ何回もbrew入れて消してどれなら動くんだと。
brew install opencv3 --c++11 --with-tbb --with-gstreamer --with-opengl --with-openni --with-contrib

で、こいつ。どうしたらいいのだ。。。
library not found for -lippicv

解決したページ
https://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/
モリックさん ありがとう。

ippicvを探す
find /usr/local -name "libippicv.a"
ここが見つかった場合
/usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a

リボリックリンクを貼る
ln -s /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a /usr/local/lib/

こんなのでちゃった場合

fatal error: 'opencv2/text.hpp' file not found

include "opencv2/text.hpp"

こんな感じでパスを通す。
~/.bashrc
export PKG_CONFIG_PATH=/usr/local/opt/opencv3/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/opt/opencv3/lib

ValueError: too many values to unpack

findContoursはバージョンによって戻り値が変わってるようで、
_, をはじめに追加が必要。

古いバージョン
contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

新しいバージョン
_, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

結果

太文字はなんとなくいける
detect_2016-10-21 22.55.02.png
detect_2016-10-21 23.02.23.png
detect_2016-10-21 23.16.22.png
detect_2016-10-21 23.25.06.png
detect_2016-10-21 23.25.12.png
detect_2016-10-21 23.27.55.png
detect_2016-10-21 23.30.10.png
detect_2016-10-21 23.31.52.png
detect_2016-10-21 23.32.46.png
detect_2016-10-21 23.33.09.png

590 590画像で。
部分的に認識されてる。
detect_590_1fc36f6427e8a492511dccee47b8607e.png
detect_590_3b6a5931c91204b45d5e7446b51d706c.jpg
detect_590_32ed93a61d856e376ae52b616d63f453.jpg
detect_590_b.jpg
detect_590_rirekisho_sp.jpg
detect_590_rirekisyo.jpg
detect_590_rirekisyosample.jpg
detect_590_ダウンロード.png
detect_590_法人設立届サンプル(市町村)画像.png
detect_590_法人設立届出書サンプル(国税)画像.png

256 256に変換してみた。
detect_256_1fc36f6427e8a492511dccee47b8607e.png
detect_256_3b6a5931c91204b45d5e7446b51d706c.jpg
detect_256_32ed93a61d856e376ae52b616d63f453.jpg
detect_256_b.jpg
detect_256_rirekisho_sp.jpg
detect_256_rirekisyo.jpg
detect_256_rirekisyosample.jpg
detect_256_法人設立届サンプル(市町村)画像.png
detect_256_法人設立届出書サンプル(国税)画像.png

python版はまだエラーになってる

わかる方いましたら連絡お待ちしています。

cvのバージョン確認方法

python

import cv2
cv2.version
'3.1.0'

ビルド時に使用する pkg-config のパス (PKG_CONFIG_PATH)
共有ライブラリのパス (LD_LIBRARY_PATH)

.bashrcに追記
export PKG_CONFIG_PATH=/usr/local/opt/opencv3/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/opt/opencv3/lib

pythonのパスを通す
http://qiita.com/massaru129/items/f065f79b1322a82edb1d

代替え案

OCR

r-cnn

End-to-End Text Recognition with Convolutional Neural Networks
https://crypto.stanford.edu/~dwu4/papers/ICPR2012.pdf

END-TO-END TEXT RECOGNITION WITH CONVOLUTIONAL NEURAL
NETWORKS

https://crypto.stanford.edu/~dwu4/papers/HonorThesis.pdf

opencv公式 erfilter
http://docs.opencv.org/3.0-beta/modules/text/doc/erfilter.html

void computeNMChannels(InputArray _src, OutputArrayOfArrays _channels, int _mode)

色情報とかハフ変換とか組み合わせて、最終的にSVMとかで学習させた方がよいのでは
こういう濃淡のヒストグラムを用いる手法でも結構いけるのでは
http://www.aso.ecei.tohoku.ac.jp/publication_data/509.pdf

opencv例
http://stackoverflow.com/questions/24385714/detect-text-region-in-image-using-opencv

おまけ

c++
http://www.bohyoh.com/Books/MeikaiCPP01/index.html

opencvサンプル
http://opencv.jp/cookbook/opencv_img.html

特徴量抽出
http://qiita.com/hmichu/items/f5f1c778a155c7c414fd

ocr
http://stackoverflow.com/questions/28591117/how-do-i-segment-a-document-using-tesseract-then-output-the-resulting-bounding-b
最近傍補間(ニアレストネイバー Nearest neighbor)等
http://imagingsolution.blog107.fc2.com/blog-entry-142.html

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15