numpy mask creation broadcasting
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have n-size of image (n,row,col) and n-size of floats (n,1).
what I want to do is to create mask of 0 and 1 of (row, col) size. 1's in center and 0's in edges. size of 1's is according to weights.
example
>>> immask = np.zeros((2,8,8))
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
>>> multiplier = np.array([16./64,32./64])
[0.25 0.5 ]
#**insert magic here**
# expected result :
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
is there any way to do this with broadcasting? not using loop. thanks in advance.
python numpy image-processing
add a comment |
I have n-size of image (n,row,col) and n-size of floats (n,1).
what I want to do is to create mask of 0 and 1 of (row, col) size. 1's in center and 0's in edges. size of 1's is according to weights.
example
>>> immask = np.zeros((2,8,8))
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
>>> multiplier = np.array([16./64,32./64])
[0.25 0.5 ]
#**insert magic here**
# expected result :
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
is there any way to do this with broadcasting? not using loop. thanks in advance.
python numpy image-processing
1
What have you tried so far? And how should your weights affect the amount of 1's? Is the factor in multiplier the ratio of 1's per row/colum?
– Scotty1-
Jan 4 at 15:11
so far much like @roland-smith answer, using looping on range(n), using half of image row and col, substracted by it's ratio, slicing through like it's from (+)left/up : (-)right/down, assign them by 1. immask[n,(col-(multiplier[n]*col)//2):-(col-(multiplier[n]*col)//2),(row-(multiplier[n]*row)//2):-(row-(multiplier[n]*row)//2)] (forgetting the details, but something like that)
– itonia.x.i
Jan 4 at 16:41
add a comment |
I have n-size of image (n,row,col) and n-size of floats (n,1).
what I want to do is to create mask of 0 and 1 of (row, col) size. 1's in center and 0's in edges. size of 1's is according to weights.
example
>>> immask = np.zeros((2,8,8))
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
>>> multiplier = np.array([16./64,32./64])
[0.25 0.5 ]
#**insert magic here**
# expected result :
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
is there any way to do this with broadcasting? not using loop. thanks in advance.
python numpy image-processing
I have n-size of image (n,row,col) and n-size of floats (n,1).
what I want to do is to create mask of 0 and 1 of (row, col) size. 1's in center and 0's in edges. size of 1's is according to weights.
example
>>> immask = np.zeros((2,8,8))
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
>>> multiplier = np.array([16./64,32./64])
[0.25 0.5 ]
#**insert magic here**
# expected result :
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
is there any way to do this with broadcasting? not using loop. thanks in advance.
python numpy image-processing
python numpy image-processing
asked Jan 4 at 15:03
itonia.x.iitonia.x.i
103
103
1
What have you tried so far? And how should your weights affect the amount of 1's? Is the factor in multiplier the ratio of 1's per row/colum?
– Scotty1-
Jan 4 at 15:11
so far much like @roland-smith answer, using looping on range(n), using half of image row and col, substracted by it's ratio, slicing through like it's from (+)left/up : (-)right/down, assign them by 1. immask[n,(col-(multiplier[n]*col)//2):-(col-(multiplier[n]*col)//2),(row-(multiplier[n]*row)//2):-(row-(multiplier[n]*row)//2)] (forgetting the details, but something like that)
– itonia.x.i
Jan 4 at 16:41
add a comment |
1
What have you tried so far? And how should your weights affect the amount of 1's? Is the factor in multiplier the ratio of 1's per row/colum?
– Scotty1-
Jan 4 at 15:11
so far much like @roland-smith answer, using looping on range(n), using half of image row and col, substracted by it's ratio, slicing through like it's from (+)left/up : (-)right/down, assign them by 1. immask[n,(col-(multiplier[n]*col)//2):-(col-(multiplier[n]*col)//2),(row-(multiplier[n]*row)//2):-(row-(multiplier[n]*row)//2)] (forgetting the details, but something like that)
– itonia.x.i
Jan 4 at 16:41
1
1
What have you tried so far? And how should your weights affect the amount of 1's? Is the factor in multiplier the ratio of 1's per row/colum?
– Scotty1-
Jan 4 at 15:11
What have you tried so far? And how should your weights affect the amount of 1's? Is the factor in multiplier the ratio of 1's per row/colum?
– Scotty1-
Jan 4 at 15:11
so far much like @roland-smith answer, using looping on range(n), using half of image row and col, substracted by it's ratio, slicing through like it's from (+)left/up : (-)right/down, assign them by 1. immask[n,(col-(multiplier[n]*col)//2):-(col-(multiplier[n]*col)//2),(row-(multiplier[n]*row)//2):-(row-(multiplier[n]*row)//2)] (forgetting the details, but something like that)
– itonia.x.i
Jan 4 at 16:41
so far much like @roland-smith answer, using looping on range(n), using half of image row and col, substracted by it's ratio, slicing through like it's from (+)left/up : (-)right/down, assign them by 1. immask[n,(col-(multiplier[n]*col)//2):-(col-(multiplier[n]*col)//2),(row-(multiplier[n]*row)//2):-(row-(multiplier[n]*row)//2)] (forgetting the details, but something like that)
– itonia.x.i
Jan 4 at 16:41
add a comment |
3 Answers
3
active
oldest
votes
This function does something like that:
import numpy as np
def make_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Make masks where criteria is met
return (np.abs(r) <= w) & (np.abs(c) <= w)
# Test
masks = make_masks([0.25, 0.5], 8)
# Convert type as needed
masks_f = masks.astype(np.float32)
print(masks_f)
Output:
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
EDIT
To make circle-shaped masks, the function could be as follows:
import numpy as np
def make_circle_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Arrays with distances to centre
d = r * r + c * c
# Make masks where criteria is met
return d <= w * w
thank you, this is what I'm looking for. can 1's be shaped to circle?
– itonia.x.i
Jan 4 at 17:08
@itonia.x.i I have added a version for circle-shaped masks.
– jdehesa
Jan 4 at 17:14
add a comment |
I think if you want to use some kind of matrix to be applied over a picture this:
from scipy.ndimage.filters import convolve
could work for you. You could take a deeper look here how this works.
noted. what I really want to do is to divide a image into 8x8 images, then only get center of image according to it's weights/percents, so it looks like images but dotted-like and vary on radius of the dot-like.
– itonia.x.i
Jan 4 at 16:49
add a comment |
Try this:
In [1]: import numpy as np
In [3]: immask = np.zeros((2,8,8))
In [6]: immask[0, 3:5, 3:5] = 1
In [7]: immask[1, 2:6, 2:6] = 1
In [8]: immask
Out[8]:
array([[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]])
nice, I can use loop on 'n' (i.e. 2), and using it's negative instead immask[0, 3:-3, 3:-3] = 1. it is easy to read in loop but it will take long since I have many 'n'.
– itonia.x.i
Jan 4 at 16:44
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%2f54041467%2fnumpy-mask-creation-broadcasting%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This function does something like that:
import numpy as np
def make_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Make masks where criteria is met
return (np.abs(r) <= w) & (np.abs(c) <= w)
# Test
masks = make_masks([0.25, 0.5], 8)
# Convert type as needed
masks_f = masks.astype(np.float32)
print(masks_f)
Output:
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
EDIT
To make circle-shaped masks, the function could be as follows:
import numpy as np
def make_circle_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Arrays with distances to centre
d = r * r + c * c
# Make masks where criteria is met
return d <= w * w
thank you, this is what I'm looking for. can 1's be shaped to circle?
– itonia.x.i
Jan 4 at 17:08
@itonia.x.i I have added a version for circle-shaped masks.
– jdehesa
Jan 4 at 17:14
add a comment |
This function does something like that:
import numpy as np
def make_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Make masks where criteria is met
return (np.abs(r) <= w) & (np.abs(c) <= w)
# Test
masks = make_masks([0.25, 0.5], 8)
# Convert type as needed
masks_f = masks.astype(np.float32)
print(masks_f)
Output:
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
EDIT
To make circle-shaped masks, the function could be as follows:
import numpy as np
def make_circle_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Arrays with distances to centre
d = r * r + c * c
# Make masks where criteria is met
return d <= w * w
thank you, this is what I'm looking for. can 1's be shaped to circle?
– itonia.x.i
Jan 4 at 17:08
@itonia.x.i I have added a version for circle-shaped masks.
– jdehesa
Jan 4 at 17:14
add a comment |
This function does something like that:
import numpy as np
def make_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Make masks where criteria is met
return (np.abs(r) <= w) & (np.abs(c) <= w)
# Test
masks = make_masks([0.25, 0.5], 8)
# Convert type as needed
masks_f = masks.astype(np.float32)
print(masks_f)
Output:
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
EDIT
To make circle-shaped masks, the function could be as follows:
import numpy as np
def make_circle_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Arrays with distances to centre
d = r * r + c * c
# Make masks where criteria is met
return d <= w * w
This function does something like that:
import numpy as np
def make_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Make masks where criteria is met
return (np.abs(r) <= w) & (np.abs(c) <= w)
# Test
masks = make_masks([0.25, 0.5], 8)
# Convert type as needed
masks_f = masks.astype(np.float32)
print(masks_f)
Output:
[[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0.]]]
EDIT
To make circle-shaped masks, the function could be as follows:
import numpy as np
def make_circle_masks(weights, rows, cols=None):
if cols is None:
cols = rows
# Add two dimensions to weights
w = np.asarray(weights)[:, np.newaxis, np.newaxis]
# Open grid of row and column coordinates
r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
# Arrays with distances to centre
d = r * r + c * c
# Make masks where criteria is met
return d <= w * w
edited Jan 4 at 17:13
answered Jan 4 at 15:37
jdehesajdehesa
27.8k43759
27.8k43759
thank you, this is what I'm looking for. can 1's be shaped to circle?
– itonia.x.i
Jan 4 at 17:08
@itonia.x.i I have added a version for circle-shaped masks.
– jdehesa
Jan 4 at 17:14
add a comment |
thank you, this is what I'm looking for. can 1's be shaped to circle?
– itonia.x.i
Jan 4 at 17:08
@itonia.x.i I have added a version for circle-shaped masks.
– jdehesa
Jan 4 at 17:14
thank you, this is what I'm looking for. can 1's be shaped to circle?
– itonia.x.i
Jan 4 at 17:08
thank you, this is what I'm looking for. can 1's be shaped to circle?
– itonia.x.i
Jan 4 at 17:08
@itonia.x.i I have added a version for circle-shaped masks.
– jdehesa
Jan 4 at 17:14
@itonia.x.i I have added a version for circle-shaped masks.
– jdehesa
Jan 4 at 17:14
add a comment |
I think if you want to use some kind of matrix to be applied over a picture this:
from scipy.ndimage.filters import convolve
could work for you. You could take a deeper look here how this works.
noted. what I really want to do is to divide a image into 8x8 images, then only get center of image according to it's weights/percents, so it looks like images but dotted-like and vary on radius of the dot-like.
– itonia.x.i
Jan 4 at 16:49
add a comment |
I think if you want to use some kind of matrix to be applied over a picture this:
from scipy.ndimage.filters import convolve
could work for you. You could take a deeper look here how this works.
noted. what I really want to do is to divide a image into 8x8 images, then only get center of image according to it's weights/percents, so it looks like images but dotted-like and vary on radius of the dot-like.
– itonia.x.i
Jan 4 at 16:49
add a comment |
I think if you want to use some kind of matrix to be applied over a picture this:
from scipy.ndimage.filters import convolve
could work for you. You could take a deeper look here how this works.
I think if you want to use some kind of matrix to be applied over a picture this:
from scipy.ndimage.filters import convolve
could work for you. You could take a deeper look here how this works.
answered Jan 4 at 15:18
default_settingsdefault_settings
5517
5517
noted. what I really want to do is to divide a image into 8x8 images, then only get center of image according to it's weights/percents, so it looks like images but dotted-like and vary on radius of the dot-like.
– itonia.x.i
Jan 4 at 16:49
add a comment |
noted. what I really want to do is to divide a image into 8x8 images, then only get center of image according to it's weights/percents, so it looks like images but dotted-like and vary on radius of the dot-like.
– itonia.x.i
Jan 4 at 16:49
noted. what I really want to do is to divide a image into 8x8 images, then only get center of image according to it's weights/percents, so it looks like images but dotted-like and vary on radius of the dot-like.
– itonia.x.i
Jan 4 at 16:49
noted. what I really want to do is to divide a image into 8x8 images, then only get center of image according to it's weights/percents, so it looks like images but dotted-like and vary on radius of the dot-like.
– itonia.x.i
Jan 4 at 16:49
add a comment |
Try this:
In [1]: import numpy as np
In [3]: immask = np.zeros((2,8,8))
In [6]: immask[0, 3:5, 3:5] = 1
In [7]: immask[1, 2:6, 2:6] = 1
In [8]: immask
Out[8]:
array([[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]])
nice, I can use loop on 'n' (i.e. 2), and using it's negative instead immask[0, 3:-3, 3:-3] = 1. it is easy to read in loop but it will take long since I have many 'n'.
– itonia.x.i
Jan 4 at 16:44
add a comment |
Try this:
In [1]: import numpy as np
In [3]: immask = np.zeros((2,8,8))
In [6]: immask[0, 3:5, 3:5] = 1
In [7]: immask[1, 2:6, 2:6] = 1
In [8]: immask
Out[8]:
array([[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]])
nice, I can use loop on 'n' (i.e. 2), and using it's negative instead immask[0, 3:-3, 3:-3] = 1. it is easy to read in loop but it will take long since I have many 'n'.
– itonia.x.i
Jan 4 at 16:44
add a comment |
Try this:
In [1]: import numpy as np
In [3]: immask = np.zeros((2,8,8))
In [6]: immask[0, 3:5, 3:5] = 1
In [7]: immask[1, 2:6, 2:6] = 1
In [8]: immask
Out[8]:
array([[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]])
Try this:
In [1]: import numpy as np
In [3]: immask = np.zeros((2,8,8))
In [6]: immask[0, 3:5, 3:5] = 1
In [7]: immask[1, 2:6, 2:6] = 1
In [8]: immask
Out[8]:
array([[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.]]])
answered Jan 4 at 15:24
Roland SmithRoland Smith
27.1k33257
27.1k33257
nice, I can use loop on 'n' (i.e. 2), and using it's negative instead immask[0, 3:-3, 3:-3] = 1. it is easy to read in loop but it will take long since I have many 'n'.
– itonia.x.i
Jan 4 at 16:44
add a comment |
nice, I can use loop on 'n' (i.e. 2), and using it's negative instead immask[0, 3:-3, 3:-3] = 1. it is easy to read in loop but it will take long since I have many 'n'.
– itonia.x.i
Jan 4 at 16:44
nice, I can use loop on 'n' (i.e. 2), and using it's negative instead immask[0, 3:-3, 3:-3] = 1. it is easy to read in loop but it will take long since I have many 'n'.
– itonia.x.i
Jan 4 at 16:44
nice, I can use loop on 'n' (i.e. 2), and using it's negative instead immask[0, 3:-3, 3:-3] = 1. it is easy to read in loop but it will take long since I have many 'n'.
– itonia.x.i
Jan 4 at 16:44
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.
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%2f54041467%2fnumpy-mask-creation-broadcasting%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
1
What have you tried so far? And how should your weights affect the amount of 1's? Is the factor in multiplier the ratio of 1's per row/colum?
– Scotty1-
Jan 4 at 15:11
so far much like @roland-smith answer, using looping on range(n), using half of image row and col, substracted by it's ratio, slicing through like it's from (+)left/up : (-)right/down, assign them by 1. immask[n,(col-(multiplier[n]*col)//2):-(col-(multiplier[n]*col)//2),(row-(multiplier[n]*row)//2):-(row-(multiplier[n]*row)//2)] (forgetting the details, but something like that)
– itonia.x.i
Jan 4 at 16:41