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

3D Patch extraction from 3D input #56

Closed
saqibqamar opened this issue Nov 13, 2017 · 1 comment
Closed

3D Patch extraction from 3D input #56

saqibqamar opened this issue Nov 13, 2017 · 1 comment

Comments

@saqibqamar
Copy link

saqibqamar commented Nov 13, 2017

I want to extract 3D patches with shape is 32x32x32 from a 3D input. I have given example of loads images from directory and gets shapes of image axes. Please let me know how to extract 3D patches samples from this input
`
from medpy.io import load
import numpy as np
import os
import h5py

data_path = ../....
for i in range(10):

    subject_name = 'subject-%d-' % i
    f = os.path.join(data_path, subject_name + 'C.hdr')
    img, header = load(f)
    inputs = img.astype(np.float32)
      
    A = inputs.shape[0]     #142
    B = inputs.shape[1]     # 176
    C = inputs.shape[2]     # 181
    D = np.arange(A*B*C).reshape(A,B,C)
    print (D.shape)

`
How could I use the function to create patches with size of 32x32x32 from this input? Please reply.. Thanks

@loli
Copy link
Owner

loli commented Nov 13, 2017

Well, extracting patches should be quite straight forward with numpy (remember: each image returned by medpy is, in fact, a numpy array).

You must first consider how to treat incomplete patches. The image won't always have dimension that are multiples of 32 (e.g., shape=363636). What do you do then? You must decide either to accept smaller left-over patches (e.g., shape=444). Or, alternatively, you can pad the patches. Or, third option, cropthe images to a size that is a multiple of 32 in all dimensions.

Next, should the patches be overlapping? And if yes, how far?

You can get some inspiration from Google (https://www.google.de/search?q=numpy+array+patches+extract+3d)

Medpy provides a few patch iterators for special cases: http://loli.github.io/medpy/iterators.html
For you, the CentredPatchIterator might be interesting: equally sized patches, automatically padded and non-overlapping. Us eit like

for patch i CentredPatchIterator(image, (32,32,32), cval=0):
   do_something_with_patch()

I hope I could be of help. If you have further questions, please write directly to my email, since the issue tracker is intended for bugs only.

@loli loli closed this as completed Nov 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants