mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
fix convexhull example
This commit is contained in:
parent
bfd4d81549
commit
873a848acf
1 changed files with 32 additions and 26 deletions
|
@ -1,41 +1,47 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# convexhull.rb
|
# convexhull.rb
|
||||||
|
# Draw contours and convexity defect points to captured image
|
||||||
require "rubygems"
|
require "rubygems"
|
||||||
require "opencv"
|
require "opencv"
|
||||||
require "pp"
|
|
||||||
include OpenCV
|
include OpenCV
|
||||||
|
|
||||||
window = GUI::Window.new("convexhull")
|
window = GUI::Window.new("convexhull")
|
||||||
pp CvCapture::INTERFACE
|
|
||||||
capture = CvCapture::open
|
capture = CvCapture::open
|
||||||
|
|
||||||
accuracy = 0.1
|
accuracy = 1
|
||||||
t = window.set_trackbar("accuracy", 100, 1){|v|
|
t = window.set_trackbar("accuracy", 10, 1) { |v|
|
||||||
accuracy = 0.1 * v
|
accuracy = v
|
||||||
}
|
}
|
||||||
|
|
||||||
while true
|
circle_options = { :color => CvColor::Blue, :line_type => :aa, :thickness => -1 }
|
||||||
key = GUI::wait_key(1)
|
|
||||||
image = capture.query
|
|
||||||
gray = image.BGR2GRAY
|
|
||||||
bin = gray.threshold_binary(0x44, 0xFF)
|
|
||||||
contours = bin.find_contours
|
|
||||||
while contours
|
|
||||||
image.poly_line! contours.approx(:accuracy => accuracy), :color => CvScalar::Red
|
|
||||||
contours.convexity_defects.each{|cd|
|
|
||||||
image.circle! cd.start, 1, :color => CvScalar::Blue
|
|
||||||
image.circle! cd.end, 1, :color => CvScalar::Blue
|
|
||||||
image.circle! cd.depth_point, 1, :color => CvScalar::Blue
|
|
||||||
}
|
|
||||||
|
|
||||||
|
loop do
|
||||||
|
image = capture.query
|
||||||
|
|
||||||
|
# Calculate contours from a binary image
|
||||||
|
gray = image.BGR2GRAY
|
||||||
|
bin = gray.threshold(0x44, 0xFF, :binary)
|
||||||
|
contours = bin.find_contours
|
||||||
|
|
||||||
|
while contours
|
||||||
|
# Draw contours
|
||||||
|
poly = contours.approx(:accuracy => accuracy)
|
||||||
|
begin
|
||||||
|
image.draw_contours!(poly, CvColor::Red, CvColor::Black, 2,
|
||||||
|
:thickness => 2, :line_type => :aa)
|
||||||
|
end while (poly = poly.h_next)
|
||||||
|
|
||||||
|
# Draw convexity defects
|
||||||
|
hull = contours.convex_hull2(true, false)
|
||||||
|
contours.convexity_defects(hull).each { |cd|
|
||||||
|
image.circle!(cd.start, 3, circle_options)
|
||||||
|
image.circle!(cd.depth_point, 3, circle_options)
|
||||||
|
image.circle!(cd.end, 3, circle_options)
|
||||||
|
}
|
||||||
contours = contours.h_next
|
contours = contours.h_next
|
||||||
end
|
end
|
||||||
#pts = gray.good_features_to_track(0.01, 10)
|
|
||||||
#puts pts.length
|
|
||||||
window.show image
|
window.show image
|
||||||
next unless key
|
exit if GUI::wait_key(1)
|
||||||
case key.chr
|
|
||||||
when "\e"
|
|
||||||
exit
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue