mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
Add ArcLength, ContourArea and Minimal Area Rectangle example
This commit is contained in:
parent
6b2d038024
commit
9dcbd6d93c
4 changed files with 23 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -16,4 +16,4 @@ ext/opencv/test.txt
|
||||||
pkg/
|
pkg/
|
||||||
log.txt
|
log.txt
|
||||||
*.avi
|
*.avi
|
||||||
examples/rotated-boxes-with-detected-bounding-rectangles.jpg
|
examples/contours/rotated-boxes-with-detected-bounding-rectangles.jpg
|
||||||
|
|
|
@ -30,15 +30,35 @@ contour = canny.find_contours(:mode => OpenCV::CV_RETR_LIST, :method => OpenCV::
|
||||||
while contour
|
while contour
|
||||||
# No "holes" please (aka. internal contours)
|
# No "holes" please (aka. internal contours)
|
||||||
unless contour.hole?
|
unless contour.hole?
|
||||||
|
|
||||||
|
puts '-' * 80
|
||||||
|
puts "BOUNDING RECT FOUND"
|
||||||
|
puts '-' * 80
|
||||||
|
|
||||||
|
# You can detect the "bounding rectangle" which is always oriented horizontally and vertically
|
||||||
box = contour.bounding_rect
|
box = contour.bounding_rect
|
||||||
puts "found external contour from #{box.top_left.x},#{box.top_left.y} to #{box.bottom_right.x},#{box.bottom_right.y}"
|
puts "found external contour with bounding rectangle from #{box.top_left.x},#{box.top_left.y} to #{box.bottom_right.x},#{box.bottom_right.y}"
|
||||||
|
|
||||||
|
# The contour area can be computed:
|
||||||
|
puts "that contour encloses an area of #{contour.contour_area} square pixels"
|
||||||
|
|
||||||
|
# .. as can be the length of the contour
|
||||||
|
puts "that contour is #{contour.arc_length} pixels long "
|
||||||
|
|
||||||
# Draw that bounding rectangle
|
# Draw that bounding rectangle
|
||||||
cvmat.rectangle! box.top_left, box.bottom_right, :color => OpenCV::CvColor::Black
|
cvmat.rectangle! box.top_left, box.bottom_right, :color => OpenCV::CvColor::Black
|
||||||
|
|
||||||
|
# You can also detect the "minimal rectangle" which has an angle, width, height and center coordinates
|
||||||
|
# and is not neccessarily horizonally or vertically aligned.
|
||||||
|
# The corner of the rectangle with the lowest y and x position is the achor (see imagehere: http://bit.ly/lT1XvB)
|
||||||
|
# The zero angle position is always straight up.
|
||||||
|
# Positive angle values are clockwise and negative values counter clockwise (so -60 means 60 degree counter clockwise)
|
||||||
|
box = contour.min_area_rect
|
||||||
|
puts "found minimal rectangle with its center at (#{box.center.x.round},#{box.center.y.round}), width of #{box.size.width.round}px, height of #{box.size.height.round} and an angle of #{box.angle.round} degree"
|
||||||
end
|
end
|
||||||
contour = contour.h_next
|
contour = contour.h_next
|
||||||
end
|
end
|
||||||
|
|
||||||
# And save the image
|
# And save the image
|
||||||
puts "Saving image with bounding rectangles"
|
puts "\nSaving image with bounding rectangles"
|
||||||
cvmat.save_image("rotated-boxes-with-detected-bounding-rectangles.jpg")
|
cvmat.save_image("rotated-boxes-with-detected-bounding-rectangles.jpg")
|
BIN
examples/contours/rotated-boxes.jpg
Normal file
BIN
examples/contours/rotated-boxes.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.8 KiB |
Binary file not shown.
Before Width: | Height: | Size: 23 KiB |
Loading…
Add table
Add a link
Reference in a new issue