Fo-dicom,第一個(gè)基于.NET Standard 2.0 開發(fā)的DICOM開源庫
1. 簡介:
fo-dicom是一個(gè)基于C#開發(fā)的庫,用于處理DICOM(Digital Imaging and Communications in Medicine)格式的數(shù)據(jù)。DICOM是一種用于醫(yī)學(xué)影像和相關(guān)信息的標(biāo)準(zhǔn)格式,廣泛應(yīng)用于醫(yī)學(xué)領(lǐng)域。fo-dicom提供了多平臺(tái)支持,可在 Windows、Linux 和 macOS 等操作系統(tǒng)上運(yùn)行。
fo-dicom庫的設(shè)計(jì)理念是提供一個(gè)方便、易用、功能強(qiáng)大的工具,用于處理、讀取、寫入和修改DICOM文件。該庫提供了豐富的API,支持對(duì)DICOM文件的標(biāo)簽進(jìn)行讀取和設(shè)置,支持對(duì)DICOM文件的編碼和解碼,支持對(duì)DICOM文件的傳輸和存儲(chǔ)。
fo-dicom庫還提供了對(duì)DICOM消息流的封裝,使得開發(fā)者可以方便地實(shí)現(xiàn)自定義的DICOM服務(wù)。該庫還支持對(duì)網(wǎng)絡(luò)底層的封裝,使得開發(fā)者可以輕松地實(shí)現(xiàn)基于網(wǎng)絡(luò)的DICOM通信。
開源庫地址:https://github.com/fo-dicom/fo-dicom。
2. 開發(fā)的背景和目的:
fo-dicom庫的產(chǎn)生是為了解決醫(yī)學(xué)圖像處理和DICOM數(shù)據(jù)交換的需求。在醫(yī)學(xué)領(lǐng)域,DICOM(Digital Imaging and Communications in Medicine)是一種用于存儲(chǔ)、傳輸和共享醫(yī)學(xué)影像和相關(guān)信息的標(biāo)準(zhǔn)。由于醫(yī)學(xué)影像數(shù)據(jù)的特殊性和復(fù)雜性,需要一個(gè)專門的庫來處理DICOM數(shù)據(jù),并提供方便的接口和工具。
背景上來說,DICOM標(biāo)準(zhǔn)的出現(xiàn)是為了解決各種醫(yī)學(xué)設(shè)備之間的互操作性問題。在過去,不同廠商的醫(yī)學(xué)設(shè)備使用自己的私有格式來存儲(chǔ)和傳輸影像數(shù)據(jù),這導(dǎo)致了數(shù)據(jù)共享和集成的困難。DICOM標(biāo)準(zhǔn)的制定使得不同設(shè)備可以使用統(tǒng)一的格式和通信協(xié)議,從而實(shí)現(xiàn)醫(yī)學(xué)影像的無縫交流和協(xié)作。
fo-dicom作為一個(gè)開源的DICOM庫,旨在提供一個(gè)易于使用且功能強(qiáng)大的工具,使得開發(fā)者能夠處理醫(yī)學(xué)圖像和相關(guān)數(shù)據(jù)。它基于DICOM標(biāo)準(zhǔn),提供了讀取、創(chuàng)建、修改和存儲(chǔ)DICOM數(shù)據(jù)的功能,同時(shí)支持醫(yī)學(xué)圖像的加載、處理和保存。此外,fo-dicom還具備與遠(yuǎn)程PACS(Picture Archiving and Communication System)或其他DICOM節(jié)點(diǎn)的網(wǎng)絡(luò)通信能力,以及查詢和檢索功能,方便用戶根據(jù)條件查詢和獲取DICOM實(shí)例。
3.主要特點(diǎn)和安裝方式
閱讀官方文檔,即可獲得安裝方法:https://fo-dicom.github.io/stable/v5/index.html。
4. 主要功能:
- DICOM 數(shù)據(jù)處理:fo-dicom 支持讀取、創(chuàng)建、修改和存儲(chǔ) DICOM 數(shù)據(jù)。用戶可以輕松訪問和操作 DICOM 文件和數(shù)據(jù)集。
- 圖像處理:該庫提供了對(duì)醫(yī)學(xué)圖像的加載、處理和保存功能。用戶可以進(jìn)行像素級(jí)別的操作、圖像增強(qiáng)、格式轉(zhuǎn)換等操作。
- DICOM 網(wǎng)絡(luò)通信:fo-dicom 具備與遠(yuǎn)程 PACS 或其他 DICOM 節(jié)點(diǎn)的網(wǎng)絡(luò)通信能力,使用戶可以發(fā)送和接收 DICOM 消息。
- 查詢和檢索:fo-dicom 實(shí)現(xiàn)了查詢和檢索功能,用戶可以根據(jù)條件進(jìn)行 DICOM 實(shí)例的查詢和獲取。這有助于快速訪問所需的醫(yī)學(xué)圖像和相關(guān)數(shù)據(jù)。
5. 使用說明:
圖像渲染配置
開箱即用的 fo-dicom 默認(rèn)為內(nèi)部類FellowOakDicom.Imaging.IImage 樣式的圖像渲染。若要切換到桌面樣式或 ImageSharp 樣式的圖像呈現(xiàn),首先必須添加所需的 nuget 包,然后調(diào)用:
new DicomSetupBuilder()
.RegisterServices(s => s.AddFellowOakDicom().AddImageManager<WinFormsImageManager>())
.Build();
或:
new DicomSetupBuilder()
.RegisterServices(s => s.AddFellowOakDicom().AddImageManager<ImageSharpImageManager>())
.Build();
然后,在渲染時(shí),可以通過以下方式將 IImage 強(qiáng)制轉(zhuǎn)換為類型。
var image = new DicomImage("filename.dcm");
var bitmap = image.RenderImage().As<Bitmap>();
或:
var image = new DicomImage("filename.dcm");
var sharpimage = image.RenderImage().AsSharpImage();
日志記錄配置
Fellow Oak DICOM 使用 ,因此如果您已經(jīng)在使用它,則 Fellow Oak DICOM 日志記錄將自動(dòng)顯示。
Microsoft.Extensions.Logging
過去,F(xiàn)ellow Oak DICOM 有一個(gè)用于日志記錄的自定義抽象:ILogger 和 ILogManager。 出于向后兼容性的目的,這仍然受支持,但不建議用于新應(yīng)用程序。
services.AddLogManager<MyLogManager>();
其中 MyLogManager 如下所示:
using FellowOakDicom.Log;
public class MyLogManager: ILogManager {
public ILogger GetLogger(string name) {
...
}
}
示例應(yīng)用程序
這里有許多使用 fo-dicom 的簡單示例應(yīng)用程序,它們位于單獨(dú)的存儲(chǔ)庫中。這些還包括示例 以前包含在 VS 解決方案的“示例”子文件夾中。
6.例子
文件操作
var file = DicomFile.Open(@"test.dcm"); // Alt 1
var file = await DicomFile.OpenAsync(@"test.dcm"); // Alt 2
var patientid = file.Dataset.GetString(DicomTag.PatientID);
file.Dataset.AddOrUpdate(DicomTag.PatientName, "DOE^JOHN");
// creates a new instance of DicomFile
var newFile = file.Clone(DicomTransferSyntax.JPEGProcess14SV1);
file.Save(@"output.dcm"); // Alt 1
await file.SaveAsync(@"output.dcm"); // Alt 2
將圖像渲染為 JPEG
var image = new DicomImage(@"test.dcm");
image.RenderImage().AsBitmap().Save(@"test.jpg"); // Windows Forms
C-Store SCU系列
var client = DicomClientFactory.Create("127.0.0.1", 12345, false, "SCU", "ANY-SCP");
await client.AddRequestAsync(new DicomCStoreRequest(@"test.dcm"));
await client.SendAsync();
C-Echo SCU/SCP
var server = new DicomServer<DicomCEchoProvider>(12345);
var client = DicomClientFactory.Create("127.0.0.1", 12345, false, "SCU", "ANY-SCP");
client.NegotiateAsyncOps();
// Optionally negotiate user identity
client.NegotiateUserIdentity(new DicomUserIdentityNegotiation
{
UserIdentityType = DicomUserIdentityType.Jwt,
PositiveResponseRequested = true,
PrimaryField = "JWT_TOKEN"
});
for (int i = 0; i < 10; i++)
await client.AddRequestAsync(new DicomCEchoRequest());
await client.SendAsync();
C-Find SCU
var cfind = DicomCFindRequest.CreateStudyQuery(patientId: "12345");
cfind.OnResponseReceived = (DicomCFindRequest rq, DicomCFindResponse rp) => {
Console.WriteLine("Study UID: {0}", rp.Dataset.GetString(DicomTag.StudyInstanceUID));
};
var client = DicomClientFactory.Create("127.0.0.1", 11112, false, "SCU-AE", "SCP-AE");
await client.AddRequestAsync(cfind);
await client.SendAsync();
C-Move SCU系列
var cmove = new DicomCMoveRequest("DEST-AE", studyInstanceUid);
var client = DicomClientFactory.Create("127.0.0.1", 11112, false, "SCU-AE", "SCP-AE");
await client.AddRequestAsync(cmove);
await client.SendAsync();
N-Action SCU
// It is better to increase 'associationLingerTimeoutInMs' default is 50 ms, which may not be
// be sufficient
var dicomClient = DicomClientFactory.Create("127.0.0.1", 12345, false, "SCU-AE", "SCP-AE",
DicomClientDefaults.DefaultAssociationRequestTimeoutInMs, DicomClientDefaults.DefaultAssociationReleaseTimeoutInMs,5000);
var txnUid = DicomUIDGenerator.GenerateDerivedFromUUID().UID;
var nActionDicomDataSet = new DicomDataset
{
{ DicomTag.TransactionUID, txnUid }
};
var dicomRefSopSequence = new DicomSequence(DicomTag.ReferencedSOPSequence);
var seqItem = new DicomDataset()
{
{ DicomTag.ReferencedSOPClassUID, "1.2.840.10008.5.1.4.1.1.1" },
{ DicomTag.ReferencedSOPInstanceUID, "1.3.46.670589.30.2273540226.4.54" }
};
dicomRefSopSequence.Items.Add(seqItem);
nActionDicomDataSet.Add(dicomRefSopSequence);
var nActionRequest = new DicomNActionRequest(DicomUID.StorageCommitmentPushModelSOPClass,
DicomUID.StorageCommitmentPushModelSOPInstance, 1)
{
Dataset = nActionDicomDataSet,
OnResponseReceived = (DicomNActionRequest request, DicomNActionResponse response) =>
{
Console.WriteLine("NActionResponseHandler, response status:{0}", response.Status);
},
};
await dicomClient.AddRequestAsync(nActionRequest);
dicomClient.OnNEventReportRequest = OnNEventReportRequest;
await dicomClient.SendAsync();
private static Task<DicomNEventReportResponse> OnNEventReportRequest(DicomNEventReportRequest request)
{
var refSopSequence = request.Dataset.GetSequence(DicomTag.ReferencedSOPSequence);
foreach(var item in refSopSequence.Items)
{
Console.WriteLine("SOP Class UID: {0}", item.GetString(DicomTag.ReferencedSOPClassUID));
Console.WriteLine("SOP Instance UID: {0}", item.GetString(DicomTag.ReferencedSOPInstanceUID));
}
return Task.FromResult(new DicomNEventReportResponse(request, DicomStatus.Success));
}
具有高級(jí) DICOM 客戶端連接的 C-ECHO:手動(dòng)控制 TCP 連接和 DICOM 關(guān)聯(lián)。
var cancellationToken = CancellationToken.None;
// Alternatively, inject IDicomServerFactory via dependency injection instead of using this static method
using var server = DicomServerFactory.Create<DicomCEchoProvider>(12345);
var connectionRequest = new AdvancedDicomClientConnectionRequest
{
NetworkStreamCreationOptions = new NetworkStreamCreationOptions
{
Host = "127.0.0.1",
Port = server.Port,
}
};
// Alternatively, inject IAdvancedDicomClientConnectionFactory via dependency injection instead of using this static method
using var connection = await AdvancedDicomClientConnectionFactory.OpenConnectionAsync(connectionRequest, cancellationToken);
var associationRequest = new AdvancedDicomClientAssociationRequest
{
CallingAE = "EchoSCU",
CalledAE = "EchoSCP",
// Optionally negotiate user identity
UserIdentityNegotiation = new DicomUserIdentityNegotiation
{
UserIdentityType = DicomUserIdentityType.UsernameAndPasscode,
PositiveResponseRequested = true,
PrimaryField = "USERNAME",
SecondaryField = "PASSCODE",
}
};
var cEchoRequest = new DicomCEchoRequest();
using var association = await connection.OpenAssociationAsync(associationRequest, cancellationToken);
try
{
DicomCEchoResponse cEchoResponse = await association.SendCEchoRequestAsync(cEchoRequest, cancellationToken).ConfigureAwait(false);
Console.WriteLine(cEchoResponse.Status);
}
finally
{
await association.ReleaseAsync(cancellationToken);
}
7. 社區(qū)和生態(tài):
fo-dicom 有一個(gè)活躍的社區(qū),包括眾多貢獻(xiàn)者和維護(hù)者。它在 GitHub 上有一個(gè)開放的倉庫,用戶可以在其中提交問題、提出建議和貢獻(xiàn)代碼。fo-dicom 的更新頻率較高,并得到了廣泛的應(yīng)用和認(rèn)可。
為了方便新手學(xué)習(xí)官方構(gòu)建了樣例庫:https://github.com/fo-dicom/fo-dicom-samples。
同時(shí)支持多個(gè)平臺(tái)的案例開發(fā)開發(fā):
8. 優(yōu)勢和劣勢:
- 優(yōu)勢:fo-dicom 是一個(gè)強(qiáng)大且易于使用的 DICOM 庫,具備處理醫(yī)學(xué)圖像和相關(guān)數(shù)據(jù)的核心功能。它提供了多平臺(tái)支持、良好的文檔和示例代碼,并擁有一個(gè)活躍的社區(qū)。
- 劣勢:由于 DICOM 標(biāo)準(zhǔn)的復(fù)雜性,初學(xué)者可能需要一些時(shí)間來適應(yīng) fo-dicom 的使用方式。另外,某些高級(jí)功能可能需要額外的配置或第三方組件的支持。
9. 未來計(jì)劃和發(fā)展方向:
fo-dicom 的未來計(jì)劃包括進(jìn)一步增強(qiáng)圖像處理功能、優(yōu)化性能、改進(jìn)網(wǎng)絡(luò)通信和增加對(duì)新版 DICOM 標(biāo)準(zhǔn)的支持。通過不斷改進(jìn)和擴(kuò)展功能,fo-dicom 將繼續(xù)滿足用戶對(duì)醫(yī)學(xué)圖像處理和數(shù)據(jù)交互的需求。
今天先介紹到這里,后續(xù)我將持續(xù)分享關(guān)于fo-dicom庫的使用經(jīng)驗(yàn)技巧,歡迎有需要的朋友持續(xù)關(guān)注。