←Select platform

AddMagicWandToRegion Method

Summary
Sets a region based on the color found at point (x,y) in the image.
Syntax
C#
Objective-C
C++/CLI
Java
Python
- (BOOL)addMagicWandToRegion:(NSInteger)left  
                         top:(NSInteger)top  
         lowerToleranceColor:(LTRasterColor *)lowTolColor  
         upperToleranceColor:(LTRasterColor *)upTolColor  
                 combineMode:(LTRasterRegionCombineMode)combineMode  
                       error:(NSError **)error 
public void addMagicWandToRegion( 
  int left,  
  int top,  
  RasterColor lowerToleranceColor,  
  RasterColor upperToleranceColor,  
  RasterRegionCombineMode combineMode 
) 

Parameters

left
X coordinate of a point. The color of the specified point will be used to set the region.

top
Y coordinate of a point. The color of the specified point will be used to set the region.

lowerToleranceColor
Lower tolerance values that set the lower stopping point for the region.

upperToleranceColor
Upper tolerance values that set the upper stopping point for the region.

combineMode
The action to take regarding the existing image region, if one is defined.

Remarks

For color bitmaps:

If the value of the pixel at (x, y) is (125, 125, 125) and lowerToleranceColor is (20,30,15), then the lower stopping point is (105, 95, 110). If upperToleranceColor is (20,30,15), then the upper stopping point is (145,155,140).

For gray scale bitmaps:

The minimum channel tolerance value of  lowerToleranceColor will be used to set the lower stopping point, and the value of  upperToleranceColor will be used to set the upper stopping point. For example, if the value of the pixel at (x, y) is (125, 125, 125) and  lowerToleranceColor is (20,30,15), the smallest value of the triplet (15) will be used to create the lower stopping point of (110,110,110). If  upperToleranceColor is (10,25,20), the smallest value of that triplet (10) will be used to create the upper stopping point of (135,135,135).

This function supports 12 and 16-bit grayscale and 48 and 64-bit color images. Support for 12 and 16-bit grayscale and 48 and 64-bit color images is available in Document/Medical Imaging.

This method supports signed/unsigned data images.

To update an existing region, specify how the new region is to be combined with the existing one by using the  combineMode parameter. For more information, refer to RasterRegionCombineMode.

The AddMagicWandToRegion function can use the Extended Grayscale mask. For more information, refer to Grayscale Images

For more information, refer to Creating a Region.

For more information, refer to Saving A Region.

For more information, refer to Working with the Existing Region.

Example

This example will load an image, set a magic wand region to it, and fill the region with blue before saving the image back to disk

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Core; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Dicom; 
using Leadtools.Drawing; 
using Leadtools.Controls; 
using Leadtools.Svg; 
 
 
public void AddMagicWandToRegionExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_AddMagicWandToRegion.bmp"); 
 
   // Load the image 
   RasterImage image = codecs.Load(srcFileName); 
 
   // Posterize the image to decrease the number of colors 
   PosterizeCommand posterize = new PosterizeCommand(16); 
   posterize.Run(image); 
 
   // Specify a pixel in the upper left of the displayed image 
   LeadPoint pt = new LeadPoint(image.ImageWidth / 8, image.ImageHeight / 8); 
 
   // Adjust the point in case the view perspective is not TopLeft 
   pt = image.PointToImage(RasterViewPerspective.TopLeft, pt); 
 
   // Create a magic wand region at this point 
   RasterColor lowerColor = new RasterColor(20, 30, 150); 
   RasterColor upperColor = new RasterColor(15, 30, 10); 
   image.AddMagicWandToRegion(pt.X, pt.Y, lowerColor, upperColor, RasterRegionCombineMode.Set); 
 
   // Fill the region with blue 
   FillCommand fill = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Blue)); 
   fill.Run(image); 
 
   // Save the image 
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24); 
 
   image.Dispose(); 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 
 
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.core.*; 
import leadtools.svg.*; 
import leadtools.imageprocessing.CloneCommand; 
import leadtools.imageprocessing.FillCommand; 
import leadtools.imageprocessing.FlipCommand; 
import leadtools.imageprocessing.GrayscaleCommand; 
import leadtools.imageprocessing.color.InvertCommand; 
import leadtools.imageprocessing.color.PosterizeCommand; 
 
 
public void addMagicWandToRegionExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1.cmp"); 
   String destFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1_AddMagicWandToRegion.bmp"); 
 
   // Load the image 
   RasterImage image = codecs.load(srcFileName); 
 
   // Posterize the image to decrease the number of colors 
   PosterizeCommand posterize = new PosterizeCommand(16); 
   posterize.run(image); 
   assertTrue(posterize.getLevels() == 16); 
 
   // Specify a pixel in the upper left of the displayed image 
   LeadPoint pt = new LeadPoint(image.getImageWidth() / 8, image.getImageHeight() / 8); 
 
   // Adjust the point in case the view perspective is not TopLeft 
   pt = image.pointToImage(RasterViewPerspective.TOP_LEFT, pt); 
 
   // Create a magic wand region at this point 
   RasterColor lowerColor = new RasterColor(20, 30, 150); 
   RasterColor upperColor = new RasterColor(15, 30, 10); 
   image.addMagicWandToRegion(pt.getX(), pt.getY(), lowerColor, upperColor, RasterRegionCombineMode.SET); 
 
   // Fill the region with blue 
   FillCommand fill = new FillCommand(RasterColor.fromKnownColor(RasterKnownColor.BLUE)); 
   fill.run(image); 
 
   // Save the image 
   codecs.save(image, destFileName, RasterImageFormat.BMP, 24); 
 
   // Clean up 
   image.dispose(); 
   codecs.dispose(); 
 
   assertTrue("file unsuccessfully saved to " + destFileName, (new File(destFileName)).exists()); 
   System.out.printf("File saved successfully to %s%n", destFileName); 
} 
Requirements

Target Platforms

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

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