Identify complex polygons using cedge approach
Source code name: "identify_polygon_cedge.py"
Programming language: Python
Topic: Images/Shape identification
DMelt Version 1.7. Last modified: 07/30/1970. License: Pro
https://datamelt.org/code/cache/identify_polygon_cedge_1944.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.


# Identify polygons using contour edge  (edge) approach and BoofCV

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, True, True, GrayF32, GrayF32)
canny.process(input,0.1,0.3,binary)
contours = canny.getContours()

g2 = displayImage.createGraphics()
g2.setStroke(BasicStroke(3))

# used to select colors for each line
rand = Random(234)
for e in contours:
      g2.setColor(Color(rand.nextInt()))
      for s in e.segments:
                  vertexes = ShapeFittingOps.fitPolygon(s.points,False,
						splitFraction, minimumSideFraction,100)
                  VisualizeShapes.drawPolygon(vertexes, False, g2)

gui.addImage(displayImage, "Canny Trace")


ShowImages.showWindow(gui, "Polygon from Contour", True)


You see the box below because you did not login.