Created
August 19, 2012 03:55
-
-
Save ser1zw/3391789 to your computer and use it in GitHub Desktop.
ruby-opencvのCvContour#approx_polyのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'opencv' | |
include OpenCV | |
# (1)画像を読み込んで2値化 | |
img = CvMat.load('lenna.png') # ※適当な画像を指定してください | |
gray = img.BGR2GRAY | |
bin = gray.threshold(100, 255, :binary) | |
# (2)近似対象の輪郭線を取得 | |
contours = bin.find_contours(:mode => CV_RETR_EXTERNAL) | |
# (3)全輪郭線をapprox_polyで近似 | |
# CvContour#approx_poly(approx_poly_option) | |
# 引数: | |
# approx_poly_option (Hash)…近似オプション | |
# :method - 近似手法。Douglas-Peuckerアルゴリズム(:dp)のみ。[cvApproxPolyの引数methodに対応] | |
# :accuracy - Douglas-Peuckerアルゴリズムの近似精度[cvApproxPolyの引数parameterに対応] | |
# :recursive - trueなら全部近似、falseなら1つのシーケンスのみ近似[cvApproxPolyの引数parameter2に対応] | |
# 戻り値: | |
# 近似された折れ線(CvContour) | |
poly = contours.approx_poly(:method => :dp, :accuracy => 2.0, :recursive => true) | |
# (4)描画して表示 | |
begin | |
img.draw_contours!(poly, CvColor::Blue, CvColor::Black, 2, | |
:thickness => 1, :line_type => :aa) | |
end while (poly = poly.h_next) | |
window = GUI::Window.new('approx_poly') | |
window.show img | |
GUI::wait_key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment