通过 Java 从 WEBP 中删除背景
构建您自己的 Java 应用程序以使用服务器端 API 从 WEBP 文件中删除背景。
如何使用 Java 删除 WEBP 文件中的背景
从图像中去除背景涉及隔离前景对象,这是一项需要对象识别的任务。存在多种方法来识别 WEBP 格式照片中的对象。对于具有统一颜色背景的简单图像,自动方法就足够了。然而,对于具有多个或部分合并人物的照片,预先标记对象就变得必要。这涉及指定要删除的矩形区域和对象类型。在复杂的情况下,Cloud API 可以实现自动对象检测。 Cloud API 提供了一个云应用程序,能够识别照片中的对象,利用生成的轮廓来去除背景。去除后,可以平滑图形留下的边缘以提高图像质量。为了删除 WEBP 文件中的背景,我们将使用 Aspose.Imaging for Java API 是一个功能丰富、功能强大 易于使用的 Java 平台图像处理和转换 API。您可以直接从 Maven
并通过将以下配置添加到 pom.xml 将其安装在基于 Maven 的项目中。
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>
通过 Java 从 WEBP 中删除背景的步骤
你需要 aspose-imaging-version-jdk16.jar 在您自己的环境中尝试以下工作流程。
- 使用 Image.load 方法加载 WEBP 文件
- 移除背景;
- 以 Aspose.Imaging 支持的格式将图像保存到光盘
系统要求
所有主要操作系统都支持 Java 的 Aspose.Imaging。只需确保您具有以下先决条件。
- 已安装 JDK 1.6 或更高版本。
删除 WEBP 图像中的背景 - 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]); | |
} | |
} |
关于 Java API 的 Aspose.Imaging
Aspose.Imaging API 是一种图像处理解决方案,用于在应用程序中创建、修改、绘制或转换图像(照片)。它提供:跨平台的图像处理,包括但不限于各种图像格式之间的转换(包括统一的多页或多帧图像处理)、绘图等修改、使用图形基元、转换(调整大小、裁剪、翻转和旋转) 、二值化、灰度、调整)、高级图像处理功能(过滤、抖动、遮罩、去偏斜)和内存优化策略。它是一个独立的库,不依赖任何软件进行图像操作。可以在项目中使用原生 API 轻松添加高性能图像转换功能。这些是 100% 私有的本地 API,图像在您的服务器上处理。通过在线应用删除 WEBP 中的背景
通过访问我们的 Live Demos 网站 移除 WEBP 文档中的背景。 现场演示有以下好处
WEBP 什么是 WEBP 文件格式
WebP 是 Google 推出的一种现代光栅 Web 图像文件格式,它基于无损和有损压缩。它提供相同的图像质量,同时大大减小了图像尺寸。由于大多数网页使用图像作为数据的有效表示,因此在网页中使用 WebP 图像会导致网页加载速度更快。根据谷歌的说法,WebP 无损图像的大小比 PNG 小 26%,而 WebP 有损图像比同类 JPEG 图像小 25-34%。基于 WebP 和其他图像文件格式之间的结构相似性 (SSIM) 索引比较图像。 WebP 是 WebM 多媒体容器格式的姊妹项目。
阅读更多其他支持的删除背景格式
使用 Java,可以轻松地从不同格式中删除背景,包括。