How can I add a small image to a bigger one in Tensorflow?
I want to overlay a smaller image onto a larger one.
I have tried adding to a slice but couldn't get it to work.
So, as a simple example, how do I perform this NumPy operation in Tensorflow:
a = np.array([1, 1, 1, 1])
b = np.array([5, 5])
c = a
c[1:3] = c[1:3] + b
print(c)
# => [1 6 6 1]
python tensorflow
add a comment |
I want to overlay a smaller image onto a larger one.
I have tried adding to a slice but couldn't get it to work.
So, as a simple example, how do I perform this NumPy operation in Tensorflow:
a = np.array([1, 1, 1, 1])
b = np.array([5, 5])
c = a
c[1:3] = c[1:3] + b
print(c)
# => [1 6 6 1]
python tensorflow
This is an ongoing question with TensorFlow. I have given answers to similar cases here, here, here and here (and opened an issue about the need of better support for this here). Check out if any of the answers there help you and then you can mark this as duplicate. If not, I can give a specific answer to your case here.
– jdehesa
Nov 16 '18 at 11:33
Also, I assume your actual case would be for a two-dimensional tensor? I am saying because, while in NumPy it is pretty much the same, in TensorFlow it would make a bigger difference. And would it be overlaying a single image into another, or a single image into a batch, or one batch of images into another batch of images...? And is each image a 2D tensor or 3D, with RGB channels?
– jdehesa
Nov 16 '18 at 11:35
I added a possible implementation. Actually since you are not straight out replacing, but adding to what is already there, can be done more easily just with padding.
– jdehesa
Nov 16 '18 at 15:20
add a comment |
I want to overlay a smaller image onto a larger one.
I have tried adding to a slice but couldn't get it to work.
So, as a simple example, how do I perform this NumPy operation in Tensorflow:
a = np.array([1, 1, 1, 1])
b = np.array([5, 5])
c = a
c[1:3] = c[1:3] + b
print(c)
# => [1 6 6 1]
python tensorflow
I want to overlay a smaller image onto a larger one.
I have tried adding to a slice but couldn't get it to work.
So, as a simple example, how do I perform this NumPy operation in Tensorflow:
a = np.array([1, 1, 1, 1])
b = np.array([5, 5])
c = a
c[1:3] = c[1:3] + b
print(c)
# => [1 6 6 1]
python tensorflow
python tensorflow
asked Nov 16 '18 at 8:07
PumpkinPumpkin
588
588
This is an ongoing question with TensorFlow. I have given answers to similar cases here, here, here and here (and opened an issue about the need of better support for this here). Check out if any of the answers there help you and then you can mark this as duplicate. If not, I can give a specific answer to your case here.
– jdehesa
Nov 16 '18 at 11:33
Also, I assume your actual case would be for a two-dimensional tensor? I am saying because, while in NumPy it is pretty much the same, in TensorFlow it would make a bigger difference. And would it be overlaying a single image into another, or a single image into a batch, or one batch of images into another batch of images...? And is each image a 2D tensor or 3D, with RGB channels?
– jdehesa
Nov 16 '18 at 11:35
I added a possible implementation. Actually since you are not straight out replacing, but adding to what is already there, can be done more easily just with padding.
– jdehesa
Nov 16 '18 at 15:20
add a comment |
This is an ongoing question with TensorFlow. I have given answers to similar cases here, here, here and here (and opened an issue about the need of better support for this here). Check out if any of the answers there help you and then you can mark this as duplicate. If not, I can give a specific answer to your case here.
– jdehesa
Nov 16 '18 at 11:33
Also, I assume your actual case would be for a two-dimensional tensor? I am saying because, while in NumPy it is pretty much the same, in TensorFlow it would make a bigger difference. And would it be overlaying a single image into another, or a single image into a batch, or one batch of images into another batch of images...? And is each image a 2D tensor or 3D, with RGB channels?
– jdehesa
Nov 16 '18 at 11:35
I added a possible implementation. Actually since you are not straight out replacing, but adding to what is already there, can be done more easily just with padding.
– jdehesa
Nov 16 '18 at 15:20
This is an ongoing question with TensorFlow. I have given answers to similar cases here, here, here and here (and opened an issue about the need of better support for this here). Check out if any of the answers there help you and then you can mark this as duplicate. If not, I can give a specific answer to your case here.
– jdehesa
Nov 16 '18 at 11:33
This is an ongoing question with TensorFlow. I have given answers to similar cases here, here, here and here (and opened an issue about the need of better support for this here). Check out if any of the answers there help you and then you can mark this as duplicate. If not, I can give a specific answer to your case here.
– jdehesa
Nov 16 '18 at 11:33
Also, I assume your actual case would be for a two-dimensional tensor? I am saying because, while in NumPy it is pretty much the same, in TensorFlow it would make a bigger difference. And would it be overlaying a single image into another, or a single image into a batch, or one batch of images into another batch of images...? And is each image a 2D tensor or 3D, with RGB channels?
– jdehesa
Nov 16 '18 at 11:35
Also, I assume your actual case would be for a two-dimensional tensor? I am saying because, while in NumPy it is pretty much the same, in TensorFlow it would make a bigger difference. And would it be overlaying a single image into another, or a single image into a batch, or one batch of images into another batch of images...? And is each image a 2D tensor or 3D, with RGB channels?
– jdehesa
Nov 16 '18 at 11:35
I added a possible implementation. Actually since you are not straight out replacing, but adding to what is already there, can be done more easily just with padding.
– jdehesa
Nov 16 '18 at 15:20
I added a possible implementation. Actually since you are not straight out replacing, but adding to what is already there, can be done more easily just with padding.
– jdehesa
Nov 16 '18 at 15:20
add a comment |
1 Answer
1
active
oldest
votes
This is one possible implementation:
import tensorflow as tf
# i and j are first row and colum
# alpha (0..1) is the intensity of the overlay
def overlay_patch(img, patch, i, j, alpha=0.5):
img_shape = tf.shape(img)
img_rows, img_cols = img_shape[0], img_shape[1]
patch_shape = tf.shape(patch)
patch_rows, patch_cols = patch_shape[0], patch_shape[1]
i_end = i + patch_rows
j_end = j + patch_cols
# Mix patch: alpha from patch, minus alpha from image
overlay = alpha * (patch - img[i:i_end, j:j_end])
# Pad patch
overlay_pad = tf.pad(overlay, [[i, img_rows - i_end], [j, img_cols - j_end], [0, 0]])
# Make final image
img_overlay = img + overlay_pad
return img_overlay
Test:
img = tf.placeholder(tf.float32, [None, None, None])
patch = tf.placeholder(tf.float32, [None, None, None])
i = tf.placeholder(tf.int32, )
j = tf.placeholder(tf.int32, )
alpha = tf.placeholder(tf.float32, )
img_overlay = overlay_patch(img, patch, i, j, alpha)
with tf.Session() as sess:
result = sess.run(img_overlay, feed_dict={
img: [[[ 1], [ 2], [ 3], [ 4]],
[[ 5], [ 6], [ 7], [ 8]],
[[ 9], [10], [11], [12]],
[[13], [14], [15], [16]]],
patch: [[[10], [20], [30]],
[[40], [50], [60]]],
i: 2, j: 1, alpha: 0.5
})
print(result[..., 0])
Output:
[[ 1. 2. 3. 4. ]
[ 5. 6. 7. 8. ]
[ 9. 10. 15.5 21. ]
[13. 27. 32.5 38. ]]
1
@Pumpkin I added the 3rd dimension thing, and analphaparameter to control the blend (0 is full original image, 1 is full patch). I still try to avoid the making the mask, I think just multiplication and addition should be faster. Here I "pre-subtract" the image from the patch before adding it so the final blending is correct.
– jdehesa
Nov 16 '18 at 15:53
@Pumpkin (I'm thinking this trick can actually be always used for slice replacement withalpha=1, at least with signed data types)
– jdehesa
Nov 16 '18 at 15:57
Nice, too bad I can't upvote again. :)
– Pumpkin
Nov 16 '18 at 16:02
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%2f53333756%2fhow-can-i-add-a-small-image-to-a-bigger-one-in-tensorflow%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
This is one possible implementation:
import tensorflow as tf
# i and j are first row and colum
# alpha (0..1) is the intensity of the overlay
def overlay_patch(img, patch, i, j, alpha=0.5):
img_shape = tf.shape(img)
img_rows, img_cols = img_shape[0], img_shape[1]
patch_shape = tf.shape(patch)
patch_rows, patch_cols = patch_shape[0], patch_shape[1]
i_end = i + patch_rows
j_end = j + patch_cols
# Mix patch: alpha from patch, minus alpha from image
overlay = alpha * (patch - img[i:i_end, j:j_end])
# Pad patch
overlay_pad = tf.pad(overlay, [[i, img_rows - i_end], [j, img_cols - j_end], [0, 0]])
# Make final image
img_overlay = img + overlay_pad
return img_overlay
Test:
img = tf.placeholder(tf.float32, [None, None, None])
patch = tf.placeholder(tf.float32, [None, None, None])
i = tf.placeholder(tf.int32, )
j = tf.placeholder(tf.int32, )
alpha = tf.placeholder(tf.float32, )
img_overlay = overlay_patch(img, patch, i, j, alpha)
with tf.Session() as sess:
result = sess.run(img_overlay, feed_dict={
img: [[[ 1], [ 2], [ 3], [ 4]],
[[ 5], [ 6], [ 7], [ 8]],
[[ 9], [10], [11], [12]],
[[13], [14], [15], [16]]],
patch: [[[10], [20], [30]],
[[40], [50], [60]]],
i: 2, j: 1, alpha: 0.5
})
print(result[..., 0])
Output:
[[ 1. 2. 3. 4. ]
[ 5. 6. 7. 8. ]
[ 9. 10. 15.5 21. ]
[13. 27. 32.5 38. ]]
1
@Pumpkin I added the 3rd dimension thing, and analphaparameter to control the blend (0 is full original image, 1 is full patch). I still try to avoid the making the mask, I think just multiplication and addition should be faster. Here I "pre-subtract" the image from the patch before adding it so the final blending is correct.
– jdehesa
Nov 16 '18 at 15:53
@Pumpkin (I'm thinking this trick can actually be always used for slice replacement withalpha=1, at least with signed data types)
– jdehesa
Nov 16 '18 at 15:57
Nice, too bad I can't upvote again. :)
– Pumpkin
Nov 16 '18 at 16:02
add a comment |
This is one possible implementation:
import tensorflow as tf
# i and j are first row and colum
# alpha (0..1) is the intensity of the overlay
def overlay_patch(img, patch, i, j, alpha=0.5):
img_shape = tf.shape(img)
img_rows, img_cols = img_shape[0], img_shape[1]
patch_shape = tf.shape(patch)
patch_rows, patch_cols = patch_shape[0], patch_shape[1]
i_end = i + patch_rows
j_end = j + patch_cols
# Mix patch: alpha from patch, minus alpha from image
overlay = alpha * (patch - img[i:i_end, j:j_end])
# Pad patch
overlay_pad = tf.pad(overlay, [[i, img_rows - i_end], [j, img_cols - j_end], [0, 0]])
# Make final image
img_overlay = img + overlay_pad
return img_overlay
Test:
img = tf.placeholder(tf.float32, [None, None, None])
patch = tf.placeholder(tf.float32, [None, None, None])
i = tf.placeholder(tf.int32, )
j = tf.placeholder(tf.int32, )
alpha = tf.placeholder(tf.float32, )
img_overlay = overlay_patch(img, patch, i, j, alpha)
with tf.Session() as sess:
result = sess.run(img_overlay, feed_dict={
img: [[[ 1], [ 2], [ 3], [ 4]],
[[ 5], [ 6], [ 7], [ 8]],
[[ 9], [10], [11], [12]],
[[13], [14], [15], [16]]],
patch: [[[10], [20], [30]],
[[40], [50], [60]]],
i: 2, j: 1, alpha: 0.5
})
print(result[..., 0])
Output:
[[ 1. 2. 3. 4. ]
[ 5. 6. 7. 8. ]
[ 9. 10. 15.5 21. ]
[13. 27. 32.5 38. ]]
1
@Pumpkin I added the 3rd dimension thing, and analphaparameter to control the blend (0 is full original image, 1 is full patch). I still try to avoid the making the mask, I think just multiplication and addition should be faster. Here I "pre-subtract" the image from the patch before adding it so the final blending is correct.
– jdehesa
Nov 16 '18 at 15:53
@Pumpkin (I'm thinking this trick can actually be always used for slice replacement withalpha=1, at least with signed data types)
– jdehesa
Nov 16 '18 at 15:57
Nice, too bad I can't upvote again. :)
– Pumpkin
Nov 16 '18 at 16:02
add a comment |
This is one possible implementation:
import tensorflow as tf
# i and j are first row and colum
# alpha (0..1) is the intensity of the overlay
def overlay_patch(img, patch, i, j, alpha=0.5):
img_shape = tf.shape(img)
img_rows, img_cols = img_shape[0], img_shape[1]
patch_shape = tf.shape(patch)
patch_rows, patch_cols = patch_shape[0], patch_shape[1]
i_end = i + patch_rows
j_end = j + patch_cols
# Mix patch: alpha from patch, minus alpha from image
overlay = alpha * (patch - img[i:i_end, j:j_end])
# Pad patch
overlay_pad = tf.pad(overlay, [[i, img_rows - i_end], [j, img_cols - j_end], [0, 0]])
# Make final image
img_overlay = img + overlay_pad
return img_overlay
Test:
img = tf.placeholder(tf.float32, [None, None, None])
patch = tf.placeholder(tf.float32, [None, None, None])
i = tf.placeholder(tf.int32, )
j = tf.placeholder(tf.int32, )
alpha = tf.placeholder(tf.float32, )
img_overlay = overlay_patch(img, patch, i, j, alpha)
with tf.Session() as sess:
result = sess.run(img_overlay, feed_dict={
img: [[[ 1], [ 2], [ 3], [ 4]],
[[ 5], [ 6], [ 7], [ 8]],
[[ 9], [10], [11], [12]],
[[13], [14], [15], [16]]],
patch: [[[10], [20], [30]],
[[40], [50], [60]]],
i: 2, j: 1, alpha: 0.5
})
print(result[..., 0])
Output:
[[ 1. 2. 3. 4. ]
[ 5. 6. 7. 8. ]
[ 9. 10. 15.5 21. ]
[13. 27. 32.5 38. ]]
This is one possible implementation:
import tensorflow as tf
# i and j are first row and colum
# alpha (0..1) is the intensity of the overlay
def overlay_patch(img, patch, i, j, alpha=0.5):
img_shape = tf.shape(img)
img_rows, img_cols = img_shape[0], img_shape[1]
patch_shape = tf.shape(patch)
patch_rows, patch_cols = patch_shape[0], patch_shape[1]
i_end = i + patch_rows
j_end = j + patch_cols
# Mix patch: alpha from patch, minus alpha from image
overlay = alpha * (patch - img[i:i_end, j:j_end])
# Pad patch
overlay_pad = tf.pad(overlay, [[i, img_rows - i_end], [j, img_cols - j_end], [0, 0]])
# Make final image
img_overlay = img + overlay_pad
return img_overlay
Test:
img = tf.placeholder(tf.float32, [None, None, None])
patch = tf.placeholder(tf.float32, [None, None, None])
i = tf.placeholder(tf.int32, )
j = tf.placeholder(tf.int32, )
alpha = tf.placeholder(tf.float32, )
img_overlay = overlay_patch(img, patch, i, j, alpha)
with tf.Session() as sess:
result = sess.run(img_overlay, feed_dict={
img: [[[ 1], [ 2], [ 3], [ 4]],
[[ 5], [ 6], [ 7], [ 8]],
[[ 9], [10], [11], [12]],
[[13], [14], [15], [16]]],
patch: [[[10], [20], [30]],
[[40], [50], [60]]],
i: 2, j: 1, alpha: 0.5
})
print(result[..., 0])
Output:
[[ 1. 2. 3. 4. ]
[ 5. 6. 7. 8. ]
[ 9. 10. 15.5 21. ]
[13. 27. 32.5 38. ]]
edited Nov 16 '18 at 15:53
answered Nov 16 '18 at 15:13
jdehesajdehesa
23.2k43352
23.2k43352
1
@Pumpkin I added the 3rd dimension thing, and analphaparameter to control the blend (0 is full original image, 1 is full patch). I still try to avoid the making the mask, I think just multiplication and addition should be faster. Here I "pre-subtract" the image from the patch before adding it so the final blending is correct.
– jdehesa
Nov 16 '18 at 15:53
@Pumpkin (I'm thinking this trick can actually be always used for slice replacement withalpha=1, at least with signed data types)
– jdehesa
Nov 16 '18 at 15:57
Nice, too bad I can't upvote again. :)
– Pumpkin
Nov 16 '18 at 16:02
add a comment |
1
@Pumpkin I added the 3rd dimension thing, and analphaparameter to control the blend (0 is full original image, 1 is full patch). I still try to avoid the making the mask, I think just multiplication and addition should be faster. Here I "pre-subtract" the image from the patch before adding it so the final blending is correct.
– jdehesa
Nov 16 '18 at 15:53
@Pumpkin (I'm thinking this trick can actually be always used for slice replacement withalpha=1, at least with signed data types)
– jdehesa
Nov 16 '18 at 15:57
Nice, too bad I can't upvote again. :)
– Pumpkin
Nov 16 '18 at 16:02
1
1
@Pumpkin I added the 3rd dimension thing, and an
alpha parameter to control the blend (0 is full original image, 1 is full patch). I still try to avoid the making the mask, I think just multiplication and addition should be faster. Here I "pre-subtract" the image from the patch before adding it so the final blending is correct.– jdehesa
Nov 16 '18 at 15:53
@Pumpkin I added the 3rd dimension thing, and an
alpha parameter to control the blend (0 is full original image, 1 is full patch). I still try to avoid the making the mask, I think just multiplication and addition should be faster. Here I "pre-subtract" the image from the patch before adding it so the final blending is correct.– jdehesa
Nov 16 '18 at 15:53
@Pumpkin (I'm thinking this trick can actually be always used for slice replacement with
alpha=1, at least with signed data types)– jdehesa
Nov 16 '18 at 15:57
@Pumpkin (I'm thinking this trick can actually be always used for slice replacement with
alpha=1, at least with signed data types)– jdehesa
Nov 16 '18 at 15:57
Nice, too bad I can't upvote again. :)
– Pumpkin
Nov 16 '18 at 16:02
Nice, too bad I can't upvote again. :)
– Pumpkin
Nov 16 '18 at 16:02
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%2f53333756%2fhow-can-i-add-a-small-image-to-a-bigger-one-in-tensorflow%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
This is an ongoing question with TensorFlow. I have given answers to similar cases here, here, here and here (and opened an issue about the need of better support for this here). Check out if any of the answers there help you and then you can mark this as duplicate. If not, I can give a specific answer to your case here.
– jdehesa
Nov 16 '18 at 11:33
Also, I assume your actual case would be for a two-dimensional tensor? I am saying because, while in NumPy it is pretty much the same, in TensorFlow it would make a bigger difference. And would it be overlaying a single image into another, or a single image into a batch, or one batch of images into another batch of images...? And is each image a 2D tensor or 3D, with RGB channels?
– jdehesa
Nov 16 '18 at 11:35
I added a possible implementation. Actually since you are not straight out replacing, but adding to what is already there, can be done more easily just with padding.
– jdehesa
Nov 16 '18 at 15:20