3D local averages and using 3D convolution
I'm new to python and am far more familliar with Matlab. If my question is ill suited for this forum, don't hesitate to point it out.
I'm trying to make local averages at a very fast speed. It's like I'm trying to reduce the number of pixel in an image, by making an average of multiple pixels for each new pixel, except I'm doing it in 3D.
Imagine a 1000x1000x6 arrays. I'm dividing this array in multiple tiny arrays of 10x10x3. I then want to calculate the mean of all those tiny arrays and put them back together to build back my array.
The way I did it on Matlab was with convn(array,seed,'valid')
, which is a multi-dimension convolution function.
What would be the easiest way to do it in python?
Thanks
RMT
python convolution
add a comment |
I'm new to python and am far more familliar with Matlab. If my question is ill suited for this forum, don't hesitate to point it out.
I'm trying to make local averages at a very fast speed. It's like I'm trying to reduce the number of pixel in an image, by making an average of multiple pixels for each new pixel, except I'm doing it in 3D.
Imagine a 1000x1000x6 arrays. I'm dividing this array in multiple tiny arrays of 10x10x3. I then want to calculate the mean of all those tiny arrays and put them back together to build back my array.
The way I did it on Matlab was with convn(array,seed,'valid')
, which is a multi-dimension convolution function.
What would be the easiest way to do it in python?
Thanks
RMT
python convolution
Maybe look at numpy'sconvolve
? numpy.org/devdocs/reference/generated/numpy.convolve.html
– wgoodall01
Nov 12 '18 at 23:30
Tryscipy.ndimage.filters.convolve
: docs.scipy.org/doc/scipy-0.14.0/reference/generated/…
– RafazZ
Nov 12 '18 at 23:57
add a comment |
I'm new to python and am far more familliar with Matlab. If my question is ill suited for this forum, don't hesitate to point it out.
I'm trying to make local averages at a very fast speed. It's like I'm trying to reduce the number of pixel in an image, by making an average of multiple pixels for each new pixel, except I'm doing it in 3D.
Imagine a 1000x1000x6 arrays. I'm dividing this array in multiple tiny arrays of 10x10x3. I then want to calculate the mean of all those tiny arrays and put them back together to build back my array.
The way I did it on Matlab was with convn(array,seed,'valid')
, which is a multi-dimension convolution function.
What would be the easiest way to do it in python?
Thanks
RMT
python convolution
I'm new to python and am far more familliar with Matlab. If my question is ill suited for this forum, don't hesitate to point it out.
I'm trying to make local averages at a very fast speed. It's like I'm trying to reduce the number of pixel in an image, by making an average of multiple pixels for each new pixel, except I'm doing it in 3D.
Imagine a 1000x1000x6 arrays. I'm dividing this array in multiple tiny arrays of 10x10x3. I then want to calculate the mean of all those tiny arrays and put them back together to build back my array.
The way I did it on Matlab was with convn(array,seed,'valid')
, which is a multi-dimension convolution function.
What would be the easiest way to do it in python?
Thanks
RMT
python convolution
python convolution
edited Nov 13 '18 at 3:20
RafazZ
1,017624
1,017624
asked Nov 12 '18 at 22:49
Raphaël Maltais-Tariant
82
82
Maybe look at numpy'sconvolve
? numpy.org/devdocs/reference/generated/numpy.convolve.html
– wgoodall01
Nov 12 '18 at 23:30
Tryscipy.ndimage.filters.convolve
: docs.scipy.org/doc/scipy-0.14.0/reference/generated/…
– RafazZ
Nov 12 '18 at 23:57
add a comment |
Maybe look at numpy'sconvolve
? numpy.org/devdocs/reference/generated/numpy.convolve.html
– wgoodall01
Nov 12 '18 at 23:30
Tryscipy.ndimage.filters.convolve
: docs.scipy.org/doc/scipy-0.14.0/reference/generated/…
– RafazZ
Nov 12 '18 at 23:57
Maybe look at numpy's
convolve
? numpy.org/devdocs/reference/generated/numpy.convolve.html– wgoodall01
Nov 12 '18 at 23:30
Maybe look at numpy's
convolve
? numpy.org/devdocs/reference/generated/numpy.convolve.html– wgoodall01
Nov 12 '18 at 23:30
Try
scipy.ndimage.filters.convolve
: docs.scipy.org/doc/scipy-0.14.0/reference/generated/…– RafazZ
Nov 12 '18 at 23:57
Try
scipy.ndimage.filters.convolve
: docs.scipy.org/doc/scipy-0.14.0/reference/generated/…– RafazZ
Nov 12 '18 at 23:57
add a comment |
1 Answer
1
active
oldest
votes
I think the closest thing that you can find to the convn
is the SciPy's convolve
. Below is the example
import numpy as np
from scipy.ndimage import convolve
M = np.random.random((1000, 1000, 6))
seed = np.ones((3, 3, 3)) * 0.1 / 27.
N = convolve(M, seed, mode='constant', cval=0)
The mode='constant', cval=0
is just zero-padding.
Not sure if that's what you need, but that's a start
Doc: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.convolve.html
I tried using this function but I was a bit confuse about the output. I'm interested and only the results that doesn't touch the padding. Meaning, if I do that convolution, I would expect a 997x957x4 array. Would the results that interest me would be N[1:-1,1:-1,1:-1]?
– Raphaël Maltais-Tariant
Nov 13 '18 at 17:16
That what themode
argument is for -- it makes sure the edges are convolved properly. If you want to have onlyvalid
input in the convolution (that is no padding allowed), you can always strip the final result of the edges using slicing. This is more of a hack tho :)
– RafazZ
Nov 13 '18 at 17:28
Just saw your edit to the comment -- I think you are right with the slicing, assuming you have a 3x3 kernel. Correct me if I am wrong, but I think the expected output would be 998x998x4, if the convolution isvalid
, input is 1000x1000x6, and the kernel is 3x3x3
– RafazZ
Nov 13 '18 at 17:30
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271222%2f3d-local-averages-and-using-3d-convolution%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
I think the closest thing that you can find to the convn
is the SciPy's convolve
. Below is the example
import numpy as np
from scipy.ndimage import convolve
M = np.random.random((1000, 1000, 6))
seed = np.ones((3, 3, 3)) * 0.1 / 27.
N = convolve(M, seed, mode='constant', cval=0)
The mode='constant', cval=0
is just zero-padding.
Not sure if that's what you need, but that's a start
Doc: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.convolve.html
I tried using this function but I was a bit confuse about the output. I'm interested and only the results that doesn't touch the padding. Meaning, if I do that convolution, I would expect a 997x957x4 array. Would the results that interest me would be N[1:-1,1:-1,1:-1]?
– Raphaël Maltais-Tariant
Nov 13 '18 at 17:16
That what themode
argument is for -- it makes sure the edges are convolved properly. If you want to have onlyvalid
input in the convolution (that is no padding allowed), you can always strip the final result of the edges using slicing. This is more of a hack tho :)
– RafazZ
Nov 13 '18 at 17:28
Just saw your edit to the comment -- I think you are right with the slicing, assuming you have a 3x3 kernel. Correct me if I am wrong, but I think the expected output would be 998x998x4, if the convolution isvalid
, input is 1000x1000x6, and the kernel is 3x3x3
– RafazZ
Nov 13 '18 at 17:30
add a comment |
I think the closest thing that you can find to the convn
is the SciPy's convolve
. Below is the example
import numpy as np
from scipy.ndimage import convolve
M = np.random.random((1000, 1000, 6))
seed = np.ones((3, 3, 3)) * 0.1 / 27.
N = convolve(M, seed, mode='constant', cval=0)
The mode='constant', cval=0
is just zero-padding.
Not sure if that's what you need, but that's a start
Doc: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.convolve.html
I tried using this function but I was a bit confuse about the output. I'm interested and only the results that doesn't touch the padding. Meaning, if I do that convolution, I would expect a 997x957x4 array. Would the results that interest me would be N[1:-1,1:-1,1:-1]?
– Raphaël Maltais-Tariant
Nov 13 '18 at 17:16
That what themode
argument is for -- it makes sure the edges are convolved properly. If you want to have onlyvalid
input in the convolution (that is no padding allowed), you can always strip the final result of the edges using slicing. This is more of a hack tho :)
– RafazZ
Nov 13 '18 at 17:28
Just saw your edit to the comment -- I think you are right with the slicing, assuming you have a 3x3 kernel. Correct me if I am wrong, but I think the expected output would be 998x998x4, if the convolution isvalid
, input is 1000x1000x6, and the kernel is 3x3x3
– RafazZ
Nov 13 '18 at 17:30
add a comment |
I think the closest thing that you can find to the convn
is the SciPy's convolve
. Below is the example
import numpy as np
from scipy.ndimage import convolve
M = np.random.random((1000, 1000, 6))
seed = np.ones((3, 3, 3)) * 0.1 / 27.
N = convolve(M, seed, mode='constant', cval=0)
The mode='constant', cval=0
is just zero-padding.
Not sure if that's what you need, but that's a start
Doc: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.convolve.html
I think the closest thing that you can find to the convn
is the SciPy's convolve
. Below is the example
import numpy as np
from scipy.ndimage import convolve
M = np.random.random((1000, 1000, 6))
seed = np.ones((3, 3, 3)) * 0.1 / 27.
N = convolve(M, seed, mode='constant', cval=0)
The mode='constant', cval=0
is just zero-padding.
Not sure if that's what you need, but that's a start
Doc: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.filters.convolve.html
answered Nov 13 '18 at 0:05
RafazZ
1,017624
1,017624
I tried using this function but I was a bit confuse about the output. I'm interested and only the results that doesn't touch the padding. Meaning, if I do that convolution, I would expect a 997x957x4 array. Would the results that interest me would be N[1:-1,1:-1,1:-1]?
– Raphaël Maltais-Tariant
Nov 13 '18 at 17:16
That what themode
argument is for -- it makes sure the edges are convolved properly. If you want to have onlyvalid
input in the convolution (that is no padding allowed), you can always strip the final result of the edges using slicing. This is more of a hack tho :)
– RafazZ
Nov 13 '18 at 17:28
Just saw your edit to the comment -- I think you are right with the slicing, assuming you have a 3x3 kernel. Correct me if I am wrong, but I think the expected output would be 998x998x4, if the convolution isvalid
, input is 1000x1000x6, and the kernel is 3x3x3
– RafazZ
Nov 13 '18 at 17:30
add a comment |
I tried using this function but I was a bit confuse about the output. I'm interested and only the results that doesn't touch the padding. Meaning, if I do that convolution, I would expect a 997x957x4 array. Would the results that interest me would be N[1:-1,1:-1,1:-1]?
– Raphaël Maltais-Tariant
Nov 13 '18 at 17:16
That what themode
argument is for -- it makes sure the edges are convolved properly. If you want to have onlyvalid
input in the convolution (that is no padding allowed), you can always strip the final result of the edges using slicing. This is more of a hack tho :)
– RafazZ
Nov 13 '18 at 17:28
Just saw your edit to the comment -- I think you are right with the slicing, assuming you have a 3x3 kernel. Correct me if I am wrong, but I think the expected output would be 998x998x4, if the convolution isvalid
, input is 1000x1000x6, and the kernel is 3x3x3
– RafazZ
Nov 13 '18 at 17:30
I tried using this function but I was a bit confuse about the output. I'm interested and only the results that doesn't touch the padding. Meaning, if I do that convolution, I would expect a 997x957x4 array. Would the results that interest me would be N[1:-1,1:-1,1:-1]?
– Raphaël Maltais-Tariant
Nov 13 '18 at 17:16
I tried using this function but I was a bit confuse about the output. I'm interested and only the results that doesn't touch the padding. Meaning, if I do that convolution, I would expect a 997x957x4 array. Would the results that interest me would be N[1:-1,1:-1,1:-1]?
– Raphaël Maltais-Tariant
Nov 13 '18 at 17:16
That what the
mode
argument is for -- it makes sure the edges are convolved properly. If you want to have only valid
input in the convolution (that is no padding allowed), you can always strip the final result of the edges using slicing. This is more of a hack tho :)– RafazZ
Nov 13 '18 at 17:28
That what the
mode
argument is for -- it makes sure the edges are convolved properly. If you want to have only valid
input in the convolution (that is no padding allowed), you can always strip the final result of the edges using slicing. This is more of a hack tho :)– RafazZ
Nov 13 '18 at 17:28
Just saw your edit to the comment -- I think you are right with the slicing, assuming you have a 3x3 kernel. Correct me if I am wrong, but I think the expected output would be 998x998x4, if the convolution is
valid
, input is 1000x1000x6, and the kernel is 3x3x3– RafazZ
Nov 13 '18 at 17:30
Just saw your edit to the comment -- I think you are right with the slicing, assuming you have a 3x3 kernel. Correct me if I am wrong, but I think the expected output would be 998x998x4, if the convolution is
valid
, input is 1000x1000x6, and the kernel is 3x3x3– RafazZ
Nov 13 '18 at 17:30
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271222%2f3d-local-averages-and-using-3d-convolution%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Maybe look at numpy's
convolve
? numpy.org/devdocs/reference/generated/numpy.convolve.html– wgoodall01
Nov 12 '18 at 23:30
Try
scipy.ndimage.filters.convolve
: docs.scipy.org/doc/scipy-0.14.0/reference/generated/…– RafazZ
Nov 12 '18 at 23:57