将12位DICOM图像转换为8位jpeg [英] converting 12 bit DICOM image to 8 bit jpeg
问题描述
我正在尝试使用dicom库将DICOM文件加载到python中。我已经完成了以下操作
I am trying to load DICOM files into python using the dicom library. I have done the following
ds=dicom.read_file(r"C:\Users\Z003SPFR.AD005\ML\GLCM AND SVM\data\NECT\1.IMA")
# # store the raw image data
DicomImage = ds.pixel_array
这使我看到的值似乎是12位,因为获得的最大值约为3047,最小值为0。然后,我将自己的映射函数设置为将其设置为0-255。我使用了以下代码:
This gives me values that appear to be 12 bit, since the highest value obtained was around 3047 and lowest value was 0. Then i made my own mapping function to bring it to the range 0-255. I used the following code:
leftMin = 0
leftMax = np.amax(DicomImage)
rightMin = 0
rightMax = 255
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
#print(translate(value, leftMin, leftMax, rightMin, rightMax))
def int12_to_int8(img):
img_array = []
for eachRow in img:
for eachPix in eachRow:
img_array.append(translate(eachPix,leftMin, leftMax, rightMin, rightMax))
img_array = np.array(img_array)
img_array = img_array.reshape(512,512)
return img_array
correct_range_image = int12_to_int8(DicomImage)
完成此操作后,我意识到数组img_array的类型为uint16。我希望将其作为uint8,所以我使用以下行将其转换为uint8:
After doing this I realized that the array img_array was of type uint16. I wanted it as uint8, so i used the following line to convert to uint8:
cvuint8 = cv2.convertScaleAbs(correct_range_image)
然后我显示了生成的图像。但是我收到的图像不能很好地代表原始图像。我已经张贴了原始图像和转换后图像的图片。如何更好地进行转换,以便更好地表现原始图像?我以前显示的代码在这里:
Then I displayed the resulting image. But i received an image that didn't represent the original image very well. I have posted pictures of the original image and the converted image. How can I make the conversion better so that I get a better representation of the original image?? Code I used to display is here :
cv2.imwrite('1.jpeg', cvuint8 )
cv2.imshow('image',cvuint8 )[enter image description here][1]
cv2.waitKey(0)
图像
[1]转换后的图像: https://i.stack.imgur.com/wdGCo.jpg
[2]原始图片: https://i.stack.imgur.com/JyfYI.jpg
[1] Converted Image: https://i.stack.imgur.com/wdGCo.jpg [2] Original Image: https://i.stack.imgur.com/JyfYI.jpg
推荐答案
我想出了解决问题的方法。正如艾哈迈德·艾哈迈德(Ahmed)前面提到的那样,DICOM围绕重新缩放斜率,截距和窗口水平/宽度进行适当显示。经过大量的文档之后,这是使用OpenCV,numpy和pydicom库在Python中渲染DICOM的方法,这些库使所有工作变得简单
I figured out the solution for my problem.As mentioned above by Ahmed, DICOM plays around rescale slope, intercept and window level/width for proper display. After going through lot of documents, here is the way to render DICOM in Python using OpenCV,numpy and pydicom libraries which make all work easy
代码:
1读取图像
Code: 1.Read the image
ds=dicom.read_file("image_path")
# store the raw image data
img = ds.pixel_array
-
使用重新缩放坡度并截取来自图像标题的信息并将其转换。
Use rescale slope and intercept information from the image header and translate it.
rescale_slope = 1
rescale_intercept = -1024
rescale_slope=1 rescale_intercept=-1024
def translate(value,rescale_slope,rescale_intercept):
return (value*rescale_slope)+rescale_intercept
def int12_to_int8(DicomImage):
img_array = []
for eachRow in DicomImage:
for eachPix in eachRow:
img_array.append(translate(eachPix,rescale_slope,rescale_intercept))
img_array = np.array(img_array)
img_array = img_array.reshape(512,512)
return img_array
img_1 = int12_to_int8(img)
3。使用窗口级别
3.Use window level and width information from the image header to display in the proper range.
def get_LUT_value(data, window, level)
return np.piecewise(data,
[data <= (level - 0.5 - (window-1)/2),
data > (level - 0.5 + (window-1)/2)],
[0, 255, lambda data: ((data - (level - 0.5))/(window-1) + 0.5)*(255-0)])
level=200
window=800
scaled_img=get_LUT_value(img, window, level)
4。最后,按需要添加最终图像
4.Finally, with final image as wanted
scaled_img = cv2.convertScaleAbs(scaled_img)
cv2.imshow('image',scaled_img)
cv2.imwrite("hem.jpg",scaled_img)
cv2.waitKey(0)
这篇关于将12位DICOM图像转换为8位jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!