Shifting an image by x pixels to left while maintaining the original shape
I want to shift an image by x pixels while maintaining the original shape. I tried the following:
import cv2
img = cv2.imread("roi.jpg")
shift = img[:,x:size[1]]
But the problem with the above approach is that, the original shape of the image is lost. How could I preserve the original shape while shifting an image by x pixels to the left.
python opencv image-processing opencv3.0
add a comment |
I want to shift an image by x pixels while maintaining the original shape. I tried the following:
import cv2
img = cv2.imread("roi.jpg")
shift = img[:,x:size[1]]
But the problem with the above approach is that, the original shape of the image is lost. How could I preserve the original shape while shifting an image by x pixels to the left.
python opencv image-processing opencv3.0
You can use Affine Transformation.
– zindarod
Jan 20 at 6:44
@zindarod I had checked this but could not really understand this on how to use it.
– Suhail Gupta
Jan 20 at 6:49
add a comment |
I want to shift an image by x pixels while maintaining the original shape. I tried the following:
import cv2
img = cv2.imread("roi.jpg")
shift = img[:,x:size[1]]
But the problem with the above approach is that, the original shape of the image is lost. How could I preserve the original shape while shifting an image by x pixels to the left.
python opencv image-processing opencv3.0
I want to shift an image by x pixels while maintaining the original shape. I tried the following:
import cv2
img = cv2.imread("roi.jpg")
shift = img[:,x:size[1]]
But the problem with the above approach is that, the original shape of the image is lost. How could I preserve the original shape while shifting an image by x pixels to the left.
python opencv image-processing opencv3.0
python opencv image-processing opencv3.0
asked Jan 20 at 6:37
Suhail GuptaSuhail Gupta
9,54450139246
9,54450139246
You can use Affine Transformation.
– zindarod
Jan 20 at 6:44
@zindarod I had checked this but could not really understand this on how to use it.
– Suhail Gupta
Jan 20 at 6:49
add a comment |
You can use Affine Transformation.
– zindarod
Jan 20 at 6:44
@zindarod I had checked this but could not really understand this on how to use it.
– Suhail Gupta
Jan 20 at 6:49
You can use Affine Transformation.
– zindarod
Jan 20 at 6:44
You can use Affine Transformation.
– zindarod
Jan 20 at 6:44
@zindarod I had checked this but could not really understand this on how to use it.
– Suhail Gupta
Jan 20 at 6:49
@zindarod I had checked this but could not really understand this on how to use it.
– Suhail Gupta
Jan 20 at 6:49
add a comment |
1 Answer
1
active
oldest
votes
In image processing, this thing is referred to as Translation of image.
The original image:

import cv2
import numpy as np
# Read image
img = cv2.imread("roi.jpg")
# The number of pixels
num_rows, num_cols = img.shape[:2]
# Creating a translation matrix
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
# Image translation
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
#cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
This will give you:

But we want something like this:

Translation basically means that we are shifting the image by adding/subtracting the X and Y coordinates. In order to do this, we need to create a transformation matrix, as shown as follows:

Here, the tx and ty values are the X and Y translation values, that is, the image will be moved by X units towards the right, and by Y units downwards.
So once we create a matrix like this, we can use the function, warpAffine, to apply to our image.
The third argument in warpAffine refers to the number of rows and columns in the resulting image. Since the number of rows and columns is the same as the original image, the resultant image is going to get cropped. The reason for this is because we didn't have enough space in the output when we applied the translation matrix. To avoid cropping, we can do something like this:
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
And this will result in:

Remember this image is resized while being uploaded here, don't worry, this is your desired result.
Moreover, if we want to move the image in the middle of a bigger image frame; we can do something like this by carrying out the following:
num_rows, num_cols = img.shape[:2]
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
translation_matrix = np.float32([ [1,0,-30], [0,1,-50] ])
img_translation = cv2.warpAffine(img_translation, translation_matrix, (num_cols + 70 + 30, num_rows + 110 + 50))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
Which gives you the output as:

Why are you taking tx and ty as 70 and 110?
– Suhail Gupta
Jan 20 at 7:22
How am I going to shift the image to left?
– Suhail Gupta
Jan 20 at 7:23
Well, 70 and 110 was just used for demonstration purpose really, nothing specific. Regarding your shifting image to left, well padding your numpy array with zeros in lower and right side would be enough. I hope you know about padding.
– Amit Amola
Jan 20 at 7:58
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%2f54274185%2fshifting-an-image-by-x-pixels-to-left-while-maintaining-the-original-shape%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
In image processing, this thing is referred to as Translation of image.
The original image:

import cv2
import numpy as np
# Read image
img = cv2.imread("roi.jpg")
# The number of pixels
num_rows, num_cols = img.shape[:2]
# Creating a translation matrix
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
# Image translation
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
#cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
This will give you:

But we want something like this:

Translation basically means that we are shifting the image by adding/subtracting the X and Y coordinates. In order to do this, we need to create a transformation matrix, as shown as follows:

Here, the tx and ty values are the X and Y translation values, that is, the image will be moved by X units towards the right, and by Y units downwards.
So once we create a matrix like this, we can use the function, warpAffine, to apply to our image.
The third argument in warpAffine refers to the number of rows and columns in the resulting image. Since the number of rows and columns is the same as the original image, the resultant image is going to get cropped. The reason for this is because we didn't have enough space in the output when we applied the translation matrix. To avoid cropping, we can do something like this:
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
And this will result in:

Remember this image is resized while being uploaded here, don't worry, this is your desired result.
Moreover, if we want to move the image in the middle of a bigger image frame; we can do something like this by carrying out the following:
num_rows, num_cols = img.shape[:2]
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
translation_matrix = np.float32([ [1,0,-30], [0,1,-50] ])
img_translation = cv2.warpAffine(img_translation, translation_matrix, (num_cols + 70 + 30, num_rows + 110 + 50))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
Which gives you the output as:

Why are you taking tx and ty as 70 and 110?
– Suhail Gupta
Jan 20 at 7:22
How am I going to shift the image to left?
– Suhail Gupta
Jan 20 at 7:23
Well, 70 and 110 was just used for demonstration purpose really, nothing specific. Regarding your shifting image to left, well padding your numpy array with zeros in lower and right side would be enough. I hope you know about padding.
– Amit Amola
Jan 20 at 7:58
add a comment |
In image processing, this thing is referred to as Translation of image.
The original image:

import cv2
import numpy as np
# Read image
img = cv2.imread("roi.jpg")
# The number of pixels
num_rows, num_cols = img.shape[:2]
# Creating a translation matrix
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
# Image translation
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
#cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
This will give you:

But we want something like this:

Translation basically means that we are shifting the image by adding/subtracting the X and Y coordinates. In order to do this, we need to create a transformation matrix, as shown as follows:

Here, the tx and ty values are the X and Y translation values, that is, the image will be moved by X units towards the right, and by Y units downwards.
So once we create a matrix like this, we can use the function, warpAffine, to apply to our image.
The third argument in warpAffine refers to the number of rows and columns in the resulting image. Since the number of rows and columns is the same as the original image, the resultant image is going to get cropped. The reason for this is because we didn't have enough space in the output when we applied the translation matrix. To avoid cropping, we can do something like this:
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
And this will result in:

Remember this image is resized while being uploaded here, don't worry, this is your desired result.
Moreover, if we want to move the image in the middle of a bigger image frame; we can do something like this by carrying out the following:
num_rows, num_cols = img.shape[:2]
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
translation_matrix = np.float32([ [1,0,-30], [0,1,-50] ])
img_translation = cv2.warpAffine(img_translation, translation_matrix, (num_cols + 70 + 30, num_rows + 110 + 50))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
Which gives you the output as:

Why are you taking tx and ty as 70 and 110?
– Suhail Gupta
Jan 20 at 7:22
How am I going to shift the image to left?
– Suhail Gupta
Jan 20 at 7:23
Well, 70 and 110 was just used for demonstration purpose really, nothing specific. Regarding your shifting image to left, well padding your numpy array with zeros in lower and right side would be enough. I hope you know about padding.
– Amit Amola
Jan 20 at 7:58
add a comment |
In image processing, this thing is referred to as Translation of image.
The original image:

import cv2
import numpy as np
# Read image
img = cv2.imread("roi.jpg")
# The number of pixels
num_rows, num_cols = img.shape[:2]
# Creating a translation matrix
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
# Image translation
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
#cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
This will give you:

But we want something like this:

Translation basically means that we are shifting the image by adding/subtracting the X and Y coordinates. In order to do this, we need to create a transformation matrix, as shown as follows:

Here, the tx and ty values are the X and Y translation values, that is, the image will be moved by X units towards the right, and by Y units downwards.
So once we create a matrix like this, we can use the function, warpAffine, to apply to our image.
The third argument in warpAffine refers to the number of rows and columns in the resulting image. Since the number of rows and columns is the same as the original image, the resultant image is going to get cropped. The reason for this is because we didn't have enough space in the output when we applied the translation matrix. To avoid cropping, we can do something like this:
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
And this will result in:

Remember this image is resized while being uploaded here, don't worry, this is your desired result.
Moreover, if we want to move the image in the middle of a bigger image frame; we can do something like this by carrying out the following:
num_rows, num_cols = img.shape[:2]
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
translation_matrix = np.float32([ [1,0,-30], [0,1,-50] ])
img_translation = cv2.warpAffine(img_translation, translation_matrix, (num_cols + 70 + 30, num_rows + 110 + 50))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
Which gives you the output as:

In image processing, this thing is referred to as Translation of image.
The original image:

import cv2
import numpy as np
# Read image
img = cv2.imread("roi.jpg")
# The number of pixels
num_rows, num_cols = img.shape[:2]
# Creating a translation matrix
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
# Image translation
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
#cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
This will give you:

But we want something like this:

Translation basically means that we are shifting the image by adding/subtracting the X and Y coordinates. In order to do this, we need to create a transformation matrix, as shown as follows:

Here, the tx and ty values are the X and Y translation values, that is, the image will be moved by X units towards the right, and by Y units downwards.
So once we create a matrix like this, we can use the function, warpAffine, to apply to our image.
The third argument in warpAffine refers to the number of rows and columns in the resulting image. Since the number of rows and columns is the same as the original image, the resultant image is going to get cropped. The reason for this is because we didn't have enough space in the output when we applied the translation matrix. To avoid cropping, we can do something like this:
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
And this will result in:

Remember this image is resized while being uploaded here, don't worry, this is your desired result.
Moreover, if we want to move the image in the middle of a bigger image frame; we can do something like this by carrying out the following:
num_rows, num_cols = img.shape[:2]
translation_matrix = np.float32([ [1,0,70], [0,1,110] ])
img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110))
translation_matrix = np.float32([ [1,0,-30], [0,1,-50] ])
img_translation = cv2.warpAffine(img_translation, translation_matrix, (num_cols + 70 + 30, num_rows + 110 + 50))
cv2.namedWindow('Translation', cv2.WINDOW_NORMAL)
cv2.imshow('Translation', img_translation)
cv2.waitKey(0)
cv2.destroyAllWindows()
Which gives you the output as:

edited Jan 20 at 6:56
answered Jan 20 at 6:45
Amit AmolaAmit Amola
434517
434517
Why are you taking tx and ty as 70 and 110?
– Suhail Gupta
Jan 20 at 7:22
How am I going to shift the image to left?
– Suhail Gupta
Jan 20 at 7:23
Well, 70 and 110 was just used for demonstration purpose really, nothing specific. Regarding your shifting image to left, well padding your numpy array with zeros in lower and right side would be enough. I hope you know about padding.
– Amit Amola
Jan 20 at 7:58
add a comment |
Why are you taking tx and ty as 70 and 110?
– Suhail Gupta
Jan 20 at 7:22
How am I going to shift the image to left?
– Suhail Gupta
Jan 20 at 7:23
Well, 70 and 110 was just used for demonstration purpose really, nothing specific. Regarding your shifting image to left, well padding your numpy array with zeros in lower and right side would be enough. I hope you know about padding.
– Amit Amola
Jan 20 at 7:58
Why are you taking tx and ty as 70 and 110?
– Suhail Gupta
Jan 20 at 7:22
Why are you taking tx and ty as 70 and 110?
– Suhail Gupta
Jan 20 at 7:22
How am I going to shift the image to left?
– Suhail Gupta
Jan 20 at 7:23
How am I going to shift the image to left?
– Suhail Gupta
Jan 20 at 7:23
Well, 70 and 110 was just used for demonstration purpose really, nothing specific. Regarding your shifting image to left, well padding your numpy array with zeros in lower and right side would be enough. I hope you know about padding.
– Amit Amola
Jan 20 at 7:58
Well, 70 and 110 was just used for demonstration purpose really, nothing specific. Regarding your shifting image to left, well padding your numpy array with zeros in lower and right side would be enough. I hope you know about padding.
– Amit Amola
Jan 20 at 7:58
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%2f54274185%2fshifting-an-image-by-x-pixels-to-left-while-maintaining-the-original-shape%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
You can use Affine Transformation.
– zindarod
Jan 20 at 6:44
@zindarod I had checked this but could not really understand this on how to use it.
– Suhail Gupta
Jan 20 at 6:49