How to swap blue and green channel in an image using OpenCV
I am facing a little bit of problem in swapping the channels (specifically red and blue) of an image. I am using Opencv 3.0.0 and Python 2.7.12. Following is my code for swapping the channels
import cv2
img = cv2.imread("input/car1.jpg")
#The obvious approach
Cimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#Manual Approach
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
cv2.imshow("frame",Cimg)
cv2.imshow("frame2", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I am unable to figure out why the same image undergoing through the same(probably) operation is giving two different outputs. Can someone throw some light on what's going wrong?
Thanks!
Original Image
Manual Operation
COLOR_BGR2RGB
python opencv
add a comment |
I am facing a little bit of problem in swapping the channels (specifically red and blue) of an image. I am using Opencv 3.0.0 and Python 2.7.12. Following is my code for swapping the channels
import cv2
img = cv2.imread("input/car1.jpg")
#The obvious approach
Cimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#Manual Approach
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
cv2.imshow("frame",Cimg)
cv2.imshow("frame2", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I am unable to figure out why the same image undergoing through the same(probably) operation is giving two different outputs. Can someone throw some light on what's going wrong?
Thanks!
Original Image
Manual Operation
COLOR_BGR2RGB
python opencv
add a comment |
I am facing a little bit of problem in swapping the channels (specifically red and blue) of an image. I am using Opencv 3.0.0 and Python 2.7.12. Following is my code for swapping the channels
import cv2
img = cv2.imread("input/car1.jpg")
#The obvious approach
Cimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#Manual Approach
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
cv2.imshow("frame",Cimg)
cv2.imshow("frame2", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I am unable to figure out why the same image undergoing through the same(probably) operation is giving two different outputs. Can someone throw some light on what's going wrong?
Thanks!
Original Image
Manual Operation
COLOR_BGR2RGB
python opencv
I am facing a little bit of problem in swapping the channels (specifically red and blue) of an image. I am using Opencv 3.0.0 and Python 2.7.12. Following is my code for swapping the channels
import cv2
img = cv2.imread("input/car1.jpg")
#The obvious approach
Cimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#Manual Approach
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
cv2.imshow("frame",Cimg)
cv2.imshow("frame2", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I am unable to figure out why the same image undergoing through the same(probably) operation is giving two different outputs. Can someone throw some light on what's going wrong?
Thanks!
Original Image
Manual Operation
COLOR_BGR2RGB
python opencv
python opencv
asked Jul 23 '16 at 6:37


Shashwat VermaShashwat Verma
8018
8018
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
red
and blue
are just views of your image. When you do img[:,:,0] = red
this changes img
but also blue
which is just a view (basically just a reference to the sub-array img[:,:,0]
) not a copy, so you loose the original blue channel values. Basically what you assume is a temp copy just is not. Add .copy()
and it will work.
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2].copy()
blue = img[:,:,0].copy()
img[:,:,0] = red
img[:,:,2] = blue
print("with copy:n", img)
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
print("without copy:n",img)
results:
with copy:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
without copy:
[[[ 2 1 2]
[ 5 4 5]
[ 8 7 8]]
[[11 10 11]
[14 13 14]
[17 16 17]]
[[20 19 20]
[23 22 23]
[26 25 26]]]
Note: you actually only need 1 temp copy of 1 channel.
Or you could also simply do img[:,:,::-1]
this will create a view again but with swapped channels, img
will stay unchanged, unless you reassign it:
img = np.arange(27).reshape((3,3,3))
print(img[:,:,::-1])
print(img)
img = img[:,:,::-1]
print(img)
results:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
Worked! Thanks :)
– Shashwat Verma
Jul 23 '16 at 18:09
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%2f38538952%2fhow-to-swap-blue-and-green-channel-in-an-image-using-opencv%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
red
and blue
are just views of your image. When you do img[:,:,0] = red
this changes img
but also blue
which is just a view (basically just a reference to the sub-array img[:,:,0]
) not a copy, so you loose the original blue channel values. Basically what you assume is a temp copy just is not. Add .copy()
and it will work.
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2].copy()
blue = img[:,:,0].copy()
img[:,:,0] = red
img[:,:,2] = blue
print("with copy:n", img)
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
print("without copy:n",img)
results:
with copy:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
without copy:
[[[ 2 1 2]
[ 5 4 5]
[ 8 7 8]]
[[11 10 11]
[14 13 14]
[17 16 17]]
[[20 19 20]
[23 22 23]
[26 25 26]]]
Note: you actually only need 1 temp copy of 1 channel.
Or you could also simply do img[:,:,::-1]
this will create a view again but with swapped channels, img
will stay unchanged, unless you reassign it:
img = np.arange(27).reshape((3,3,3))
print(img[:,:,::-1])
print(img)
img = img[:,:,::-1]
print(img)
results:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
Worked! Thanks :)
– Shashwat Verma
Jul 23 '16 at 18:09
add a comment |
red
and blue
are just views of your image. When you do img[:,:,0] = red
this changes img
but also blue
which is just a view (basically just a reference to the sub-array img[:,:,0]
) not a copy, so you loose the original blue channel values. Basically what you assume is a temp copy just is not. Add .copy()
and it will work.
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2].copy()
blue = img[:,:,0].copy()
img[:,:,0] = red
img[:,:,2] = blue
print("with copy:n", img)
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
print("without copy:n",img)
results:
with copy:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
without copy:
[[[ 2 1 2]
[ 5 4 5]
[ 8 7 8]]
[[11 10 11]
[14 13 14]
[17 16 17]]
[[20 19 20]
[23 22 23]
[26 25 26]]]
Note: you actually only need 1 temp copy of 1 channel.
Or you could also simply do img[:,:,::-1]
this will create a view again but with swapped channels, img
will stay unchanged, unless you reassign it:
img = np.arange(27).reshape((3,3,3))
print(img[:,:,::-1])
print(img)
img = img[:,:,::-1]
print(img)
results:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
Worked! Thanks :)
– Shashwat Verma
Jul 23 '16 at 18:09
add a comment |
red
and blue
are just views of your image. When you do img[:,:,0] = red
this changes img
but also blue
which is just a view (basically just a reference to the sub-array img[:,:,0]
) not a copy, so you loose the original blue channel values. Basically what you assume is a temp copy just is not. Add .copy()
and it will work.
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2].copy()
blue = img[:,:,0].copy()
img[:,:,0] = red
img[:,:,2] = blue
print("with copy:n", img)
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
print("without copy:n",img)
results:
with copy:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
without copy:
[[[ 2 1 2]
[ 5 4 5]
[ 8 7 8]]
[[11 10 11]
[14 13 14]
[17 16 17]]
[[20 19 20]
[23 22 23]
[26 25 26]]]
Note: you actually only need 1 temp copy of 1 channel.
Or you could also simply do img[:,:,::-1]
this will create a view again but with swapped channels, img
will stay unchanged, unless you reassign it:
img = np.arange(27).reshape((3,3,3))
print(img[:,:,::-1])
print(img)
img = img[:,:,::-1]
print(img)
results:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
red
and blue
are just views of your image. When you do img[:,:,0] = red
this changes img
but also blue
which is just a view (basically just a reference to the sub-array img[:,:,0]
) not a copy, so you loose the original blue channel values. Basically what you assume is a temp copy just is not. Add .copy()
and it will work.
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2].copy()
blue = img[:,:,0].copy()
img[:,:,0] = red
img[:,:,2] = blue
print("with copy:n", img)
img = np.arange(27).reshape((3,3,3))
red = img[:,:,2]
blue = img[:,:,0]
img[:,:,0] = red
img[:,:,2] = blue
print("without copy:n",img)
results:
with copy:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
without copy:
[[[ 2 1 2]
[ 5 4 5]
[ 8 7 8]]
[[11 10 11]
[14 13 14]
[17 16 17]]
[[20 19 20]
[23 22 23]
[26 25 26]]]
Note: you actually only need 1 temp copy of 1 channel.
Or you could also simply do img[:,:,::-1]
this will create a view again but with swapped channels, img
will stay unchanged, unless you reassign it:
img = np.arange(27).reshape((3,3,3))
print(img[:,:,::-1])
print(img)
img = img[:,:,::-1]
print(img)
results:
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
[[[ 2 1 0]
[ 5 4 3]
[ 8 7 6]]
[[11 10 9]
[14 13 12]
[17 16 15]]
[[20 19 18]
[23 22 21]
[26 25 24]]]
edited Jan 1 at 22:58
answered Jul 23 '16 at 6:50
JulienJulien
7,70831637
7,70831637
Worked! Thanks :)
– Shashwat Verma
Jul 23 '16 at 18:09
add a comment |
Worked! Thanks :)
– Shashwat Verma
Jul 23 '16 at 18:09
Worked! Thanks :)
– Shashwat Verma
Jul 23 '16 at 18:09
Worked! Thanks :)
– Shashwat Verma
Jul 23 '16 at 18:09
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%2f38538952%2fhow-to-swap-blue-and-green-channel-in-an-image-using-opencv%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