這是用戶在 2025-5-1 15:53 為 https://ww2.mathworks.cn/company/technical-articles/accessing-data-in-dicom-files.html 保存的雙語快照頁面,由 沉浸式翻譯 提供雙語支持。了解如何保存?
訪問 DICOM 檔中的數據

Technical Articles   技術文章

Accessing data in DICOM files
訪問 DICOM 檔中的數據

By Jeff Mather, MathWorks
作者:Jeff Mather,MathWorks


The DICOM (Digital Imaging and Communication in Medicine) format describes how to compose messages to send between imaging modalities (e.g., Computed Tomography (CT), Magnetic Resonance (MR), and ultrasound devices) and defines a set of operations for transmitting them across a network. These messages can also be written to files for offline storage on a picture archiving system, CD, or other type of storage device. DICOM-formatted messages combine images and metadata to create a rich description of a medical imaging procedure. This format is extremely detailed, with a specification that is more than 2,500 pages long.
DICOM (Digital Imaging and Communication in Medicine) 格式描述了如何編寫消息以在成像模式( 例如 ,計算機斷層掃描 (CT)、磁共振 (MR) 和超聲設備)之間發送,並定義了一組通過網路傳輸消息的作。還可以將這些消息寫入檔,以便在圖片存檔系統、CD 或其他類型的存儲設備上進行離線存儲。DICOM 格式的消息將圖像和元數據組合在一起,以創建醫學成像程式的豐富描述。這種格式非常詳細,規範長達 2,500 多頁。

MATLAB and the Image Processing Toolbox provide easy access to DICOM data. Accessing data in DICOM files becomes as easy as working with TIFF or JPEG images. This article presents examples of using DICOM and provides background information about the format.
MATLAB 和 Image Processing Toolbox 提供對 DICOM 數據的輕鬆訪問。訪問 DICOM 檔中的數據就像使用 TIFF 或 JPEG 圖像一樣簡單。本文提供了使用 DICOM 的範例,並提供了有關該格式的背景資訊。

Before DICOM: Chaos  DICOM 之前:混沌

DICOM has significantly improved communication between medical devices and lowered the cost and complexity of integrating hardware and software solutions. Before DICOM, each manufacturer used proprietary image formats and communications protocols to connect their hardware solutions with third-party products. Integrating medical hardware and software from different vendors meant translating from one vendor's protocols to another's. This process was chaotic and fraught with difficulty. A cottage industry developed to provide data translation services.
DICOM 顯著改善了醫療設備之間的通信,並降低了整合硬體和軟體解決方案的成本和複雜性。在 DICOM 之前,每個製造商都使用專有的圖像格式和通信協定將其硬體解決方案與第三方產品連接起來。集成來自不同供應商的醫療硬體和軟體意味著從一個供應商的協議轉換為另一個供應商的協定。這個過程很混亂,充滿了困難。為提供數據翻譯服務而開發的家庭手工業。

With the advent of DICOM as a formal standard in 1993, one protocol replaced many protocols and formats. DICOM is the common format, easing integration of solutions from different vendors. For example, it is possible to integrate a MR scanner from GE Medical Systems with a picture archive system (PACS) from Agfa and another vendor's film printer without using translation devices. Integration isn't limited to just hardware. Software such as MATLAB that supports DICOM can share images with all of these devices, provided that each of the hardware devices has implemented the necessary DICOM services.
隨著 DICOM 於 1993 年作為正式標準問世,一種協定取代了許多協定和格式。DICOM 是通用格式,可簡化來自不同供應商的解決方案的整合。例如,可以將 GE Medical Systems 的 MR 掃描器與 Agfa 的圖片存檔系統 (PACS) 和其他供應商的膠片印表機整合,而無需使用轉換設備。集成不僅限於硬體。支援 DICOM 的軟體(如 MATLAB)可以與所有這些設備共用圖像,前提是每個硬體設備都實施了必要的 DICOM 服務。

A Simple First Example  簡單的第一個例子

In medical imaging, a patient is subject to an imaging study, which may contain multiple series of images. Each series is performed on a single modality such as an MR, CT, or X-ray device and can have multiple related images. Suppose that we have a study consisting of a series of 20 transverse MRI brain images and we want to read them into MATLAB. (These 20 images are stored in 20 DICOM files with names such as brain_017.dcm, which you can download from MATLAB Central if you want to run the examples.) Let's suppose we know that each image is 256-by-256 and contains signed 16-bit data. We can read the series with the following code:
在醫學成像中,患者需要接受影像學研究 ,其中可能包含多個系列的圖像。每個系列都在單一模態 (如 MR、CT 或 X 射線設備)上執行,並且可以有多個相關圖像 。假設我們有一項研究,由一系列 20 張橫向 MRI 腦部圖像組成,我們想將它們讀入 MATLAB。(這 20 個圖像存儲在 20 個 DICOM 檔中,其名稱為 brain_017.dcm,如果要運行範例,可以從 MATLAB Central 下載這些檔。假設我們知道每個圖像都是 256 x 256 的,並且包含有符號的 16 位數據。我們可以使用以下代碼讀取該系列:

% Preallocate the 256-by-256-by-1-by-20 image array.
X = repmat(int16(0), [256 256 1 20]);
% Read the series of images.
 for p=1:20
   filename = sprintf('brain_%03d.dcm', p);
   X(:,:,1,p) = dicomread(filename);
end
% Display the image stack.
montage(X,[])

After running this code, the MATLAB workspace contains a 4-D array with the image data, and a plot of the MR slices appears.
運行此代碼后,MATLAB 工作區包含一個包含圖像數據的 4-D 陣列,並顯示 MR 切片圖。

dicom_fig1_w.jpg
Click on image to see enlarged view.
點擊圖片查看放大圖。

You can use MATLAB and the Image Processing Toolbox to perform multiple tasks with this data. For example, MATLAB provides sophisticated volume visualization techniques to reconstruct a 3-D surface from these slices and then apply surface and lighting effects. The example in the Help section for the isocaps function shows this implemented. The imtransform and tformarray functions in the Image Processing Toolbox make it easy to extract slices in different orientations from the transverse data. For example, the "Extracting Slices from a 3-Dimensional MRI Data Set" example shows how to extract saggital slices from a similar dataset. You can also use the morphology functions within the Image Processing Toolbox to perform operations such as image segmentation, feature extraction, and image statistics. Alternatively, you can write your own functions to perform volume estimation, shrinkwrapping, etc.
您可以使用 MATLAB 和 Image Processing Toolbox 使用此資料執行多項任務。例如,MATLAB 提供了複雜的體積可視化技術,可以從這些切片重建 3-D 表面,然後應用表面和照明效果。説明部分中 isocaps 函數的範例顯示了此實現。Image Processing Toolbox 中的 imtransformtformarray 函數可以輕鬆地從橫向數據中提取不同方向的切片。例如,「 從 3 維 MRI 數據集中提取切片 」示例顯示了如何從類似的數據集中提取下層切片。您還可以使用 Image Processing Toolbox 中的 morphology 函數來執行圖像分割、特徵提取和圖像統計等作。或者,您可以編寫自己的函數來執行體積估算、收縮包裝等。

Accessing DICOM Metadata  訪問 DICOM 元數據

In the previous example, we made several assumptions as we preallocated the storage array. All of these assumptions were based on metadata that exists in the DICOM file. Furthermore, the values of the image pixels are limited to a narrow band in the total possible dynamic range of the image, so we had to pass an empty array as a special argument to montage to rescale the data. Let's use the metadata attributes in the DICOM files to (1) intelligently preallocate our array and (2) rescale the data values to fill the 16-bit dynamic range.
在前面的示例中,我們在預分配存儲陣列時做了幾個假設。所有這些假設都基於 DICOM 檔中存在的元數據。此外,圖像圖元的值被限制在圖像總可能動態範圍中的窄帶內,因此我們必須將一個空數位作為特殊參數傳遞給蒙太奇以重新縮放數據。讓我們使用 DICOM 檔中的元資料屬性來 (1) 智慧地預分配我們的陣列,以及 (2) 重新縮放資料值以填充 16 位動態範圍。

Because all images in a series must have the same dimensions and bit-depth, we only need to get the metadata from one image in the series to preallocate the array. The Image Processing Toolbox function dicominfo returns the metadata from a DICOM file.
由於序列中的所有圖像必須具有相同的尺寸和位深度,因此我們只需要從序列中的一個圖像獲取元數據即可預分配陣列。Image Processing Toolbox 函數 dicominfo 從 DICOM 檔返回元數據。

dicom_code_w.gif

As you can see, there are many metadata values, or attributes. The values for "Rows," "Columns," and "BitsStored" tell us exactly what we need to know, and we can rewrite the code that reads the image stack.
如您所見,有許多元數據值或屬性 。“Rows”、“Columns” 和 “BitsStored” 的值準確地告訴我們需要瞭解的內容,我們可以重寫讀取圖像堆棧的代碼。

nRows = info.Rows;
nCols = info.Columns;
nPlanes = info.SamplesPerPixel;
nFrames = 20; % The number of files in the directory
X = repmat(int16(0), [nRows, nCols, nPlanes, nFrames]);
for p = 1:nFrames
  fname = sprintf('brain_%03d.dcm', p);
  X(:,:,:,p) = dicomread(fname);
end

We still have to take for granted that the image contains signed data&msdash;a "PixelRepresentation" value of "1" indicates that type of data-but the rest of the information that we need is easily obtained. We can use this metadata along with the imlincomb function in the Image Processing Toolbox to rescale the image data to fill the entire 16-bit dynamic range. The linear combination to rescale the grayscale values is "y = (x - b) * m", where b is the minimum x value and m is a constant ratio derived from the input and output ranges.
我們仍然必須理所當然地認為圖像包含簽名數據&msdash;“PixelRepresentation”值為“1”表示該數據類型——但我們需要的其餘資訊很容易獲得。我們可以將此元數據與 Image Processing Toolbox 中的 imlincomb 函數一起使用,以重新縮放圖像數據以填充整個 16 位動態範圍。重新縮放灰度值的線性組合是“y = (x - b) * m”,其中 b 是最小 x 值,m 是從輸入和輸出範圍得出的常數比率。

% Keep track of the minimum and maximum pixel values.
minPixels = repmat(0, [1, nFrames]);
maxPixels = repmat(0, [1, nFrames]);
for p = 1:nFrames
  fname = sprintf('brain_%03d.dcm', p);
  info = dicominfo(fname);
  minPixels(p) = info.SmallestImagePixelValue;
  maxPixels(p) = info.LargestImagePixelValue;
end
% Rescale image to start at 0.
b = min(minPixels);
m = 2^16/(max(maxPixels) - b);
Y = imlincomb(m, X, -(m * b), 'uint16');

Details about DICOM Metadata
有關 DICOM 元數據的詳細資訊

As shown in the previous example, a typical DICOM file contains numerous attributes. The reason for this is that DICOM messages encapsulate all of the information about a medical imaging procedure, including details about the patient, study, imaging modality, and image series in addition to the image frame stored in the file. Together, all of these attributes comprise an Information Object.
如前面的示例所示,典型的 DICOM 檔包含許多屬性。這樣做的原因是,DICOM 消息封裝了有關醫學成像程式的所有資訊,包括有關患者、研究、成像模式和圖像系列的詳細資訊,以及存儲在檔中的圖像幀。所有這些屬性共同構成一個 Information Object。

The DICOM specification contains numerous Information Object Definitions (IODs), such as MR Image, Ultrasound Multiframe Image, and Radiotherapy Plan. The DICOM files in the examples above contain MR Image Information Objects. dicomread understands most IODs that are listed in the DICOM specification.
DICOM 規範包含許多資訊物件定義 (IOD),例如 MR 圖像、超聲多幀圖像和放射治療計劃。上述範例中的 DICOM 檔包含 MR 影像資訊物件。dicomread 瞭解 DICOM 規範中列出的大多數 IOD。

IODs are defined in terms of smaller functional units, or modules, which correspond to specific real-world objects, such as patients, imaging equipment, etc. The hundred-or-so modules in DICOM describe everything that could be combined to make a DICOM information object. For example, the following table lists the modules that constitute an MR Image IOD:
IOD 是根據較小的功能單元或模組定義的,這些功能單元或模組對應於特定的現實世界物件,例如患者、成像設備等。DICOM 中的大約一百個模組描述了可以組合成 DICOM 資訊物件的所有內容。例如,下表列出了構成 MR Image IOD 的模組:

Modules in the MR Image IOD:
MR Image IOD 中的模組:

Module Description Patient Details about the patient General Study General information about the study, a set of imaging procedures Patient Study (U) Information about the patient at the time of the study General Series General information about one particular imaging procedure Frame of Reference Information to spatially relate images in a series General Equipment General information about the image modality that produced the series General Image Details that identify and describe an image within the series Image Plane Slice thickness/location/spacing and other orientation details Image Pixel The actual image pixels and information on how to interpret them Contrast/Bolus (C) Details about medication given at the time of imaging MR Image Values specific to magnetic resonance imaging Overlay Plane (U) Graphics or bitmapped text to display with a particular image VOI LUT (U) Information to transform the contrast or intensity of the image SOP Common Date/time of message creation and other generic DICOM details
模組描述 患者 關於患者的詳細資訊 一般研究 關於研究的一般資訊,一組成像程式 患者研究 (U) 研究時有關患者的資訊 一般系列 關於一個特定成像程式的一般資訊 參考框架 在空間上關聯系列圖像的資訊 一般設備 關於產生系列的圖像模態的一般資訊 一般圖像 識別和描述系列中的圖像 圖像平面 切片厚度/位置/間距和其他方向細節 圖像圖元 實際圖像圖元和有關如何解釋它們的資訊 對比度/推注 (C) 成像時給出的藥物詳細資訊 MR 圖像 特定於磁共振成像的值 疊加平面 (U) 與特定圖像一起顯示的圖形或點陣圖文本 VOI LUT (U) 轉換圖像對比度或強度的資訊 SOP 通用訊息創建日期/時間和其他通用 DICOM 詳細資訊

Some modules, such as Patient, General Study, General Equipment, and Image Pixel, are found in many IODs. Other modules appear only in specific IODs. The MR Image module, for example, is only part of the MR Image IOD.
一些模組,例如 Patient、General Study、General Equipment 和 Image Pixel,在許多 IOD 中都可以找到。其他模組僅顯示在特定的 IOD 中。例如,MR Image 模組只是 MR Image IOD 的一部分。

The IOD contains modules that describe the patient, study, series, and images for a particular medical imaging procedure. Also, notice that some modules have a "(C)" or "(U)" near their name, which means that for this IOD they are "conditionally required" or "user optional" modules; the conditional and user optional modules may or may not appear in every MR Image IOD. For example, the Contrast/Bolus module will only appear if medication is given at the time of imaging, and the VOI LUT module does not have to be implemented by the device that writes the DICOM file.
IOD 包含描述特定醫學成像程式的患者、研究、系列和圖像的模組。另外,請注意,某些模組的名稱附近有“(C)”或“(U)”,這意味著對於此 IOD,它們是“有條件要求”或“使用者可選”模組;conditional 和 user 可選模組可能會出現也可能不會出現在每個 MR Image IOD 中。例如,只有在成像時給予藥物時,才會出現 Contrast/Bolus 模組,並且 VOI LUT 模組不必由寫入 DICOM 檔的設備實現。

All of the modules and attributes are defined in PS 3.3 of the DICOM specification, which are available online from the National Electrical Manufacturer's Association. Additionally, all of the defined attributes are listed in a data dictionary. Vendors sometimes define private attributes that are specific to their hardware. While you do not need to do anything special to read private attributes with dicomread or dicominfo, if you want to provide additional information about attributes that are not part of the DICOM specification, you can provide your own data dictionary.
所有模組和屬性都在 DICOM 規範的 PS 3.3 中定義,該規範可從美國國家電氣製造商協會在線獲取。此外,所有定義的屬性都列在數據字典中。供應商有時會定義特定於其硬體的私有屬性。雖然不需要執行任何特殊作即可使用 dicomreaddicominfo 讀取私有屬性,但如果要提供有關不屬於 DICOM 規範的屬性的其他資訊,則可以提供自己的數據字典。

Writing DICOM Files  編寫 DICOM 檔

We will now create a new DICOM file. Writing a new file requires an image and metadata. We created a new image with the scaled data in one of the examples above, and dicomwrite and dicominfo will help us create the metadata we need.
現在,我們將創建一個新的 DICOM 檔。編寫新檔需要圖像和元數據。在上面的示例之一中,我們使用縮放后的數據創建了一個新圖像,dicomwritedicominfo 將幫助我們創建所需的元數據。

To write a DICOM file, simply use dicomwrite in the Image Processing Toolbox. Currently, this function supports creating Secondary Capture (SC) Image IODs, which contain all of the pixel data and metadata necessary for correctly interpreting the image. Essentially, an SC image is similar to what you would generate with a framegrabber, or if you had generated the image yourself from scratch or from another image (as in the example above). Metadata about the patient, image, and study will be present, but information about the original hardware that produced the image will not.
要編寫 DICOM 檔,只需使用 Image Processing Toolbox 中的 dicomwrite 即可。目前,此功能支援創建二次捕獲 (SC) 圖像 IOD,其中包含正確解釋圖像所需的所有像素數據和元數據。從本質上講,SC 圖像類似於您使用圖像採集卡生成的圖像,或者如果您自己從頭開始或從其他圖像生成圖像(如上例所示)。將提供有關患者、圖像和研究的元數據,但不會顯示有關生成圖像的原始硬體的資訊。

To write the tenth frame of the scaled image using default metadata to a new file, use dicomwrite with its basic syntax:
要使用預設元數據將縮放圖像的第十幀寫入新檔,請使用 dicomwrite 及其基本語法:

frame = Y(:,:,:,10);
filename = 'brain_scaled_10.dcm';
dicomwrite(frame, filename)

In this example, dicomwrite infers the metadata that is required, filling in blank values where appropriate. To override default values, provide dicomwrite with a structure containing attributes:
在此示例中,dicomwrite 會推斷所需的元數據,並在適當的地方填充空白值。要覆蓋預設值,請為 dicomwrite 提供包含屬性的結構:

info = dicominfo('brain_001.dcm');
dicomwrite(frame, filename, info)

dicomwrite picks the values that apply to the Secondary Capture IOD and ignores the rest. When dicomwrite makes a new file, it is actually performing two operations. First, it creates a Secondary Capture information object. Then it encodes the information object's attributes, turning them into a stream of bytes that are written to the output file. This last step demonstrates the DICOM concept of services.
dicomwrite 選擇適用於 Secondary Capture IOD 的值,並忽略其餘值。當 dicomwrite 創建新檔時,它實際上是在執行兩個作。首先,它會創建一個 Secondary Capture 信息物件。然後,它對資訊對象的屬性進行編碼,將它們轉換為寫入輸出檔的位元組流。最後一步演示了 DICOM 的服務概念。

A service provides a set of concrete actions to perform on objects. Common services include Storage, Print Management, Verification, and Query/Retrieve. When dealing with network messages, services are implemented by adding extra attributes to the message and then sending a series of instructions to the receiving machine or application, which deciphers the extra attributes to figure out what to do with the message. When working with DICOM files, the attributes are added to the file, and writing or reading the file replaces the transfer of instructions.
服務提供了一組要對物件執行的具體作。常見服務包括 Storage、Print Management、Verification 和 Query/Retrieve。在處理網路消息時,服務是通過向消息添加額外屬性,然後向接收機器或應用程式發送一系列指令來實現的,接收機器或應用程式解密額外的屬性以弄清楚如何處理消息。使用 DICOM 檔時,屬性將添加到檔案中,並且寫入或讀取檔案將替換指令的傳輸。

Writing a file containing DICOM data is very much like performing the Storage service. Essentially, the IOD's attributes plus those for the Storage service are combined and "frozen" when written to disk. Reading the file "thaws" the IOD and service attributes, and the entire context of what to do with the information is preserved.
寫入包含 DICOM 資料的檔與執行 Storage 服務非常相似。實質上,IOD 的屬性加上 Storage 服務的屬性在寫入磁碟時被合併並 「凍結」。讀取檔會“解凍”IOD 和服務屬性,並保留如何處理信息的整個上下文。

Encoding rules found in the DICOM standard provide the algorithm for transforming attributes into a series of bytes to be transmitted or stored. Different byte-ordering schemes can be used during encoding, and image pixels can be compressed using numerous techniques, such as JPEG or run-length encoding. In the calls to dicomwrite above, the files are encoded using so-called "implicit VR, little endian" style, and the pixels are not compressed.
DICOM 標準中的編碼規則提供了將屬性轉換為一系列位元組以傳輸或儲存的演算法。在編碼過程中可以使用不同的位元組排序方案,並且可以使用多種技術(如 JPEG 或遊程編碼)壓縮圖像圖元。在上面對 dicomwrite 的調用中,檔使用所謂的“隱式 VR,小端”樣式進行編碼,並且圖元未壓縮。

Working Together  攜手合作

A host of Transfer Syntaxes unambiguously define how to decode a DICOM message. One of these transfer syntaxes appears at the beginning of a DICOM network transmission or at the start of a DICOM file. If both the sender/writer and receiver/reader support the same transfer syntax, communication proceeds. Otherwise, communication fails. The DICOM features in MATLAB and the Image Processing Toolbox support the most commonly used transfer syntaxes.
許多 Transfer Syntaxes 明確定義了如何解碼 DICOM 消息。這些傳輸語法之一出現在 DICOM 網路傳輸的開頭或 DICOM 檔的開頭。如果發送方/寫入方和接收方/讀取器都支援相同的傳輸語法,則通信將繼續進行。否則,通信將失敗。MATLAB 和 Image Processing Toolbox 中的 DICOM 功能支援最常用的傳輸語法。

With all of these possibilities-IODs, optional modules, services, and transfer syntaxes-it can be difficult to know exactly how well two modalities will interact. Implementing a service requires knowing which attributes to send or receive. Furthermore, making the service useful frequently requires knowing what kind of data to put into attributes and verifying that the information is correct. Consequently, few manufacturers support the full set of "Service-Object Pairs" that are possible in DICOM. In addition, different applications support different transfer syntaxes.
由於所有這些可能性 - IOD、可選模組、服務和傳輸語法 - 可能很難確切地知道兩種模式的交互效果如何。實現服務需要知道要發送或接收哪些屬性。此外,要使服務經常有用,需要知道將哪種數據類型放入 attributes 中,並驗證資訊是否正確。因此,很少有製造商支援 DICOM 中可能的完整「服務物件對」集。此外,不同的應用程式支援不同的傳輸語法。

Fortunately, every application that implements part of DICOM should know what it supports. Manufacturers publish the information about the services, IODs, and transfer syntaxes that they support in DICOM conformance statements. By comparing statements from two different vendors, you can identify whether the two applications can share information.
幸運的是,每個實現 DICOM 部分的應用程式都應該知道它支持什麼。製造商在 DICOM 一致性聲明中發佈有關他們支持的服務、IOD 和傳輸語法的資訊。通過比較來自兩個不同供應商的語句,您可以確定兩個應用程式是否可以共享資訊。

Limitless Possibilities with DICOM
DICOM 的無限可能

DICOM makes it easy to acquire and put medical images into a file. It is becoming more prevalent in the medical field.
DICOM 可以輕鬆獲取醫學影像並將其放入檔中。它在醫療領域越來越普遍。

Many medical device manufacturers and software manufacturers are adding DICOM to just about any product that can work with medical images and data.
許多醫療設備製造商和軟體製造商正在將 DICOM 添加到幾乎任何可以處理醫學圖像和數據的產品中。

Using MATLAB with the Image Processing Toolbox provides easy access to medical images, modality metadata, and patient information within DICOM files. They also provide numerical and image processing algorithms, GUI-building tools, and visualization techniques. With MATLAB, the Image Processing Toolbox, and DICOM it is possible to quickly view medical images, design and test new modalities, and create GUI-driven medical image analysis systems.
將 MATLAB 與 Image Processing Toolbox 結合使用,可以輕鬆存取 DICOM 檔中的醫學圖像、模態元數據和患者資訊。它們還提供數值和圖像處理演算法、GUI 構建工具和可視化技術。借助 MATLAB、Image Processing Toolbox 和 DICOM,可以快速查看醫學影像、設計和測試新模態,並創建 GUI 驅動的醫學影像分析系統。

Published 2002   2002 年出版

View Articles for Related Industries
查看相關行業的文章