←Select platform

Color Property

Summary

The color for extraction.

Syntax
C#
Objective-C
C++/CLI
Java
Python
public RasterColor Color {get; set;} 
@property (nonatomic, strong) LTRasterColor * color; 
public RasterColor getColor(); 
public void setColor( 
   RasterColor rasterColor 
); 
public:  
   property RasterColor^ Color 
   { 
      RasterColor^ get() 
      void set(RasterColor^ value) 
   } 
Color # get and set (ExObjColorInfo) 

Property Value

The color for object extraction. The default value is RasterColor.Black.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Core; 
 
public void ExtractObjectsCommandUseMultiColorsExample() 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   // Load the original image 
   using (RasterImage inputImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "unwarp1.jpg"))) 
   { 
      // Setup the extraction options 
      Tuple<string, RasterColor>[] colors = new Tuple<string, RasterColor>[] 
      { 
         Tuple.Create("DarkGray", new RasterColor(30, 30, 30)), 
         Tuple.Create("DarkGreen", new RasterColor(41, 108, 70)), 
         Tuple.Create("LightRed", new RasterColor(200, 68, 65)) 
      }; 
      ExtractObjectsCommand command = new ExtractObjectsCommand() 
      { 
         ColorInfo = colors 
            .Select(c => new ExObjColorInfo() 
            { 
               Color = c.Item2, 
               Threshold = 50 
            }) 
            .ToArray(), 
         DetectChildren = true, 
         EightConnectivity = true, 
         IgnoreSmallNoise = true, 
         Outline = true, 
         SmallNoiseThreshold = 5, // Filter out noise smaller than 5x5 pixels 
         UseMultiColors = true 
      }; 
 
      // Extract the objects 
      command.Run(inputImage); 
 
      using (ExObjData data = command.Data) 
      { 
         // Put objects into one list for processing all at once 
         List<ExObjObject> objects = new List<ExObjObject>(); 
         foreach (ExObjResult result in data) 
            objects.AddRange(result.Objects); 
 
         // 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)) 
         { 
            // Extract each color to a separate image 
            int colorIndex = -1; 
 
            foreach (ExObjResult result in data) 
            { 
               colorIndex++; 
 
               // Fill the output image with white 
               new FillCommand(RasterColor.White).Run(outputImage); 
 
               // Populate the output image with each object's region 
               foreach (ExObjObject @object in result.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 the current color 
                     new FillCommand(colors[colorIndex].Item2).Run(outputImage); 
                  } 
 
               // Clear the output image's region 
               outputImage.MakeRegionEmpty(); 
 
               // Save the output image 
               codecs.Save(outputImage, Path.Combine(LEAD_VARS.ImagesDir, $"ExtractObjectsMultiColors_{colors[colorIndex].Item1}.png"), RasterImageFormat.Png, 0); 
            } 
         } 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Iterator; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.FillCommand; 
import leadtools.imageprocessing.core.*; 
import leadtools.internal.Tuple; 
 
 
public void extractObjectsCommandUseMultiColorsExample() { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
 
   RasterCodecs codecs = new RasterCodecs(); 
   // Load the original image 
   RasterImage inputImage = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "demoicr2.tif")); 
 
   File output = new File(combine(LEAD_VARS_IMAGES_DIR, "demoicr2.tif")); 
   assertTrue(output.exists()); 
 
   // Setup the extraction options 
   Tuple<String, RasterColor>[] colors = new Tuple[3]; 
   colors[0] = Tuple.create("DarkGray", new RasterColor(30, 30, 30)); 
   colors[1] = Tuple.create("DarkGreen", new RasterColor(41, 108, 70)); 
   colors[2] = Tuple.create("LightRed", new RasterColor(200, 68, 65)); 
 
   ExtractObjectsCommand command = new ExtractObjectsCommand(); 
   command.setDetectChildren(true); 
   command.setEightConnectivity(true); 
   command.setIgnoreSmallNoise(true); 
   command.setOutline(true); 
   command.setSmallNoiseThreshold(5);// Filter out noise smaller than 5x5 pixels 
   command.setUseMultiColors(true); 
   ExObjColorInfo[] exColors = new ExObjColorInfo[colors.length]; 
   for (int i = 0; i < colors.length; i++) { 
      exColors[i] = new ExObjColorInfo(); 
      exColors[i].setColor(colors[i].getItem2()); 
      exColors[i].setThreshold(50); 
   } 
 
   // Extract the objects 
   command.run(inputImage); 
 
   ExObjData data = command.getData(); 
 
   // Put objects into one list for processing all at once 
   ArrayList<ExObjObject> objects = new ArrayList<ExObjObject>(); 
   Iterator<ExObjResult> it = data.iterator(); 
   while (it.hasNext()) { 
      objects.addAll(it.next().getObjects()); 
   } 
 
   // Setup the region options 
   ExObjRegionOptions regionOptions = new ExObjRegionOptions(); 
   regionOptions.setHorizontal(true); 
 
   // Calculate each object's region 
   data.calculateRegion(objects, regionOptions); 
 
   // Create an output image 
   RasterImage outputImage = RasterImage.create(inputImage.getWidth(), inputImage.getHeight(), 24, 
         inputImage.getXResolution(), RasterColor.WHITE); 
 
   // Extract each color to a separate image 
   int colorIndex = -1; 
 
   for (ExObjResult result : data) { 
      colorIndex++; 
 
      // Fill the output image with white 
      new FillCommand(RasterColor.WHITE).run(outputImage); 
 
      // Populate the output image with each object's region 
      for (ExObjObject ob : result.getObjects()) { 
         for (ExObjSegment segment : ob.getRegionHorizontal()) { 
            // Update the region to the current segment 
            outputImage.addRectangleToRegion(null, segment.getBounds(), RasterRegionCombineMode.SET); 
 
            // Fill the region with the current color 
            new FillCommand(colors[colorIndex].getItem2()).run(outputImage); 
         } 
      } 
 
      // Clear the output image's region 
      outputImage.makeRegionEmpty(); 
 
      // Save the output image 
      codecs.save(outputImage, 
            combine(LEAD_VARS_IMAGES_DIR, "ExtractObjectsMultiColors_" + colors[colorIndex].getItem1() + ".png"), 
            RasterImageFormat.PNG, 0); 
   } 
 
   System.out.println("Command run and image saved to: " + combine(LEAD_VARS_IMAGES_DIR, "ExtractObjects.png")); 
   assertTrue(new File(combine(LEAD_VARS_IMAGES_DIR, "ExtractObjects.png")).exists()); 
 
   outputImage.dispose(); 
   data.dispose(); 
   inputImage.dispose(); 
   codecs.dispose(); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.3.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.ImageProcessing.Core Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.