 |
Idenify polygons and other shapes using input imag
Source code name: "identify_polygon_cbinary.py"
Programming language: Python
Topic: Images/Shape identification
DMelt Version 1.6. Last modified: 07/30/1970. License: Pro
https://datamelt.org/code/cache/identify_polygon_cbinary_4225.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
# perform identification of shapes (polygons) using Canny Contour approach
from boofcv.alg.feature.detect.edge import CannyEdge,EdgeContour,EdgeSegment
from boofcv.alg.filter.binary import BinaryImageOps,Contour,ThresholdImageOps
from boofcv.alg.misc import ImageStatistics
from boofcv.alg.shapes import ShapeFittingOps
from boofcv.factory.feature.detect.edge import FactoryEdgeDetectors
from boofcv.gui import ListDisplayPanel
from boofcv.gui.feature import VisualizeShapes
from boofcv.gui.image import ShowImages
from boofcv.io import UtilIO
from boofcv.io.image import UtilImageIO,ConvertBufferedImage
from boofcv.struct import ConnectRule,PointIndex_I32
from boofcv.struct.image import GrayF32,GrayU8
from georegression.struct.point import Point2D_I32
from java.awt import *
from java.awt.image import BufferedImage
from java.util import List
from java.util import Random
# get image
from jhplot import *
http="http://datamelt.org/examples/data/"
print Web.get(http+"polygons.png")
image = UtilImageIO.loadImage("polygons.png")
input = ConvertBufferedImage.convertFromSingle(image, None, GrayF32)
splitFraction = 0.05
minimumSideFraction = 0.1
gui=ListDisplayPanel()
gui.addImage(image,"Original")
displayImage = BufferedImage(input.width,input.height,BufferedImage.TYPE_INT_RGB)
binary = GrayU8(input.width,input.height)
# Finds edges inside the image
canny = FactoryEdgeDetectors.canny(2, False, True, GrayF32, GrayF32)
canny.process(input,0.1,0.3,binary)
contours = BinaryImageOps.contour(binary, ConnectRule.EIGHT, None)
g2 = displayImage.createGraphics()
g2.setStroke(BasicStroke(2))
# used to select colors for each line
rand = Random(234)
for c in contours:
vertexes = ShapeFittingOps.fitPolygon(c.external,True, splitFraction, minimumSideFraction,100)
g2.setColor(Color(rand.nextInt()))
VisualizeShapes.drawPolygon(vertexes,True,g2)
gui.addImage(displayImage, "Canny Contour")
ShowImages.showWindow(gui, "Polygon from Contour", True)
You see the box below because you did not login.