Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting ROI pixel Value from a directory #622

Closed
nature01 opened this issue Apr 17, 2018 · 33 comments
Closed

Extracting ROI pixel Value from a directory #622

nature01 opened this issue Apr 17, 2018 · 33 comments
Labels

Comments

@nature01
Copy link

Hi Everyone,

i have a directory with 189 DICOM data and i want to extract all the pixel Value in a specific ROI from all the DICOM Images , i used to do this for one image and that´s working.
i tried to open the directory and to switch it into numpy Array but i couldnt have any results.

This how looks my first try:

import os
import pydicom
import matplotlib.pyplot as plt
import numpy as np

dirname = 'C:\Python27\DICOM\ST000000\SE000013/'
files = os.listdir(dirname)
ds_list = [pydicom.dcmread(os.path.join(dirname, filename) ) for filename in files]

Thanks !

@mrbean-bremen
Copy link
Member

So you should get a list of datasets here. What do you get instead?

@nature01
Copy link
Author

i get an Error :(InvalidDicomError: File is missing DICOM File Meta Information header or the 'DICM' prefix is missing from the header. Use force=True to force reading.)

i tried to use force=True but it does not work either .

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Apr 17, 2018

Well, looks like you have a non-DICOM file in your folder. Also, you did not filter out subdirectories (just in case there exist some).
Just print the filename to see where you get the error.

@nature01
Copy link
Author

i Printed the files so i get all the files name in the directory and there is no subdirectories in the Folder .
I will try to get the data from the Klinik as DICOM files because there is no .dcm at the end like you already mentioned .

So if i get the right files , can i read and extract the Pixel value(4 Positions) from all the files or must i do it manually ?

Thanks a lot for your Help :)

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Apr 17, 2018

The ending of the file name does not matter. Just try to check where the problem occurs:

for filename in os.listdir(dirname):
    try:
        pydicom.dcmread(os.path.join(dirname, filename))
    except InvalidDicomError:
        print(filename)

@nature01
Copy link
Author

screenshot_code

i get this (NameError: name 'InvalidDicomError' is not defined) , i don't know if i did it right i am just a beginner

@mrbean-bremen
Copy link
Member

Ok, sorry, you have to import that:

from pydicom.errors import InvalidDicomError

@nature01
Copy link
Author

Ah okay, i get now no Error and the list of all the files !

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Apr 17, 2018

Hm, that means that you can't read these files at all - you said that you tried this with force=True?

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Apr 17, 2018

You wrote that you succeeded with one file - how did you do that? Or was that another file?

@nature01
Copy link
Author

screen2
i can get now all the files names , i tried it befor with force=true but that has not worked .
i succeded with one file to get all the Pixel value for my 4 Positions :

import numpy as np
import matplotlib.pyplot as plt
import pydicom

ds1 = pydicom.dcmread('C:\Python27\DICOM\ST000000\SE000013\MR000189')
b=ds1.pixel_array

dx, dy = ds1.PixelSpacing
dx, dy = float(dx), float(dy)
b
Sonde1=d[61,69]
print Sonde1
Sonde2=d[69,69]
print Sonde2
Sonde3=d[72,69]
print Sonde3
Sonde4=d[75,70]
print Sonde4
Out:
2003
1669
1790
1459

@mrbean-bremen
Copy link
Member

I just noticed that:
dirname = 'C:\Python27\DICOM\ST000000\SE000013/'
This usually should not work because the backslashes are seen as escape characters - you need to use r'C:\Python27\DICOM...' instead. What kind of Python do you use?

@nature01
Copy link
Author

i am using Python 2.7

@mrbean-bremen
Copy link
Member

The usual CPython? I'm just curious how that works at all with the backslashes... Or maybe your environment converts it? Anyway, to be completely sure, use print(os.path.join(dirname, filename)) to be sure you get the same as with the single file.

@mrbean-bremen
Copy link
Member

Also, you tried it with the last file, is it the same with the first one? E.g.
ds1 = pydicom.dcmread(r'C:\Python27\DICOM\ST000000\SE000013\MR000000')

@nature01
Copy link
Author

So i used print(os.path.join(dirname, filename)) and i get C:\Python27\DICOM\ST000000\SE000013\MR000193

in the case of the one file ,it is not a problem wich one is choosen, the first one will be also okay

@mrbean-bremen
Copy link
Member

That is strange. You mean you get the exception for each file if call dcmread in a loop, and you don't get the exception if you do the same call outside the loop?
Also, where comes MR000193 from? You said you have 189 files there?
As a side note - it is not a good idea to use your Python installation directory as a data directory for DICOM files - this can mess up your Python installation if you are not careful.

@nature01
Copy link
Author

i am really sorry i have in fact 193 files .
if i use dsmread for one file , i can switch as numpy array and plot it without Problem ,the Problem is to get the information from all the files.
sorry again
screen3
screen4

@mrbean-bremen
Copy link
Member

Hm, I don't see the list of the filenames in the output of [7] - I guess you just omitted them?

@mrbean-bremen
Copy link
Member

If not, that means that it works...

@nature01
Copy link
Author

yes it works

@mrbean-bremen
Copy link
Member

So, what changed? You got it to work?

@nature01
Copy link
Author

i can see all the filenames now with print filename.

Do you have an idea how can i go further ?

Thank you!

@mrbean-bremen
Copy link
Member

I admit I'm at a loss here - I don't understand the difference between the two calls (e.g. in the loop and outside). Also, I still don't understand why your paths work with backslashes, but maybe that's an IPython thing - I haven't used it.

@mrbean-bremen
Copy link
Member

To be completely sure:

for filename in os.listdir(dirname):
    filepath = os.path.join(dirname, filename)
    print 'Reading', filepath        
    try:
        pydicom.dcmread(filepath)
        print 'Succeeded'
    except InvalidDicomError:
        print('Failed')

gives you 'Failed' for all files, right?

@nature01
Copy link
Author

i have the IPython , without the backslashes nothing works.

So like you said i moved the DICOM directory from the Python installation directory , and now it works for having filesname list without the InvalidDicomError thing , hmm so weird !

@mrbean-bremen
Copy link
Member

So it does work now correctly?

@nature01
Copy link
Author

yes now it works ! i have a list of all the files , that´s mean that the files are ready to be switched into Numpy Array?
screen5

@mrbean-bremen
Copy link
Member

Yes - now your original code that uses the list comprehension should work (though it may be better to use a generator expression instead to avoid getting all datasets in memory at once).

@nature01
Copy link
Author

okay, thanks a lot .
i dont have a clear idea how to use a generator expression , do you have an example that i may follow , because after the step of reading the data i dont know what should i do to have a result .

@mrbean-bremen
Copy link
Member

Well, the easiest is probably to just use a for loop as you did and do the processing in the loop to get it running.
The generator expression basically just adds some syntactic sugar - you may come to that later.

@nature01
Copy link
Author

so only with the for loop i can execute operations on the entire directory ! that will be great !
i will post my results if i get any !

Thanks a lot fo your Help !!

@mrbean-bremen
Copy link
Member

You are welcome - have fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants