通過 Java 從 DICOM 中刪除背景
構建您自己的 Java 應用程序以使用服務器端 API 從 DICOM 文件中刪除背景。
如何使用 Java 刪除 DICOM 文件中的背景
從圖像中去除背景涉及隔離前景對象,這是一項需要對象識別的任務。存在多種方法來識別 DICOM 格式照片中的對象。對於具有統一顏色背景的簡單圖像,自動方法就足夠了。然而,對於具有多個或部分合併人物的照片,預先標記對象就變得必要。這涉及指定要刪除的矩形區域和對像類型。在復雜的情況下,Cloud API 可以實現自動對象檢測。 Cloud API 提供了一個雲應用程序,能夠識別照片中的對象,利用生成的輪廓來去除背景。去除後,可以平滑圖形留下的邊緣以提高圖像質量。為了刪除 DICOM 文件中的背景,我們將使用 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 從 DICOM 中刪除背景的步驟
你需要 aspose-imaging-version-jdk16.jar 在您自己的環境中嘗試以下工作流程。
- 使用 Image.load 方法加載 DICOM 文件
- 移除背景;
- 以 Aspose.Imaging 支持的格式將圖像保存到光盤
系統要求
所有主要操作系統都支持 Java 的 Aspose.Imaging。只需確保您具有以下先決條件。
- 已安裝 JDK 1.6 或更高版本。
刪除 DICOM 圖像中的背景 - 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,圖像在您的服務器上處理。通過在線應用刪除 DICOM 中的背景
通過訪問我們的 Live Demos 網站 移除 DICOM 文檔中的背景。 現場演示有以下好處
DICOM 什麼是 DICOM 文件格式
DICOM 是 Digital Imaging and Communications in Medicine 的首字母縮寫詞,屬於醫學信息學領域。 DICOM 是文件格式定義和網絡通信協議的結合。 DICOM 使用 .DCM 擴展名。 .DCM 以兩種不同的格式存在,即格式 1.x 和格式 2.x。 DCM Format 1.x 還提供兩個普通版本和擴展版本。 DICOM 用於集成來自不同供應商的打印機、服務器、掃描儀等醫療成像設備,還包含每個患者的唯一識別數據。如果 DICOM 文件能夠接收 DICOM 格式的圖像數據,則它們可以在兩方之間共享。 DICOM的通信部分是應用層協議,實體之間使用TCP/IP進行通信。 HTTP 和 HTTPS 協議用於 DICOM 的 Web 服務。 Web 服務支持的版本是 1.0、1.1、2 或更高版本。
閱讀更多其他支持的刪除背景格式
使用 Java,可以輕鬆地從不同格式中刪除背景,包括。