SoFunction
Updated on 2024-11-12

Calculate the true distance between two points of a DICOM image using Python implementation

Comparison of measurement results

DICOM Reader (Siu Sai Look) Measurement Results

python measurements

coding

import numpy as np
import cv2
import math
import pydicom
from pydicom.pixel_data_handlers.util import convert_color_space

ds = ("./your_dicom_path.dcm")
# The first few charts, dtype defined by Bits Allocated
img = (ds.pixel_array[0], dtype='uint8')
# Getting the color space
color_space = (0x00280004).value
# Color space conversion
img = convert_color_space(img, color_space, 'RGB')
# BGR to RGB
img = (img, cv2.COLOR_BGR2RGB)

# Pixels and real space scales
autio_x = (0x00280030).value[0]
autio_y = (0x00280030).value[1]

p1 = None
p2 = None

# European stock theorem
def cal_euler(p1, p2):
    a2 = ((p2[1] - p1[1]) * autio_x) ** 2
    b2 = ((p2[0] - p1[0]) * autio_y) ** 2
    res = (a2 + b2)
    return res

# Mouse events
def mouseHandler(event, x, y, flags, param):
    global p1, p2
    if event == cv2.EVENT_LBUTTONDOWN:
        point = (x, y)
        if p1 == None:
            p1 = point
        else:
            p2 = point
        print(point)
        ()

('p1')
('p1', mouseHandler)
('p1', img)
()

('p2')
('p2', mouseHandler)
('p2', img)
()

res = cal_euler(p1, p2)

img = (img, p1, p2, (0, 0, 255), 1, 1)
(img, str(round(res, 2)) + "mm", p2, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 1, cv2.LINE_AA)
('res', img)
()

To this point this article on the use of Python to calculate the real distance between two points of the DICOM image is introduced to this article, more related to Python to calculate the distance between two points of the image content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!