How to crop a 3D image when the point of interest lies on the edge of the image?












1














I have a 3D image of size 512*512*30. I also have a csv file with the points of interest stored in it. I want to crop a 3D volume of size 32*32*16 around the point of interest with the point at its center. Ive written the following to achieve this:



block = [32, 32, 16]
img = imageio.volread('path\to\tiff\file')
x, y, z = 191, 303, 17
img_block = img_block[x - int(block[0] / 2):x + int(block[0] / 2),
y - int(block[1] / 2):y + int(block[1] / 2),
z - int(block[2] / 2):z + int(block[2] / 2)]


This works in the above case but fails when I have an x,y,z point on the edge for example at z = 28 I get an out of bound error, which is expected.



How do I avoid this problem and ensure smooth copping?



Thanks










share|improve this question






















  • The answer depends a little on what you want to do with the outliers. Typical ways to deal with the issue are called "bounds-checking". You can pad your array with zeros, mirror at the border or just discard values. What works best for you?
    – Dschoni
    Nov 13 at 17:02






  • 1




    Furthermore: If one point is in the center, the size should be rather odd than even.
    – Dschoni
    Nov 13 at 17:06
















1














I have a 3D image of size 512*512*30. I also have a csv file with the points of interest stored in it. I want to crop a 3D volume of size 32*32*16 around the point of interest with the point at its center. Ive written the following to achieve this:



block = [32, 32, 16]
img = imageio.volread('path\to\tiff\file')
x, y, z = 191, 303, 17
img_block = img_block[x - int(block[0] / 2):x + int(block[0] / 2),
y - int(block[1] / 2):y + int(block[1] / 2),
z - int(block[2] / 2):z + int(block[2] / 2)]


This works in the above case but fails when I have an x,y,z point on the edge for example at z = 28 I get an out of bound error, which is expected.



How do I avoid this problem and ensure smooth copping?



Thanks










share|improve this question






















  • The answer depends a little on what you want to do with the outliers. Typical ways to deal with the issue are called "bounds-checking". You can pad your array with zeros, mirror at the border or just discard values. What works best for you?
    – Dschoni
    Nov 13 at 17:02






  • 1




    Furthermore: If one point is in the center, the size should be rather odd than even.
    – Dschoni
    Nov 13 at 17:06














1












1








1







I have a 3D image of size 512*512*30. I also have a csv file with the points of interest stored in it. I want to crop a 3D volume of size 32*32*16 around the point of interest with the point at its center. Ive written the following to achieve this:



block = [32, 32, 16]
img = imageio.volread('path\to\tiff\file')
x, y, z = 191, 303, 17
img_block = img_block[x - int(block[0] / 2):x + int(block[0] / 2),
y - int(block[1] / 2):y + int(block[1] / 2),
z - int(block[2] / 2):z + int(block[2] / 2)]


This works in the above case but fails when I have an x,y,z point on the edge for example at z = 28 I get an out of bound error, which is expected.



How do I avoid this problem and ensure smooth copping?



Thanks










share|improve this question













I have a 3D image of size 512*512*30. I also have a csv file with the points of interest stored in it. I want to crop a 3D volume of size 32*32*16 around the point of interest with the point at its center. Ive written the following to achieve this:



block = [32, 32, 16]
img = imageio.volread('path\to\tiff\file')
x, y, z = 191, 303, 17
img_block = img_block[x - int(block[0] / 2):x + int(block[0] / 2),
y - int(block[1] / 2):y + int(block[1] / 2),
z - int(block[2] / 2):z + int(block[2] / 2)]


This works in the above case but fails when I have an x,y,z point on the edge for example at z = 28 I get an out of bound error, which is expected.



How do I avoid this problem and ensure smooth copping?



Thanks







python numpy image-processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 16:59









StuckInPhD

82132141




82132141












  • The answer depends a little on what you want to do with the outliers. Typical ways to deal with the issue are called "bounds-checking". You can pad your array with zeros, mirror at the border or just discard values. What works best for you?
    – Dschoni
    Nov 13 at 17:02






  • 1




    Furthermore: If one point is in the center, the size should be rather odd than even.
    – Dschoni
    Nov 13 at 17:06


















  • The answer depends a little on what you want to do with the outliers. Typical ways to deal with the issue are called "bounds-checking". You can pad your array with zeros, mirror at the border or just discard values. What works best for you?
    – Dschoni
    Nov 13 at 17:02






  • 1




    Furthermore: If one point is in the center, the size should be rather odd than even.
    – Dschoni
    Nov 13 at 17:06
















The answer depends a little on what you want to do with the outliers. Typical ways to deal with the issue are called "bounds-checking". You can pad your array with zeros, mirror at the border or just discard values. What works best for you?
– Dschoni
Nov 13 at 17:02




The answer depends a little on what you want to do with the outliers. Typical ways to deal with the issue are called "bounds-checking". You can pad your array with zeros, mirror at the border or just discard values. What works best for you?
– Dschoni
Nov 13 at 17:02




1




1




Furthermore: If one point is in the center, the size should be rather odd than even.
– Dschoni
Nov 13 at 17:06




Furthermore: If one point is in the center, the size should be rather odd than even.
– Dschoni
Nov 13 at 17:06












1 Answer
1






active

oldest

votes


















0














The answer for padding could look like this:



import numpy as np

point = [0,1,2]
img = imageio.volread('path\to\tiff\file')
block = [32,32,16]
img_x, img_y, img_z = img.shape
img_padded = np.pad(img, block, 'constant', constant_values=0) #lookup np.pad for other padding options
img_block = img_padded[point[0]-block[0]/2:point[0]+block[0]/2...]





share|improve this answer





















  • Thanks, Ive tried padding as well. But wouldnt padding before cropping change the location of x,y,z?
    – StuckInPhD
    Nov 13 at 17:24










  • This answer worked for me, but I would like to add that I would add the pad amount to my coordinate, for example, if I only padded the z-dimension using npad = ((0, 0), (0, 0), (10, 10)) and img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0). The this would add 10 rows before and 10 rows after in the z-dimension. So I would add 10 to the z-coordinate.
    – StuckInPhD
    Nov 14 at 11:52










  • I'm adding once again that np.pad has more options to deal with the padded area. E.g. you can mirror or clamp the value on the edges, which makes sense if you deal with e.g. image reconstruction (to avoid abrupt changes in intensity) but doesn't make much sense for visualisation.
    – Dschoni
    Nov 15 at 13:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286025%2fhow-to-crop-a-3d-image-when-the-point-of-interest-lies-on-the-edge-of-the-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The answer for padding could look like this:



import numpy as np

point = [0,1,2]
img = imageio.volread('path\to\tiff\file')
block = [32,32,16]
img_x, img_y, img_z = img.shape
img_padded = np.pad(img, block, 'constant', constant_values=0) #lookup np.pad for other padding options
img_block = img_padded[point[0]-block[0]/2:point[0]+block[0]/2...]





share|improve this answer





















  • Thanks, Ive tried padding as well. But wouldnt padding before cropping change the location of x,y,z?
    – StuckInPhD
    Nov 13 at 17:24










  • This answer worked for me, but I would like to add that I would add the pad amount to my coordinate, for example, if I only padded the z-dimension using npad = ((0, 0), (0, 0), (10, 10)) and img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0). The this would add 10 rows before and 10 rows after in the z-dimension. So I would add 10 to the z-coordinate.
    – StuckInPhD
    Nov 14 at 11:52










  • I'm adding once again that np.pad has more options to deal with the padded area. E.g. you can mirror or clamp the value on the edges, which makes sense if you deal with e.g. image reconstruction (to avoid abrupt changes in intensity) but doesn't make much sense for visualisation.
    – Dschoni
    Nov 15 at 13:29
















0














The answer for padding could look like this:



import numpy as np

point = [0,1,2]
img = imageio.volread('path\to\tiff\file')
block = [32,32,16]
img_x, img_y, img_z = img.shape
img_padded = np.pad(img, block, 'constant', constant_values=0) #lookup np.pad for other padding options
img_block = img_padded[point[0]-block[0]/2:point[0]+block[0]/2...]





share|improve this answer





















  • Thanks, Ive tried padding as well. But wouldnt padding before cropping change the location of x,y,z?
    – StuckInPhD
    Nov 13 at 17:24










  • This answer worked for me, but I would like to add that I would add the pad amount to my coordinate, for example, if I only padded the z-dimension using npad = ((0, 0), (0, 0), (10, 10)) and img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0). The this would add 10 rows before and 10 rows after in the z-dimension. So I would add 10 to the z-coordinate.
    – StuckInPhD
    Nov 14 at 11:52










  • I'm adding once again that np.pad has more options to deal with the padded area. E.g. you can mirror or clamp the value on the edges, which makes sense if you deal with e.g. image reconstruction (to avoid abrupt changes in intensity) but doesn't make much sense for visualisation.
    – Dschoni
    Nov 15 at 13:29














0












0








0






The answer for padding could look like this:



import numpy as np

point = [0,1,2]
img = imageio.volread('path\to\tiff\file')
block = [32,32,16]
img_x, img_y, img_z = img.shape
img_padded = np.pad(img, block, 'constant', constant_values=0) #lookup np.pad for other padding options
img_block = img_padded[point[0]-block[0]/2:point[0]+block[0]/2...]





share|improve this answer












The answer for padding could look like this:



import numpy as np

point = [0,1,2]
img = imageio.volread('path\to\tiff\file')
block = [32,32,16]
img_x, img_y, img_z = img.shape
img_padded = np.pad(img, block, 'constant', constant_values=0) #lookup np.pad for other padding options
img_block = img_padded[point[0]-block[0]/2:point[0]+block[0]/2...]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 17:15









Dschoni

1,2391944




1,2391944












  • Thanks, Ive tried padding as well. But wouldnt padding before cropping change the location of x,y,z?
    – StuckInPhD
    Nov 13 at 17:24










  • This answer worked for me, but I would like to add that I would add the pad amount to my coordinate, for example, if I only padded the z-dimension using npad = ((0, 0), (0, 0), (10, 10)) and img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0). The this would add 10 rows before and 10 rows after in the z-dimension. So I would add 10 to the z-coordinate.
    – StuckInPhD
    Nov 14 at 11:52










  • I'm adding once again that np.pad has more options to deal with the padded area. E.g. you can mirror or clamp the value on the edges, which makes sense if you deal with e.g. image reconstruction (to avoid abrupt changes in intensity) but doesn't make much sense for visualisation.
    – Dschoni
    Nov 15 at 13:29


















  • Thanks, Ive tried padding as well. But wouldnt padding before cropping change the location of x,y,z?
    – StuckInPhD
    Nov 13 at 17:24










  • This answer worked for me, but I would like to add that I would add the pad amount to my coordinate, for example, if I only padded the z-dimension using npad = ((0, 0), (0, 0), (10, 10)) and img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0). The this would add 10 rows before and 10 rows after in the z-dimension. So I would add 10 to the z-coordinate.
    – StuckInPhD
    Nov 14 at 11:52










  • I'm adding once again that np.pad has more options to deal with the padded area. E.g. you can mirror or clamp the value on the edges, which makes sense if you deal with e.g. image reconstruction (to avoid abrupt changes in intensity) but doesn't make much sense for visualisation.
    – Dschoni
    Nov 15 at 13:29
















Thanks, Ive tried padding as well. But wouldnt padding before cropping change the location of x,y,z?
– StuckInPhD
Nov 13 at 17:24




Thanks, Ive tried padding as well. But wouldnt padding before cropping change the location of x,y,z?
– StuckInPhD
Nov 13 at 17:24












This answer worked for me, but I would like to add that I would add the pad amount to my coordinate, for example, if I only padded the z-dimension using npad = ((0, 0), (0, 0), (10, 10)) and img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0). The this would add 10 rows before and 10 rows after in the z-dimension. So I would add 10 to the z-coordinate.
– StuckInPhD
Nov 14 at 11:52




This answer worked for me, but I would like to add that I would add the pad amount to my coordinate, for example, if I only padded the z-dimension using npad = ((0, 0), (0, 0), (10, 10)) and img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0). The this would add 10 rows before and 10 rows after in the z-dimension. So I would add 10 to the z-coordinate.
– StuckInPhD
Nov 14 at 11:52












I'm adding once again that np.pad has more options to deal with the padded area. E.g. you can mirror or clamp the value on the edges, which makes sense if you deal with e.g. image reconstruction (to avoid abrupt changes in intensity) but doesn't make much sense for visualisation.
– Dschoni
Nov 15 at 13:29




I'm adding once again that np.pad has more options to deal with the padded area. E.g. you can mirror or clamp the value on the edges, which makes sense if you deal with e.g. image reconstruction (to avoid abrupt changes in intensity) but doesn't make much sense for visualisation.
– Dschoni
Nov 15 at 13:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286025%2fhow-to-crop-a-3d-image-when-the-point-of-interest-lies-on-the-edge-of-the-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)