 |
Identify ellipses in an image using BoofCV
Source code name: "identify_ellipse.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_ellipse_6316.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.
# identification of shapes in an image. This example shows how to recognize ellipses.
# DMelt. S.Chekanov
from boofcv.alg.filter.binary import Contour,ThresholdImageOps,BinaryImageOps
from boofcv.alg.misc import ImageStatistics
from boofcv.alg.shapes import FitData,ShapeFittingOps
from boofcv.gui.feature import VisualizeShapes
from boofcv.gui.image import ShowImages
from boofcv.io import UtilIO
from boofcv.io.image import ConvertBufferedImage,UtilImageIO
from boofcv.struct import ConnectRule
from boofcv.struct.image import GrayF32,GrayU8
from georegression.struct.shapes import EllipseRotated_F64
from java.awt import *
from java.awt.image import BufferedImage
from java.util import *
# get image
from jhplot import *
http="http://datamelt.org/examples/data/"
print Web.get(http+"cells.png")
# load and convert the image into a usable format
image = UtilImageIO.loadImage("cells.png")
input = ConvertBufferedImage.convertFromSingle(image, None, GrayF32)
binary = GrayU8(input.width,input.height)
# the mean pixel value is often a reasonable threshold when creating a binary image
mean = ImageStatistics.mean(input)
# create a binary image by thresholding
ThresholdImageOps.threshold(input, binary, float(mean), True)
# reduce noise with some filtering
filtered = BinaryImageOps.erode8(binary, 1, None)
filtered = BinaryImageOps.dilate8(filtered, 1, None)
# Find the contour around the shapes
contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT,None)
# Fit an ellipse to each external contour and draw the results
g2=image.createGraphics()
g2.setStroke(BasicStroke(3))
g2.setColor(Color.GREEN)
for c in contours:
ellipse = ShapeFittingOps.fitEllipse_I32(c.external,0,False,None)
VisualizeShapes.drawEllipse(ellipse.shape, g2)
ShowImages.showWindow(image,"Identified Ellipses",True)
You see the box below because you did not login.