Extracts the connected groups of pixels from a bitmap using various options.
public class ExtractObjectsCommand : RasterCommand
Public Class ExtractObjectsCommand
Inherits RasterCommand
public:
ref class ExtractObjectsCommand : RasterCommand
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Core;
public void ExtractObjectsCommandExample()
{
using (RasterCodecs codecs = new RasterCodecs())
// Load the original image
using (RasterImage inputImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "demoicr2.tif")))
{
// Setup the extraction options
ExtractObjectsCommand command = new ExtractObjectsCommand()
{
DetectChildren = true,
EightConnectivity = true,
Outline = true
};
// Extract the objects
command.Run(inputImage);
using (ExObjData data = command.Data)
{
// Log the number of objects from the first list
ExObjObjectList objects = data[0].Objects;
Console.WriteLine($"Number of objects (before filtering): {objects.Count}");
// Log the number of points around the first object (braces for scope)
{
int count = 0;
foreach (ExObjOutlinePoint point in objects.First().Outline)
count++;
Console.WriteLine($"First object's outline length: {count}");
}
// Setup the filter options
ExObjFilterOptions filterOptions = new ExObjFilterOptions()
{
LargeObjectThreshold = -1, // No upper limit on size
SmallObjectThreshold = 10 // Remove objects smaller than 10x10 pixels
};
// Filter the objects
data.FilterList(objects, filterOptions);
// Log the number of objects again
Console.WriteLine($"Number of objects (after filtering): {objects.Count}");
// Setup the content bound options
ExObjContentBound contentBound = new ExObjContentBound(new LeadRect(192, 260, 323, 146));
ExObjContentBoundOptions contentBoundOptions = new ExObjContentBoundOptions()
{
ObjectsOfInterest = null // Pass null to use every object in data
};
// Calculate the content bounds
data.CalculateContentBound(new ExObjContentBound[] { contentBound }, contentBoundOptions);
// Setup the region options
ExObjRegionOptions regionOptions = new ExObjRegionOptions()
{
Horizontal = true
};
// Calculate each object's region
data.CalculateRegion(objects, regionOptions);
// Create an output image
using (RasterImage outputImage = RasterImage.Create(inputImage.Width, inputImage.Height, 24, inputImage.XResolution, RasterColor.White))
{
// Fill the output image with white
new FillCommand(RasterColor.White).Run(outputImage);
// Draw the content bound rects for the first word. Red for the input, green for the output.
outputImage.AddRectangleToRegion(null, contentBound.Input, RasterRegionCombineMode.Set);
new FillCommand(new RasterColor(255, 0, 0)).Run(outputImage);
outputImage.AddRectangleToRegion(null, contentBound.Content, RasterRegionCombineMode.Set);
new FillCommand(new RasterColor(0, 255, 0)).Run(outputImage);
// Populate the output image with each object's region
foreach (ExObjObject @object in objects)
foreach (ExObjSegment segment in @object.RegionHorizontal)
{
// Update the region to the current segment
outputImage.AddRectangleToRegion(null, segment.Bounds, RasterRegionCombineMode.Set);
// Fill the region with black
new FillCommand(RasterColor.Black).Run(outputImage);
}
// Clear the output image's region
outputImage.MakeRegionEmpty();
// Save the output image
codecs.Save(outputImage, Path.Combine(LEAD_VARS.ImagesDir, "ExtractObjects.png"), RasterImageFormat.Png, 0);
}
}
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images";
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Core
Public Sub ExtractObjectsCommandExample()
Using codecs As New RasterCodecs()
' Load the original image
Using inputImage As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "demoicr2.tif"))
' Setup the extraction options
Dim command As New ExtractObjectsCommand() With {
.DetectChildren = True,
.EightConnectivity = True,
.Outline = True
}
' Extract the objects
command.Run(inputImage)
Using data As ExObjData = command.Data
' Log the number of objects from the first list
Dim objects As ExObjObjectList = data(0).Objects
Console.WriteLine($"Number of objects (before filtering): {objects.Count}")
' Log the number of points around the first object
Dim count As Integer = 0
For Each point As ExObjOutlinePoint In objects.First().Outline
count += 1
Next
Console.WriteLine($"First object's outline length: {count}")
' Setup the filter options
Dim filterOptions As New ExObjFilterOptions() With {
.LargeObjectThreshold = -1, ' No upper limit on size
.SmallObjectThreshold = 10 ' Remove objects smaller than 10x10 pixels
}
' Filter the objects
data.FilterList(objects, filterOptions)
' Log the number of objects again
Console.WriteLine($"Number of objects (after filtering): {objects.Count}")
' Setup the content bound options
Dim contentBound As New ExObjContentBound(New LeadRect(192, 260, 323, 146))
Dim contentBoundOptions As New ExObjContentBoundOptions() With {
.ObjectsOfInterest = Nothing ' Pass Nothing to use every object in data
}
' Calculate the content bounds
data.CalculateContentBound(New ExObjContentBound() {contentBound}, contentBoundOptions)
' Setup the region options
Dim regionOptions As New ExObjRegionOptions() With {
.Horizontal = True
}
' Calculate each object's region
data.CalculateRegion(objects, regionOptions)
' Create an output image
Using outputImage As RasterImage = RasterImage.Create(inputImage.Width, inputImage.Height, 24, inputImage.XResolution, RasterColor.White)
' Fill the output image with white
Call New FillCommand(RasterColor.White).Run(outputImage)
' Draw the content bound rects for the first word. Red for the input, green for the output.
outputImage.AddRectangleToRegion(Nothing, contentBound.Input, RasterRegionCombineMode.[Set])
Call New FillCommand(New RasterColor(255, 0, 0)).Run(outputImage)
outputImage.AddRectangleToRegion(Nothing, contentBound.Content, RasterRegionCombineMode.[Set])
Call New FillCommand(New RasterColor(0, 255, 0)).Run(outputImage)
' Populate the output image with each object's region
For Each [object] As ExObjObject In objects
For Each segment As ExObjSegment In [object].RegionHorizontal
' Update the region to the current segment
outputImage.AddRectangleToRegion(Nothing, segment.Bounds, RasterRegionCombineMode.[Set])
' Fill the region with black
Call New FillCommand(RasterColor.Black).Run(outputImage)
Next
Next
' Clear the output image's region
outputImage.MakeRegionEmpty()
' Save the output image
codecs.Save(outputImage, Path.Combine(LEAD_VARS.ImagesDir, "ExtractObjects.png"), RasterImageFormat.Png, 0)
End Using
End Using
End Using
End Using
End Sub
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images"
End Class
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document