PPTX DOCX XLSX PDF ODP
Aspose.Imaging  for Java
EMF

Remove background from EMF via Java

Build your own Java apps to Remove background from EMF files using server-side APIs.

How to remove background in EMF Files Using Java

Removing the background from an image involves isolating foreground objects, a task that requires object recognizing. Multiple approaches exist to identify objects within a photo in EMF format. For simple images featuring a uniform color background, an automatic method suffices. However, for photos with multiple or partially merged figures, pre-marking objects becomes necessary. This involves specifying rectangular regions and object types for removal. In intricate cases, the Cloud API enables automatic object detection. The Cloud API provides a cloud application capable of recognizing objects within photos, leveraging the resulting contours for background removal. Post-removal, edges left behind by figures can be smoothed to enhance image quality. In order to remove background in EMF files, we’ll use Aspose.Imaging for Java API which is a feature-rich, powerful and easy to use image manipulation and conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging</artifactId>
<version>version of aspose-imaging API</version>
<classifier>jdk16</classifier>
</dependency>

Steps to Remove background from EMF via Java

You need the aspose-imaging-version-jdk16.jar to try the following workflow in your own environment.

  • Load EMF files with Image.load method
  • Remove background;
  • Save image to disc in the supported by Aspose.Imaging format

System Requirements

Aspose.Imaging for Java is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • JDK 1.6 or higher is installed.
 

Remove background in EMF images - Java

import com.aspose.imaging.*;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.imageoptions.PngOptions;
import com.aspose.imaging.masking.IMaskingSession;
import com.aspose.imaging.masking.ImageMasking;
import com.aspose.imaging.masking.options.*;
import com.aspose.imaging.masking.result.MaskingResult;
import com.aspose.imaging.sources.FileCreateSource;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
//Most common example to demonstrate background change/remove tool
removeBackgroundGenericExample();
// Folder that contains images to process
static final String templatesFolder = "c:\\Data\\";
public static void removeBackgroundProcessingWithManualRectangles()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>()
{
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder + "remove_background_manual_rectangles." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
maskingOptions.setFeatheringRadius(2);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
AutoMaskingArgs maskingArgs = new AutoMaskingArgs();
maskingArgs.setObjectsRectangles(new Rectangle[]
{
// girl's bound box
new Rectangle(87, 47, 123, 308),
// boy's bound box
new Rectangle(180, 24, 126, 224)
});
maskingOptions.setArgs(maskingArgs);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
IMaskingSession maskingSession = new ImageMasking(image)
.createSession(maskingOptions);
try
{
// first run of segmentation
maskingSession.decompose().dispose();
AutoMaskingArgs argsWithUserMarkers = new AutoMaskingArgs();
argsWithUserMarkers.setObjectsPoints(new Point[][]
{
// background markers
null,
// foreground markers
new UserMarker()
// boy's head
.addPoint(218, 48, 10)
// girl's head
.addPoint(399, 66, 10)
// girs's body
.addPoint(158, 141, 10)
.addPoint(158, 209, 20)
.addPoint(115, 225, 5)
.getPoints()
});
try (MaskingResult maskingResult = maskingSession
.improveDecomposition(argsWithUserMarkers))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
finally
{
maskingSession.dispose();
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
public static void removeBackgroundAutoProcessingWithAssumedObjects()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>() {
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder
+ "remove_background_auto_assumed_objects." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
final LinkedList<AssumedObjectData> assumedObjects = new LinkedList<>();
// girl's bound box
assumedObjects.add(
new AssumedObjectData(DetectedObjectType.Human,
new Rectangle(87, 47, 123, 308)));
// boy's bound box
assumedObjects.add(
new AssumedObjectData(DetectedObjectType.Human,
new Rectangle(180, 24, 126, 224)));
maskingOptions.setAssumedObjects(assumedObjects);
maskingOptions.setCalculateDefaultStrokes(true);
maskingOptions.setFeatheringRadius(1);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
maskingOptions.setBackgroundReplacementColor(Color.getGreen());
try (MaskingResult maskingResult = new ImageMasking(image)
.decompose(maskingOptions))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
public static void removeBackgroundAutoProcessing()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>() {
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder + "remove_background_auto." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
maskingOptions.setFeatheringRadius(1);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
maskingOptions.setBackgroundReplacementColor(Color.getGreen());
try (MaskingResult maskingResult = new ImageMasking(image)
.decompose(maskingOptions))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
public static void removeBackgroundGenericExample()
{
List<String> rasterFormats = Arrays.asList("jpg", "png", "bmp", "apng",
"dicom", "jp2", "j2k", "tga", "webp", "tif", "gif");
List<String> vectorFormats = Arrays.asList("svg", "otg", "odg", "eps",
"wmf", "emf", "wmz", "emz", "cmx", "cdr");
List<String> allFormats = new LinkedList<>(rasterFormats);
allFormats.addAll(vectorFormats);
allFormats.forEach(new Consumer<String>() {
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "couple." + formatExt;
boolean isVectorFormat = vectorFormats.contains(formatExt);
//Need to rasterize vector formats before background remove
if (isVectorFormat)
{
inputFile = rasterizeVectorImage(formatExt, inputFile);
}
String outputFile = templatesFolder + "remove_background." + formatExt;
System.out.println("Processing " + formatExt);
try (RasterImage image = (RasterImage) Image.load(inputFile))
{
//Additional code examples can be found at
//https://docs.aspose.com/imaging/java/remove-background-from-images/#graph-cut-auto-masking-using-imagingcloud-api
AutoMaskingGraphCutOptions maskingOptions = new AutoMaskingGraphCutOptions();
maskingOptions.setCalculateDefaultStrokes(true);
maskingOptions.setFeatheringRadius(1);
maskingOptions.setMethod(SegmentationMethod.GraphCut);
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setSource(new FileCreateSource(outputFile, false));
maskingOptions.setExportOptions(pngOptions);
maskingOptions.setBackgroundReplacementColor(Color.getGreen());
try (MaskingResult maskingResult = new ImageMasking(image)
.decompose(maskingOptions))
{
try (Image resultImage = maskingResult.get_Item(1).getImage())
{
resultImage.save();
}
}
}
//Remove rasterized vector image
if (isVectorFormat)
{
new File(inputFile).delete();
}
}
});
}
private static String rasterizeVectorImage(String formatExt, String inputFile)
{
String outputFile = templatesFolder + "rasterized."+ formatExt + ".png";
try (Image image = Image.load(inputFile))
{
image.save(outputFile, new PngOptions());
}
return outputFile;
}
class UserMarker
{
private final List<Point> list = new LinkedList<>();
public UserMarker addPoint(int left, int top, int radius)
{
for (int y = top - radius; y <= top + radius; y++)
{
for (int x = left - radius; x <= left + radius; x++)
{
this.list.add(new Point(x, y));
}
}
return this;
}
public Point[] getPoints()
{
return this.list.toArray(new Point[0]);
}
}
 
  • About Aspose.Imaging for Java API

    Aspose.Imaging API is an image processing solution to create, modify, draw or convert images (photos) within applications. It offers: cross-platform Image processing, including but not limited to conversions between various image formats (including uniform multi-page or multi-frame image processing), modifications such as drawing, working with graphic primitives, transformations (resize, crop, flip&rotate, binarization, grayscale, adjust), advanced image manipulation features (filtering, dithering, masking, deskewing), and memory optimization strategies. It’s a standalone library and does not depend on any software for image operations. One can easily add high-performance image conversion features with native APIs within projects. These are 100% private on-premise APIs and images are processed at your servers.

    Remove background in EMF via Online App

    Remove background in EMF documents by visiting our Live Demos website . The live demo has the following benefits

      No need to download or setup anything
      No need to write any code
      Just upload your EMF files and hit "Remove background now" button
      Instantly get the download link for the resultant file

    EMF What is EMF File Format

    Enhanced metafile format (EMF) stores graphical images device-independently. Metafiles of EMF comprises of variable-length records in chronological order that can render the stored image after parsing on any output device. These variable-length records can be definitions of enclosed objects, commands for drawing, and graphics properties critical to render the image accurately. When a device opens an EMF metafile using its own graphics environment, the proportions, dimensions, colors and other graphic properties of original image remains same regardless of the opening device platform.

    Read More

    Other Supported Remove background Formats

    Using Java, one can easily remove background from different formats including.

    APNG (Animated Portable Network Graphics)
    BMP (Bitmap Picture)
    ICO (Windows icon)
    JPG (Joint Photographic Experts Group)
    DIB (Device Independent Bitmap)
    DICOM (Digital Imaging & Communications)
    DJVU (Graphics Format)
    DNG (Digital Camera Image)
    EMZ (Windows Compressed Enhanced Metafile)
    GIF (Graphical Interchange Format)
    JP2 (JPEG 2000)
    J2K (Wavelet Compressed Image)
    PNG (Portable Network Graphics)
    TIFF (Tagged Image Format)
    WEBP (Raster Web Image)
    WMF (Microsoft Windows Metafile)
    WMZ (Compressed Windows Media Player Skin)
    TGA (Targa Graphic)
    SVG (Scalable Vector Graphics)
    EPS (Encapsulated PostScript Language)
    CDR (Vector Drawing Image)
    CMX (Corel Exchange Image)
    OTG (OpenDocument Standard)
    ODG (Apache OpenOffice Draw Format)