How to undo or reverse np.meshgrid?
up vote
1
down vote
favorite
In an image resizing interpolation problem, one could use np.meshgrid
on row and col indices before operating on the meshed indices:
nrows = 600
ncols = 800
image_in = np.random.randint(0, 256, size=(nrows, ncols, 3))
scale_factor = 1.5
r = np.arange(nrows, dtype=float) * scale_factor
c = np.arange(ncols, dtype=float) * scale_factor
rr, cc = np.meshgrid(r, c, indexing='ij')
# Nearest Neighbor Interpolation
# np.floor if scale_factor >= 1. np.ceil otherwise
rr = np.floor(rr).astype(int).clip(0, nrows-1)
cc = np.floor(cc).astype(int).clip(0, ncols-1)
image_out = image_in[rr, cc, :]
Now, how would I reverse this process? Say given rr_1
, cc_1
(product of np.meshgrid
) that's processed in an unknown manner (here illustrated by np.random.randint
), how do I get the r_1
and c_1
, that is, the inputs to np.meshgrid
(preferably with ij
indexing)?
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = ?
c_1 = ?
UPDATE:
I figured it out immediately after posting. The answer is:
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = rr_1[:, 0]
c_1 = cc_1[0]
python numpy image-processing
add a comment |
up vote
1
down vote
favorite
In an image resizing interpolation problem, one could use np.meshgrid
on row and col indices before operating on the meshed indices:
nrows = 600
ncols = 800
image_in = np.random.randint(0, 256, size=(nrows, ncols, 3))
scale_factor = 1.5
r = np.arange(nrows, dtype=float) * scale_factor
c = np.arange(ncols, dtype=float) * scale_factor
rr, cc = np.meshgrid(r, c, indexing='ij')
# Nearest Neighbor Interpolation
# np.floor if scale_factor >= 1. np.ceil otherwise
rr = np.floor(rr).astype(int).clip(0, nrows-1)
cc = np.floor(cc).astype(int).clip(0, ncols-1)
image_out = image_in[rr, cc, :]
Now, how would I reverse this process? Say given rr_1
, cc_1
(product of np.meshgrid
) that's processed in an unknown manner (here illustrated by np.random.randint
), how do I get the r_1
and c_1
, that is, the inputs to np.meshgrid
(preferably with ij
indexing)?
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = ?
c_1 = ?
UPDATE:
I figured it out immediately after posting. The answer is:
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = rr_1[:, 0]
c_1 = cc_1[0]
python numpy image-processing
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In an image resizing interpolation problem, one could use np.meshgrid
on row and col indices before operating on the meshed indices:
nrows = 600
ncols = 800
image_in = np.random.randint(0, 256, size=(nrows, ncols, 3))
scale_factor = 1.5
r = np.arange(nrows, dtype=float) * scale_factor
c = np.arange(ncols, dtype=float) * scale_factor
rr, cc = np.meshgrid(r, c, indexing='ij')
# Nearest Neighbor Interpolation
# np.floor if scale_factor >= 1. np.ceil otherwise
rr = np.floor(rr).astype(int).clip(0, nrows-1)
cc = np.floor(cc).astype(int).clip(0, ncols-1)
image_out = image_in[rr, cc, :]
Now, how would I reverse this process? Say given rr_1
, cc_1
(product of np.meshgrid
) that's processed in an unknown manner (here illustrated by np.random.randint
), how do I get the r_1
and c_1
, that is, the inputs to np.meshgrid
(preferably with ij
indexing)?
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = ?
c_1 = ?
UPDATE:
I figured it out immediately after posting. The answer is:
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = rr_1[:, 0]
c_1 = cc_1[0]
python numpy image-processing
In an image resizing interpolation problem, one could use np.meshgrid
on row and col indices before operating on the meshed indices:
nrows = 600
ncols = 800
image_in = np.random.randint(0, 256, size=(nrows, ncols, 3))
scale_factor = 1.5
r = np.arange(nrows, dtype=float) * scale_factor
c = np.arange(ncols, dtype=float) * scale_factor
rr, cc = np.meshgrid(r, c, indexing='ij')
# Nearest Neighbor Interpolation
# np.floor if scale_factor >= 1. np.ceil otherwise
rr = np.floor(rr).astype(int).clip(0, nrows-1)
cc = np.floor(cc).astype(int).clip(0, ncols-1)
image_out = image_in[rr, cc, :]
Now, how would I reverse this process? Say given rr_1
, cc_1
(product of np.meshgrid
) that's processed in an unknown manner (here illustrated by np.random.randint
), how do I get the r_1
and c_1
, that is, the inputs to np.meshgrid
(preferably with ij
indexing)?
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = ?
c_1 = ?
UPDATE:
I figured it out immediately after posting. The answer is:
# Suppose rr_1, cc_1 = np.meshgrid(r_1, c_1, indexing='ij')
rr_1 = np.random.randint(0, nrows, size=(nrows, ncols, 3))
cc_1 = np.random.randint(0, ncols, size=(nrows, ncols, 3))
r_1 = rr_1[:, 0]
c_1 = cc_1[0]
python numpy image-processing
python numpy image-processing
edited Nov 20 at 4:57
Aqueous Carlos
295213
295213
asked Nov 20 at 3:03
Zhanwen Chen
8019
8019
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The numpy.meshgrid
creates a higher dimensional array from input arrays in order to create grid-like arrays. So imagine you want to get a 2D grid by using some input 1D vectors r
and c
. numpy.meshgrid
returns rr
and cc
as 2D arrays which respectively hold the y axis or x axis constant everywhere on the 2D array (this is why it is a grid).
Here is a test case:
import numpy as np
r = np.arange(5) # [0 1 2 3 4]
c = np.arange(5,10,1) # [5 6 7 8 9]
rr, cc = np.meshgrid(r,c,indexing='ij')
r_original = rr[:,0]
c_original = cc[0,:]
print(r_original) # [0 1 2 3 4]
print(c_original) # [5 6 7 8 9]
Note that the grids we have created for rr
and cc
are
rr = [[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
cc = [[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]]
Since you are using indexing='ij'
in your case and the 2D arrays are transposed. Hence, the values that hold constant for rr
and cc
respectively are the x axis and y axis (contrary to the case where you do not use indexing='ij'
).
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',
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%2f53385605%2fhow-to-undo-or-reverse-np-meshgrid%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
up vote
1
down vote
accepted
The numpy.meshgrid
creates a higher dimensional array from input arrays in order to create grid-like arrays. So imagine you want to get a 2D grid by using some input 1D vectors r
and c
. numpy.meshgrid
returns rr
and cc
as 2D arrays which respectively hold the y axis or x axis constant everywhere on the 2D array (this is why it is a grid).
Here is a test case:
import numpy as np
r = np.arange(5) # [0 1 2 3 4]
c = np.arange(5,10,1) # [5 6 7 8 9]
rr, cc = np.meshgrid(r,c,indexing='ij')
r_original = rr[:,0]
c_original = cc[0,:]
print(r_original) # [0 1 2 3 4]
print(c_original) # [5 6 7 8 9]
Note that the grids we have created for rr
and cc
are
rr = [[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
cc = [[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]]
Since you are using indexing='ij'
in your case and the 2D arrays are transposed. Hence, the values that hold constant for rr
and cc
respectively are the x axis and y axis (contrary to the case where you do not use indexing='ij'
).
add a comment |
up vote
1
down vote
accepted
The numpy.meshgrid
creates a higher dimensional array from input arrays in order to create grid-like arrays. So imagine you want to get a 2D grid by using some input 1D vectors r
and c
. numpy.meshgrid
returns rr
and cc
as 2D arrays which respectively hold the y axis or x axis constant everywhere on the 2D array (this is why it is a grid).
Here is a test case:
import numpy as np
r = np.arange(5) # [0 1 2 3 4]
c = np.arange(5,10,1) # [5 6 7 8 9]
rr, cc = np.meshgrid(r,c,indexing='ij')
r_original = rr[:,0]
c_original = cc[0,:]
print(r_original) # [0 1 2 3 4]
print(c_original) # [5 6 7 8 9]
Note that the grids we have created for rr
and cc
are
rr = [[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
cc = [[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]]
Since you are using indexing='ij'
in your case and the 2D arrays are transposed. Hence, the values that hold constant for rr
and cc
respectively are the x axis and y axis (contrary to the case where you do not use indexing='ij'
).
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The numpy.meshgrid
creates a higher dimensional array from input arrays in order to create grid-like arrays. So imagine you want to get a 2D grid by using some input 1D vectors r
and c
. numpy.meshgrid
returns rr
and cc
as 2D arrays which respectively hold the y axis or x axis constant everywhere on the 2D array (this is why it is a grid).
Here is a test case:
import numpy as np
r = np.arange(5) # [0 1 2 3 4]
c = np.arange(5,10,1) # [5 6 7 8 9]
rr, cc = np.meshgrid(r,c,indexing='ij')
r_original = rr[:,0]
c_original = cc[0,:]
print(r_original) # [0 1 2 3 4]
print(c_original) # [5 6 7 8 9]
Note that the grids we have created for rr
and cc
are
rr = [[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
cc = [[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]]
Since you are using indexing='ij'
in your case and the 2D arrays are transposed. Hence, the values that hold constant for rr
and cc
respectively are the x axis and y axis (contrary to the case where you do not use indexing='ij'
).
The numpy.meshgrid
creates a higher dimensional array from input arrays in order to create grid-like arrays. So imagine you want to get a 2D grid by using some input 1D vectors r
and c
. numpy.meshgrid
returns rr
and cc
as 2D arrays which respectively hold the y axis or x axis constant everywhere on the 2D array (this is why it is a grid).
Here is a test case:
import numpy as np
r = np.arange(5) # [0 1 2 3 4]
c = np.arange(5,10,1) # [5 6 7 8 9]
rr, cc = np.meshgrid(r,c,indexing='ij')
r_original = rr[:,0]
c_original = cc[0,:]
print(r_original) # [0 1 2 3 4]
print(c_original) # [5 6 7 8 9]
Note that the grids we have created for rr
and cc
are
rr = [[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
cc = [[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]
[5 6 7 8 9]]
Since you are using indexing='ij'
in your case and the 2D arrays are transposed. Hence, the values that hold constant for rr
and cc
respectively are the x axis and y axis (contrary to the case where you do not use indexing='ij'
).
edited Nov 20 at 3:38
answered Nov 20 at 3:19
b-fg
1,80111422
1,80111422
add a comment |
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%2f53385605%2fhow-to-undo-or-reverse-np-meshgrid%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