翻譯|使用教程|編輯:胡濤|2022-12-21 10:04:06.487|閱讀 248 次
概述:本教程通過調整示例 LEADTOOLS 媒體文件之一的大小來對高清視頻進行編碼,以創建與 MPEG-4 AVC/H.264 BD 兼容的 DICOM 文件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
LEADTOOLS是一個綜合工具包的集合,用于將識別、文檔、醫療、成像和多媒體技術整合到桌面、服務器、平板電腦、網絡和移動解決方案中,是一項企業級文檔自動化解決方案,有捕捉,OCR,OMR,表單識別和處理,PDF,打印捕獲,歸檔,注釋和顯示功能。利用業界領先的圖像處理技術,能夠智能識別文件,可以用來識別任何類型的掃描或傳真形式的圖像。
本教程介紹如何配置 LEADTOOLS DICOM Writer 過濾器,以使用 LEADTOOLS SDK 在 C# Window 控制臺應用程序中使用 MPEG-2 傳輸中的 H.264 創建數據集。本教程通過調整示例 LEADTOOLS 媒體文件之一的大小來對高清視頻進行編碼,以創建與 MPEG-4 AVC/H.264 BD 兼容的 DICOM 文件。
| 概述 | |
|---|---|
| 概括 | 本教程介紹如何使用 LEADTOOLS DICOM Writer 過濾器在 C# Windows 控制臺應用程序中創建包含 H.264 壓縮視頻的 DICOM 文件。 |
| 完成時間 | 30分鐘 |
| 視覺工作室項目 | |
| 平臺 | Windows 控制臺 C# 應用程序 |
| 集成開發環境 | 視覺工作室 2017、2019 |
| 開發許可 |
在使用文件觀察器轉換文件 - C# .NET Core教程之前,通過查看添加引用和設置許可證教程熟悉創建項目的基本步驟。
教程中創建的項目副本開始。如果您沒有該項目,請按照該教程中的步驟創建它。
所需的參考取決于項目的目的??梢酝ㄟ^本地 DLL 引用添加引用。
本教程需要以下本地 DLL,它們位于<INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:
還需要以下非 LEADTOOLS DLL:
有關特定功能所需的 DLL 的完整列表,請參閱。
不同的 SDK 功能需要不同的引用。有關完整列表,請參閱。
設置許可證文件許可證解鎖項目所需的功能。它必須在調用任何工具包函數之前設置。有關詳細信息,包括針對不同平臺的教程,請參閱設置運行時許可證。
有兩種類型的運行時許可證:
筆記
添加 LEADTOOLS NuGet 引用和設置許可證在添加引用和設置許可證教程 中有更詳細的介紹。
創建項目、添加參考和設置許可證后,就可以開始編碼了。
在解決方案資源管理器中,打開Program.cs。將以下語句添加到頂部的 using 塊中Program.cs:
【C#】
using System; using System.IO; using Leadtools; using Leadtools.Dicom; using Leadtools.Dicom.Common.DataTypes; using Leadtools.Dicom.Common.Extensions; using Leadtools.Multimedia; using LTDicWrtLib;
請務必添加以下全局變量:
【C?!?/span>
static ConvertCtrl convert;
在Program類中,添加一個名為 的新方法CreateDICOMFile()。在方法內部調用這個方法Main(),如下圖。此外,請務必Single-threaded Apartment (STA)通過添加[STAThread].
【C?!?/span>
[STAThread]
static void Main(string[] args)
{
SetLicense();
CreateDICOMFile();
}
將以下代碼添加CreateDICOMFile()到編碼高清視頻的方法中,以創建 MPEG-4 AVC/H.264 BD 兼容的 DICOM 文件。您可以在此處.DS下載用于本教程的臨時文件。
【C#】
private static void CreateDICOMFile()
{
string templateFile = @"FILE PATH TO TEMP Video_Photography.DS FILE";
string inputFile = @"C:\LEADTOOLS22\Resources\Media\DaDa_H264.mp4";
string outputFile = @"FILE PATH TO OUTPUT THE DCM FILE TO";
// Create instance of Convert control.
convert = new ConvertCtrl();
// Set the source and target files
convert.SourceFile = inputFile;
convert.TargetFile = outputFile;
// Need to resize sample video to be HD since no HD videos are in the SDK setup
convert.SelectedVideoProcessors.Add(convert.VideoProcessors.Resize);
LMVResizeLib.LMVResize resizeFilter = convert.GetSubObject(ConvertObject.SelVideoProcessor + 0) as LMVResizeLib.LMVResize;
resizeFilter.ForceSquarePixelOutput = true;
resizeFilter.Height = 1080;
resizeFilter.Width = 1920;
resizeFilter.OutputAspectRatioMode = LMVResizeLib.OutputAspectRatioModeConstants.OUTPUTASPECTRATIO_16_9;
resizeFilter.HeightControlMode = LMVResizeLib.SizeControlModeConstants.SIZECONTROL_FIXED;
resizeFilter.WidthControlMode = LMVResizeLib.SizeControlModeConstants.SIZECONTROL_FIXED;
// Done configuring the resize filter
System.Runtime.InteropServices.Marshal.ReleaseComObject(resizeFilter);
resizeFilter = null;
convert.VideoCompressors.H264.Selected = true;
convert.AudioCompressors.AAC.Selected = true;
LMH264EncoderLib.LMH264Encoder h264Encoder = convert.GetSubObject(ConvertObject.VideoCompressor) as LMH264EncoderLib.LMH264Encoder;
h264Encoder.EnableRateControl = false;
h264Encoder.QualityFactor = 28;
h264Encoder.FrameRate = 29.970f;
h264Encoder.EncodingSpeed = LMH264EncoderLib.eH264ENCODINGSPEED.H264SPEED_4;
h264Encoder.EncodingThreads = LMH264EncoderLib.eH264ENCODINGTHREADS.H264THREAD_AUTO;
h264Encoder.IFrameInterval = 11;
h264Encoder.PFrameInterval = 1;
h264Encoder.OutputFormat = LMH264EncoderLib.eH264OUTPUTFORMAT.H264FORMAT_HIGH_H264;
// Done configuring the filter
System.Runtime.InteropServices.Marshal.ReleaseComObject(h264Encoder);
h264Encoder = null;
// set the format
convert.TargetFormat = TargetFormatType.MPEG2TransportDICOM;
// Get the Dicom writer filter and set some options
//NOTE: MPEG-2 Transport is the target filter here
// DICOM writer is the file writer/Sink filter
ILTDicWrt2 dicomWriter = (ILTDicWrt2)convert.GetSubObject(ConvertObject.Sink);
dicomWriter.DICOMTemplateFile = templateFile;
dicomWriter.MPEG2DicomCompatibilityOption = MPEG2DicomCompatibilityConstants.MPEG2DICOMCOMP_IGNORE;
// Done configuring the filter
System.Runtime.InteropServices.Marshal.ReleaseComObject(dicomWriter);
dicomWriter = null;
// Add some event handlers
convert.Progress += new ProgressEventHandler(convert_Progress);
convert.Complete += new EventHandler(convert_Complete);
Console.WriteLine(string.Format("Starting to Convert {0}", convert.TargetFile));
convert.StartConvert();
while (convert.State != ConvertState.Stopped)
{
System.Windows.Forms.Application.DoEvents();
}
DicomEngine.Startup();
DicomDataSet ds = new DicomDataSet();
ds.Load(convert.TargetFile, DicomDataSetLoadFlags.LoadAndClose);
PatientBase patientBase = ds.Get<PatientBase>();
patientBase.PatientName = new PersonName("Smith^John");
patientBase.PatientSex = PatientSex.Male;
patientBase.PatientID = "1234567890";
patientBase.PatientBirthDate = DateTime.Parse("09/18/1970");
patientBase.PatientBirthTime = DateTime.Parse(DateTime.Now.ToShortTimeString());
ds.Set(patientBase);
DicomElement element;
element = ds.FindFirstElement(null, DicomTag.SOPInstanceUID, true);
if (element != null)
ds.SetValue(element, Leadtools.DicomDemos.Utils.GenerateDicomUniqueIdentifier());
GenerateUidTag(ds, DicomTag.StudyInstanceUID);
GenerateUidTag(ds, DicomTag.SeriesInstanceUID);
GenerateUidTag(ds, DicomTag.SOPInstanceUID);
ds.Save(convert.TargetFile, DicomDataSetSaveFlags.None);
DicomEngine.Shutdown();
Console.WriteLine("Patient information updated and new UIDs generated.");
}
在Program類中,添加一個名為 的新方法GenerateUidTag(DicomDataSet dicom, long UidTag)。CreateDICOMFile()如上所示,將在方法內部調用此方法以創建唯一標識符。
private static void GenerateUidTag(DicomDataSet dicom, long UidTag)
{
DicomElement element;
element = dicom.FindFirstElement(null, UidTag, true);
if (element != null)
dicom.SetValue(element, Leadtools.DicomDemos.Utils.GenerateDicomUniqueIdentifier());
}
Program在名為convert_Complete(object sender, EventArgs e)and的類中添加兩個新的事件處理程序convert_Progress(object sender, ProgressEventArgs e)。這些將在CreateDICOMFile()方法內部使用,如上所示,并將用于將應用程序進度打印到控制臺的目的。這些是可選的。
private static void convert_Complete(object sender, EventArgs e)
{
Console.WriteLine(string.Format("\nConversion of \"{0}\" complete!", convert.TargetFile));
}
private static void convert_Progress(object sender, ProgressEventArgs e)
{
Console.Write(string.Format("\rConversion progress: {0}%", e.percent));
}
<PROJECT_NAME>.csproj在Solution Explorer中右鍵單擊并選擇Add -> New Item...。選擇類選項并命名文件Utils.cs。
將以下語句添加到頂部的 using 塊中Utils.cs:
using System; using System.Text; using System.Diagnostics;
在Utils類中,添加以下變量。
private static String _prevTime; private static String _leadRoot = null; private static Object _lock = new object(); private static int _count = 0; private const int _maxCount = int.MaxValue;
Utils向名為的類添加一個新方法GenerateDicomUniqueIdentifier()。添加以下代碼以生成 UID。
public static string GenerateDicomUniqueIdentifier()
{
try
{
lock (_lock)
{
// yyyy four digit year
// MM month from 01 to 12
// dd 01 to 31
// HH hours using a 24-hour clock form 00 to 23
// mm minute 00 to 59
// ss second 00 to 59
// fffffff ten millionths of a second
const string dateFormatString = "yyyyMMdd.HHmmss.fffffff";
string sUidRet = "";
if (_leadRoot == null)
{
StringBuilder sb = new StringBuilder();
sb.Append("1.2.840.114257.1.1");
// Process Id
sb.AppendFormat(".{0}", (uint)Process.GetCurrentProcess().Id);
_leadRoot = sb.ToString();
_prevTime = DateTime.UtcNow.ToString(dateFormatString);
}
StringBuilder uid = new StringBuilder();
uid.Append(_leadRoot);
String time = DateTime.UtcNow.ToString(dateFormatString);
if (time.Equals(_prevTime))
{
if (_count == _maxCount)
throw new Exception("GenerateDicomUniqueIdentifier error -- max count reached.");
_count++;
}
else
{
_count = 1;
_prevTime = time;
}
uid.AppendFormat(".{0}.{1}", time, _count);
sUidRet = uid.ToString();
// This should not happen
if (sUidRet.Length > 64)
sUidRet = sUidRet.Substring(0, 64);
return sUidRet;
}
}
catch (Exception ex)
{
throw ex;
}
}
按F5或選擇Debug -> Start Debugging運行項目。
如果正確遵循這些步驟,應用程序將運行并在 MPEG-2 傳輸中使用 H.264 創建數據集。您可以在此處找到預期的輸出 DCM 和 AVI 文件。
以上便是使用 H264 視頻創建 DICOM 文件,如果您還有其他疑問,歡迎咨詢我們或者加入我們官方技術交流群。
歡迎下載|體驗更多LEADTOOL產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn