How to swap blue and green channel in an image using OpenCV












6















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
The original Image



Manual Operation
The manual operation



COLOR_BGR2RGB
The cv2.COLOR_BGR2RGB operation










share|improve this question



























    6















    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
    The original Image



    Manual Operation
    The manual operation



    COLOR_BGR2RGB
    The cv2.COLOR_BGR2RGB operation










    share|improve this question

























      6












      6








      6


      0






      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
      The original Image



      Manual Operation
      The manual operation



      COLOR_BGR2RGB
      The cv2.COLOR_BGR2RGB operation










      share|improve this question














      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
      The original Image



      Manual Operation
      The manual operation



      COLOR_BGR2RGB
      The cv2.COLOR_BGR2RGB operation







      python opencv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 23 '16 at 6:37









      Shashwat VermaShashwat Verma

      8018




      8018
























          1 Answer
          1






          active

          oldest

          votes


















          8














          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]]]





          share|improve this answer


























          • Worked! Thanks :)

            – Shashwat Verma
            Jul 23 '16 at 18:09











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          8














          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]]]





          share|improve this answer


























          • Worked! Thanks :)

            – Shashwat Verma
            Jul 23 '16 at 18:09
















          8














          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]]]





          share|improve this answer


























          • Worked! Thanks :)

            – Shashwat Verma
            Jul 23 '16 at 18:09














          8












          8








          8







          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]]]





          share|improve this answer















          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]]]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith