Segment v1.0 tutorial #1: extracting regions from images
Automatic image segmentation made easy, using the Segment v1.0 component.
Click here to download the sample project file
You may download a 2-day trial version of the Segment v1.0 component here, to test this project.
The Segment v1.0 component allows you to automatically detect and regions in images, allowing you to track, highlight, or extract any particular region for further manipulation.
For this tutorial, we will find regions, highlight them, draw rectangles over the detected shapes, then we will extract and save each individual region as a separate image. Let's start with detecting regions in images:
Dim seg As New foxtrot.xray.Segment()
Dim regions() As foxtrot.xray.Region = seg.SegmentBitmap(bmp, soften, depth)
This code, detects all regions in the image contained by the bmp variable, using a filtering strength soften and a neighbor search range depth. Different images will require different depth and soften values, depending on noise, texture, and color similarity of regions. Experimenting with these values will allow you to find the most accurate results for your particular content.
Using the array of regions that we received from the detection process, we will now highlight our region edges:
For Each region As foxtrot.xray.Region In regions
region.MarkEdges(bmp, System.Drawing.Color.Red)
Next
If we wanted to highlight the whole region, we could have used the MarkRegion method instead. Now, let's suppose we wanted to draw the bounding rectangle for our particular region. We can do this with the following code:
Dim gfx As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
For Each region As foxtrot.xray.Region In regions
gfx.DrawRectangle(System.Drawing.Pens.YellowGreen, region.Area)
Next
The Area property of the region, gives us the bounding rectangle for our region. We also have a Center property that contains the coordinates for the center of the extracted region. Extra credit: write code to draw crosshairs on the center of the detected region!
Lets extract these regions into separate images, now:
For Each region As foxtrot.xray.Region In regions
Dim extracted As System.Drawing.Bitmap = region.Extract(bmp, _
includeBorder, cropImage)
extracted.Save(filename, System.Drawing.Imaging.ImageFormat.Png)
Next
We have now iterated over each region, extracted the pixels, and saved each to a separate PNG file (PNG is used to preserve transparency). The includeBorder parameter is a Boolean that specifies whether to include the edges of the region in the extracted image, and the cropImage is a Boolean that specifies whether to crop the image when extracting. If we decide to crop the image, the result will be an image only large enough to contain the selected region, if not, the image will be the same size as the original.
The Extract method returns a 32bpp ARGB image, in which the pixels that are not part of the region are set to a transparent color.




