pretty code

顯示具有 DICOM 標籤的文章。 顯示所有文章
顯示具有 DICOM 標籤的文章。 顯示所有文章

2021年5月5日 星期三

吃自己的狗食

身體漸漸的好轉了,雖然這兩天左臂又多了奇怪的神經抽痛,但應該跟原本的拉傷無關吧?

EasyDump 是我10 幾年前在 DICOM 相關產業寫的小程式,全部只使用標準 C 語言,未用到任何的第三方套件,記得程式碼應該不超過 1,000 行吧?

使用自己的程式開啟自己的 DICOM DIR,感覺有點奇妙!

2019年10月31日 星期四

數億張醫療影像在網路上曝光

德國漏洞分析暨管理公司Greenbone Networks在本周公布一項研究報告,指出有大量的醫療影像儲存系統(Picture Archiving and Communication System,PACS)配置不當,而讓數億張的醫療影像在網路上曝光。

今天偶然看到這篇新聞,感覺裡面的名詞好熟悉,還好不是我前前前公司有問題。

PACS 系統主要的功能是用來儲存數位醫療影像,故可能有 2 種應用:
1. 提供醫療儀器將做好的檢查影像上傳儲存的功能。
2. 提供後續醫生觀看影像或打報告用。

DICOM 格式只是一種類似樹狀結構的 Binary 資料,故以 C 語言來說,只要定好 structure 便可以很容易的 parsing。以前練習寫過 Dump Tool,沒記錯的話,不到 1000 行就可以搞定。


至於 DICOMDIR 就是一連串 DICOM 的索引,可能包括某科室歷次檢查的影像或是你在這家醫院全部做過的檢查。

不過如果是 Viewer 的應用就比較困難,自己來寫會是個大工程。

有 Open Source 可以參考的是 vtk-dicom。

http://dgobbi.github.io/vtk-dicom/

2009年8月15日 星期六

VR GetValue

if(sVR == "AE" || sVR == "CS" || sVR == "LO" ||
   sVR == "LT" || sVR == "PN" || sVR == "SH" ||
   sVR == "ST" || sVR == "UI")
{
  InsertString(iTag, sValue);
}
else if(sVR == "IS" || sVR == "SL" || sVR == "UL")
{
  InsertLong(iTag ,StrToInt(sValue));
}
else if(sVR == "DA")
{
  InsertDate(iTag, sValue);
}
else if(sVR == "TM")
{
  InsertTime(iTag, sValue);
}
else if(sVR == "AS")
{
  InsertAge(iTag, StrToFloat(sValue));
}
else if(sVR == "DS")
{
  InsertDouble(iTag, StrToFloat(sValue));
}
else if(sVR == "SS" || sVR == "US")
{
  InsertShort(iTag, StrToFloat(sValue));
}
 

國內醫療資訊系統相關廠商代表

* 台灣英飛特股份有限公司。
* 奇菱科技股份有限公司。
* 台灣電腦服務股份有限公司。
* 鉅仁科技股份有限公司。
* 台灣愛克發吉華股份有限公司。
* 台灣富士通股份有限公司。
* 奇異亞洲醫療設備股份有限公司。
* 商之器科技股份有限公司。

HIS

醫療院所的核心系統-醫療資訊系統(HIS)

劉麗惠/台北〈企業IT〉

相較於其他行業,醫院資訊系統的獨特性自成一格,醫院所採用的資訊系統,並非一般行業使用的企業資源規劃(Enterprise Resource Planning;ERP)、客戶關係管理(Customer Relationship Management;CRM),而是所謂的醫療資訊系統(Healthcare Information System;HIS)。


然而,HIS包含了哪些層面的應用系統,根據行政院衛生署資訊中心所公告的醫療資訊系統規範2.0,將HIS區分為3大部分,分別為「醫療相關資訊系統」、「行政管理相關資訊系統」及「基礎建設與醫院相關系統」。

醫療相關資訊系統旗下子系統包括「門診系統」、「入院出院轉診系統」、「醫囑系統」、「護理系統」、「檢驗系統」、「影像與放射資訊系統」、「藥品與衛材系統」、「手術與麻醉系統」、「重症照護系統」、「營養系統」以及「電子病理管理系統」。

行政管理相關資訊系統下面的子系統則有「健康保險系統」、「財務與行政管理系統」、「決策支援系統」、「醫院顧問關係管理系統」及「高階主管資訊系統」。

基礎建設與醫院相關系統則區分為「訊息、編碼與軟體開發標準」、「資訊安全」、「病人安全系統」、「公共衛生通報與監測系統」、「社區醫療資訊系統」、「長期照護系統」、「緊急醫療危機應變系統」、「行動無線運算平台資訊系統」以及「參考資訊架構」。

Private Tag

4109,0010=XX識別字串 (VR = LO)
0010代表我們用到的後面四碼(element number)都是10開頭

Private Creator Data Elements numbered (gggg,0010-00FF) (gggg is odd) shall be used to reserve a block of Elements with Group Number gggg for use by an individual implementor.

The implementor shall insert an identification code in the first unused (unassigned) Element in this series to reserve a block of Private Elements. The VR of the private identification code shall be LO (Long String) and the VM shall be equal to 1.


4109,1002=資料1
4109,1004=資料2
4109,1006=資料3
4109,1008=資料4
4109,100A=資料5
4109,100C=資料6
4109,100E=資料7
4109,1010=資料8
4109,1012=資料9
4109,1014=資料10
4109,1016=資料11
4109,1018=資料12
4109,101A=資料13
4109,101C=資料14

Private Data Elements have an odd Group Number that is not (0001,eeee),(0003,eeee), (0005,eeee), (0007,eeee), or (FFFF, eeee). Private Data Elements are discussed further in Section 7.8 (PS 3.5 of the DICOM Standard).

Transfer Syntax

Implicit VR Little Endian 1.2.840.10008.1.2
explicit VR Little Endian 1.2.840.10008.1.2.1
explicit VR Big Endian 1.2.840.10008.1.2.2

DICOM FILE META INFORMATION

1. 128 byte File Preamble(0x00, or Application defined)
2. 4 byte DICOM prefix("DICM")
3. DICOM Meta Elements with Tags and Lengths(0002 Group)

The 0002 Group shall be encoded using the explicit VR Little Endian Transfer
Syntax(UID=1.2.840.10008.1.2.1)

Image Plane Module Attributes(影像平面屬性)

Pixel Spacing(0028,0030)
記錄兩個十進位的值,標示影像每一個圖素中心之間的物理距離,
第一個數值為一列中相鄰圖素中心之距離,第二個數值為一行中相
鄰圖素中心之距離,由此可得影像在真實空間(單位為公釐)的大小。

Image Orientation(0020,0037)
記錄相對於病人的第一列及第一行的方向餘旋。

Image Position(0020,0032)
錄影像左上角的空間影像位置(單位為公釐),空間方向是依病人方向定義的
(x 軸向左為正,y 軸向後為正,z 軸向上為正)。

Slice Thickness(0018,0050)
記錄影像厚度(單位為公釐)。

Image Pixel Module(影像圖素模組)

Sample Per Pixel(0028,0002)
代表每一個像素的Byte,RGB=3、CMYK=4、Gray=1。

Photometric Interpretation(0028,0004)
圖像的色彩空間:
MONOCHROME1 - 灰階,像素最小值顯示為白色。
MONOCHROME2 - 灰階,像素最小值顯示為黑色。
RGB - RGB彩色圖案。
HSV - HSV彩色圖案。
PALLETE COLOR - 使用調色盤的彩色圖案。
ARGB
CMYK
YBR-FULL

Planar Configuration(0028,0006)
彩色圖案的像素數據排列順序:
0000 - R1、G1、B1、R2、G2、B2。
0001 - R1、R2、G1、G2、B1、B2。

Number of Frames(0028,0008)
Multi-Frame的影像的Frmae數。

Rows(0028,0010)
圖案的高,以像素為單位。

Columns(0028,0011)
圖案的寬,以像素為單位。

Bits Allocated(0028,0100)
The size of the Pixel Cell。

Bits Stored(0028,0101)
Total number of these allocated bits that will be used to represent a Pixel Sample Value, shall never be larger than Bits Allocated。

High Bit(0028,0102)
Where the high order bit of the Bits Stored is to be placed with respect to the Bits Allocated。

Pixel Representation(0028,0103)
0表示圖素值為正整數,1表示圖素值為補數。

以上四個屬性描述了像素單元的存儲結構。


Pixel Data(7FE0,0010)
像素數據。

Retired Data Elements

PS 3.6 標示 RET的 Tag就是Retired Data Elements.

SQ and Items

Item (FFFE,E000)
Item Delimitation Item (FFFE,E00D)
Sequence Delimitation Item (FFFE,E0DD)

以上3個都沒有VR(Implicit VR),不受Transfer Syntax規範。
除了Group Number and Element Number,
如果是little endian,則Item會等於(FEFF,00E0).

明確長度(以Big Endian為例)
(FFFE,E000) (0x00000004) Value Length 直接標明後面Value的實際長度。
沒有Value,Length就是(0x00000000).

不定長度(以Big Endian為例)
(FFFE,E000) (0xFFFFFFFF) Value Length = 0xFFFFFFFF。
結尾需有Item Delimitation Item.
(FFFE,E00D) (0x00000000) Value Length = 0x00000000。(標示結尾用,故不會有Value)

SQ跟Item很像
有明確長度Value Length就標示長度
不定長度結尾需有Sequence Delimitation Item.
其Value Length = 0x00000000。(標示結尾用,故不會有Value)

Attribute's Data Element Type

mandatory 強制的。
conditional 有條件的。
user option 選擇性。

1 必備,不允許空值即長度不能為0。
1C 符合條件一定要有。
2 必備,允許空值,所以長度可以為0。
2C 符合條件一定要有。
3 選擇性。

Byte ordering

VRs constructed of a strign of characters of 8-bit single byte codes are really constructed of a string of individual bytes, and are therefore not affected by byte ordering.

The VRs that are not a string of characters and consist of multiple bytes are:

2-byte US,SS,OW adn each component of AT
4-byte OF,UL,SL and FL
8-byte FD

VR Length、Value Length

explicit VR

OB、OW、OF、SQ、UN、UT,VR為4個byte,後2個byte為0x0000,Value Length為4個byte.其他VR為2個byte,Value Length也為2個Byte.

implicit VR

無VR,Value Length為4個byte.

故簡單來說有VR,VR幾個byte,Value Length便幾個byte. 無VR一律4個byte.

補充:
explicit VR下,OB、OW、OF、SQ、UN允許不定長度,但需要用Sequence Delimitation(FFFF,E0DD)來判斷結尾,此時Value Length為0xFFFFFFFF。

implicit VR下,可以允許不定長度,規則同上。

Data Elements

xxxx,yyyy

xxxx Group Number
yyyy Element Number

Standard Data Elements have an even Group Number that is not (0000,eeee), (0002,eeee), (0004,eeee), or (0006,eeee).

Note: Usage of these groups is reserved for DIMSE Commands(see PS 3.7 of the DICOM Standard) and DICOM File Formats.

Private Data Elements have an odd Group Number that is not (0001,eeee), (0003,eeee), (0005,eeee), (0007,eeee), or (FFFF, eeee). Private Data Elements are discussed further in Section 7.8 (PS 3.5 of the DICOM Standard).

VR Padding

Values with a VR of UI shall be padded with a single trailing NULL (00H) character when necessary to achieve even length. Values with a VR of OB shall be padded with a single trailing NULL byte value (00H) when necessary to achieve even length.

(Dicom Tag的值都必須是偶數,所以除了UI及OB的VR外,其他都補空白0x20)

12-Lead ECG IOD Modules(PS 3.3-2007 Page 172 ~ 172、812 ~ 818)

SOP class UID
1.2.840.10008.5.1.4.1.1.9.1.1

Modality(0008,0060)
Shall be ECG.

Waveform Sequence(5400,0100)
Items shall be between 1 and 5, inclusive.
(最多5個Item)

Number of Waveform Channels(003A,0005)
In each Waveform Sequence Item shall be between 1 and 13, inclusive.

The total number of channels encoded across all Items shall not exceed 13.
(如果有5個Item,5個Item加起來的Channel不能超過13個)

Number of Waveform Samples(003A,0010)
In each Waveform Sequence Item Shall be less than or equal to 16384.
(1個Channel的Samples最多有16384個)

Sampling Frequency(003A,001A)
In each Waveform Sequence Item shall be between 200 and 1000, inclusive.
(Frequency in Hz,意即每秒幾個取樣值)

16384 / 1000 = 16.384 即一個Item的呈現最多不會超過 16.384 Sec.
16384 / 200 = 81.920 即一個Item的呈現最多不會超過 81.920 Sec.

this allows over 16 seconds per channel at the maximum sampling frequency;
if longer recordings are required, the General ECG IOD may be used.

Waveform Sample Interpretation(5400,1006)
In each Waveform Sequence Item shall be SS(signed short)

Waveform Bits Allocated(5400,1004)
Each Sample's Bits
(需配合54001006來看)

Channel Definition Sequence(003A,0200)
003A0005有幾個Channel,此Sequence就會有幾個Item.

Channel Sensitivity(003A,0210)
Nominal numeric value of unit quantity of sample.

Channel Sensitivity Correction Factor(003A,0212)
Multiplier to be applied to encoded sample values to match units
specified in Channel Sensitivity(003A,0210). Required if Channel
Sensitivity is present.

Something about Dicom Tags

#Study ID(該次檢查的編號)
00200010=001
(請統一填001即可)

#Study UID(該次檢查的唯一識別ID)
0020000d=1.2.840.xxxxxx(機構代號)...

#Series Number(該組影像的編號)
00200011=1
(該次檢查有好幾組的話, 請從1開始編流水號即可)

#Series UID(該組影像的唯一識別ID)
0020000e=1.2.840.xxxxxx(機構代號)...

#Instance Number(影像編號)
00200013=1
(該組有好幾張影像的的話, 請從1開始編流水號即可)

#Instance UID
00080018=1.2.840.xxxxxx(機構代號)...

Ex:
Study UID = 1.2.840.xxxxxx(機構代號).部門編號.產品編號.儀器編號.時間
Series UID = Study UID + Series Number
Instance UID = Series UID + Instance Number

只要UID不會衝突即可...

相關資料:DICOM Fundamentals.ppt P73 ~ P75
申請機構代號:
http://www.dclunie.com/medical-image-faq/html/part8.html#UIDRegistration

#Study Date(檢查日期)
00080020=03/28/2007

#Series Date(該組影像產生日期)
00080021=03/28/2007

#Image Date(影像日期)
00080023=03/28/2007

#Study Time(檢查時間)
00080030=09:44:30.703

#Series Time(該組影像產生時間)
00080031=09:44:30.703

#Image Time(影像時間)
00080033=09:44:30.703

Attribute Matching(PS 3.4-2007 Page 41)

The following types of matching may be performed on Key Attributes
in the Query/Retrieve Service Class:

— Single Value Matching
— List of UID Matching
— Universal Matching
— Wild Card Matching
— Range Matching
— Sequence Matching

Matching requires special characters ( i.e. “*”,”?”,”-”, and “\”)
character repertoire for the VR of the Key Attributes.
Notes: 1.For example, the “-“ character is not valid for the DA, DT
and TM Vrs but is used for range matching.

The total length of the Key Attribute may exceed the length as
specified in the VR in PS 3.5. The Value Multiplicity (VM) may
be larger than the VM specified in PS 3.6 for the Key Attribute,
as defined for particular Matching Type.

難怪CFind or WorkList查詢時,明明是DA的VR,卻填上"20070101-20070103",又不受VR的
長度限制,這Dicom果然還真複雜。

補充一下Range Matching:

In the absence of extended negotiation, if the Attribute is a date, then:
a) A string of the form “ - ”, where is less or
equal to , shall match all occurrences of dates which fall
between and inclusive.
b) A string of the form “- ” shall match all occurrences of
dates prior to and including .
c) A string of the form “ -“ shall match all occurrences of
and subsequent dates.

In the absence of extended negotiation, if the Attribute is a time, then:
a) A string of the form “ - ”, where is less or
equal to , shall match all occurrences of times which fall
between and inclusive.
b) A string of the form “- ” shall match all occurrences of
times prior to and including .
c) A string of the form “ -“ shall match all occurrences of
and subsequent times.

If the Attribute is a datetime, then:
a) A string of the form “ - ”, where
is less or equal to , shall match all moments in time
which fall between and inclusive.
b) A string of the form “- ” shall match all moments in
time prior to and including .
c) A string of the form “ -“ shall match all moments in
time subsequent to and including .
d) The offset from Universal Coordinated Time, if present in the
Value of the Attribute, shall be taken into account for the
purposes of the match.

If extended negotiation of combined datetime matching is successful,
then a pair of Attributes that are a date and a time, both of which
specify the same form of range matching, shall have the concatenated
string values of each range matching component matched as if they were
a single datetime Attribute.

Note: For example, a Study Date of “20060705-20060707” and a Study Time
of “1000-1800” will match the time period of July 5, 10am until July 7,
6pm, rather than the three time periods of 10am until 6pm on each of
July 5, July 6 and July 7, as would be the case without extended
negotiation.

Range matching is not defined for types of Attributes other than
dates and times.