Some error solving tips for seamless cloning and wrap affine transformation












0














I am trying to make an hair swapping application in python. I have successfully, extract out hair. However, it has not given satisfied result. But for the final part, I need to take hair image from sample head and put that in sample model image. For that, I use seamless cloning. Here's my code for it and final image:



import cv2
import numpy as np

# Read images : src image will be cloned into dst
im = cv2.imread("source.jpg")
obj = cv2.imread("hair.jpg")
# Create an all white mask
mask = 255 * np.ones(obj.shape, obj.dtype)

# The location of the center of the src in the dst
width, height, channels = im.shape
center = (250, 115)

# Seamlessly clone src into dst and put the results in output
mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)

# Write results
cv2.imwrite("normal-clone.jpg", mixed_clone)
cv2.waitKey(0)
cv2.destroyAllWindows()


And from above, I get following result:
Source ImageHair ImageSeamless cloned



I know its not good looking, but the main problem here is when I use affine transformation for scaling and rotation purpose and then seamless cloning using following code:



import cv2
import numpy as np

# Read images : src image will be cloned into dst
im = cv2.imread("source.jpg")
obj = cv2.imread("hair.jpg")

#Wrap Affine transformation
rows,cols,ch = obj.shape

pts1 = np.float32([[160,199],[250,54],[339,200]])
pts2 = np.float32([[151,196],[250,33],[347,196]])

M = cv2.getAffineTransform(pts1,pts2)

dst = cv2.warpAffine(obj,M,(cols,rows))

# Create an all white mask
mask = 255 * np.ones(dst.shape, dst.dtype)

# The location of the center of the src in the dst
width, height, channels = im.shape
center = (250, 115)

# Seamlessly clone src into dst and put the results in output
mixed_clone = cv2.seamlessClone(dst, im, mask, center, cv2.MIXED_CLONE)

# Write results
cv2.imshow("normal-clone", mixed_clone)
cv2.waitKey(0)
cv2.destroyAllWindows()


And, I get following result:



Final Image



I have another code written for those coordinate points generation. I will merge them later, so that, I do not have to input those coordinate points manually.
But for now, I want to know, why affine transformation affecting seamless cloning. What am I doing wrong??



Or, any solution for hair swapping will be appreciated.










share|improve this question





























    0














    I am trying to make an hair swapping application in python. I have successfully, extract out hair. However, it has not given satisfied result. But for the final part, I need to take hair image from sample head and put that in sample model image. For that, I use seamless cloning. Here's my code for it and final image:



    import cv2
    import numpy as np

    # Read images : src image will be cloned into dst
    im = cv2.imread("source.jpg")
    obj = cv2.imread("hair.jpg")
    # Create an all white mask
    mask = 255 * np.ones(obj.shape, obj.dtype)

    # The location of the center of the src in the dst
    width, height, channels = im.shape
    center = (250, 115)

    # Seamlessly clone src into dst and put the results in output
    mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)

    # Write results
    cv2.imwrite("normal-clone.jpg", mixed_clone)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


    And from above, I get following result:
    Source ImageHair ImageSeamless cloned



    I know its not good looking, but the main problem here is when I use affine transformation for scaling and rotation purpose and then seamless cloning using following code:



    import cv2
    import numpy as np

    # Read images : src image will be cloned into dst
    im = cv2.imread("source.jpg")
    obj = cv2.imread("hair.jpg")

    #Wrap Affine transformation
    rows,cols,ch = obj.shape

    pts1 = np.float32([[160,199],[250,54],[339,200]])
    pts2 = np.float32([[151,196],[250,33],[347,196]])

    M = cv2.getAffineTransform(pts1,pts2)

    dst = cv2.warpAffine(obj,M,(cols,rows))

    # Create an all white mask
    mask = 255 * np.ones(dst.shape, dst.dtype)

    # The location of the center of the src in the dst
    width, height, channels = im.shape
    center = (250, 115)

    # Seamlessly clone src into dst and put the results in output
    mixed_clone = cv2.seamlessClone(dst, im, mask, center, cv2.MIXED_CLONE)

    # Write results
    cv2.imshow("normal-clone", mixed_clone)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


    And, I get following result:



    Final Image



    I have another code written for those coordinate points generation. I will merge them later, so that, I do not have to input those coordinate points manually.
    But for now, I want to know, why affine transformation affecting seamless cloning. What am I doing wrong??



    Or, any solution for hair swapping will be appreciated.










    share|improve this question



























      0












      0








      0







      I am trying to make an hair swapping application in python. I have successfully, extract out hair. However, it has not given satisfied result. But for the final part, I need to take hair image from sample head and put that in sample model image. For that, I use seamless cloning. Here's my code for it and final image:



      import cv2
      import numpy as np

      # Read images : src image will be cloned into dst
      im = cv2.imread("source.jpg")
      obj = cv2.imread("hair.jpg")
      # Create an all white mask
      mask = 255 * np.ones(obj.shape, obj.dtype)

      # The location of the center of the src in the dst
      width, height, channels = im.shape
      center = (250, 115)

      # Seamlessly clone src into dst and put the results in output
      mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)

      # Write results
      cv2.imwrite("normal-clone.jpg", mixed_clone)
      cv2.waitKey(0)
      cv2.destroyAllWindows()


      And from above, I get following result:
      Source ImageHair ImageSeamless cloned



      I know its not good looking, but the main problem here is when I use affine transformation for scaling and rotation purpose and then seamless cloning using following code:



      import cv2
      import numpy as np

      # Read images : src image will be cloned into dst
      im = cv2.imread("source.jpg")
      obj = cv2.imread("hair.jpg")

      #Wrap Affine transformation
      rows,cols,ch = obj.shape

      pts1 = np.float32([[160,199],[250,54],[339,200]])
      pts2 = np.float32([[151,196],[250,33],[347,196]])

      M = cv2.getAffineTransform(pts1,pts2)

      dst = cv2.warpAffine(obj,M,(cols,rows))

      # Create an all white mask
      mask = 255 * np.ones(dst.shape, dst.dtype)

      # The location of the center of the src in the dst
      width, height, channels = im.shape
      center = (250, 115)

      # Seamlessly clone src into dst and put the results in output
      mixed_clone = cv2.seamlessClone(dst, im, mask, center, cv2.MIXED_CLONE)

      # Write results
      cv2.imshow("normal-clone", mixed_clone)
      cv2.waitKey(0)
      cv2.destroyAllWindows()


      And, I get following result:



      Final Image



      I have another code written for those coordinate points generation. I will merge them later, so that, I do not have to input those coordinate points manually.
      But for now, I want to know, why affine transformation affecting seamless cloning. What am I doing wrong??



      Or, any solution for hair swapping will be appreciated.










      share|improve this question















      I am trying to make an hair swapping application in python. I have successfully, extract out hair. However, it has not given satisfied result. But for the final part, I need to take hair image from sample head and put that in sample model image. For that, I use seamless cloning. Here's my code for it and final image:



      import cv2
      import numpy as np

      # Read images : src image will be cloned into dst
      im = cv2.imread("source.jpg")
      obj = cv2.imread("hair.jpg")
      # Create an all white mask
      mask = 255 * np.ones(obj.shape, obj.dtype)

      # The location of the center of the src in the dst
      width, height, channels = im.shape
      center = (250, 115)

      # Seamlessly clone src into dst and put the results in output
      mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)

      # Write results
      cv2.imwrite("normal-clone.jpg", mixed_clone)
      cv2.waitKey(0)
      cv2.destroyAllWindows()


      And from above, I get following result:
      Source ImageHair ImageSeamless cloned



      I know its not good looking, but the main problem here is when I use affine transformation for scaling and rotation purpose and then seamless cloning using following code:



      import cv2
      import numpy as np

      # Read images : src image will be cloned into dst
      im = cv2.imread("source.jpg")
      obj = cv2.imread("hair.jpg")

      #Wrap Affine transformation
      rows,cols,ch = obj.shape

      pts1 = np.float32([[160,199],[250,54],[339,200]])
      pts2 = np.float32([[151,196],[250,33],[347,196]])

      M = cv2.getAffineTransform(pts1,pts2)

      dst = cv2.warpAffine(obj,M,(cols,rows))

      # Create an all white mask
      mask = 255 * np.ones(dst.shape, dst.dtype)

      # The location of the center of the src in the dst
      width, height, channels = im.shape
      center = (250, 115)

      # Seamlessly clone src into dst and put the results in output
      mixed_clone = cv2.seamlessClone(dst, im, mask, center, cv2.MIXED_CLONE)

      # Write results
      cv2.imshow("normal-clone", mixed_clone)
      cv2.waitKey(0)
      cv2.destroyAllWindows()


      And, I get following result:



      Final Image



      I have another code written for those coordinate points generation. I will merge them later, so that, I do not have to input those coordinate points manually.
      But for now, I want to know, why affine transformation affecting seamless cloning. What am I doing wrong??



      Or, any solution for hair swapping will be appreciated.







      python numpy opencv dlib affinetransform






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 '18 at 17:31









      xeros

      455




      455










      asked Dec 27 '18 at 15:38









      Gkisi27

      464




      464





























          active

          oldest

          votes











          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%2f53947448%2fsome-error-solving-tips-for-seamless-cloning-and-wrap-affine-transformation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.





          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947448%2fsome-error-solving-tips-for-seamless-cloning-and-wrap-affine-transformation%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

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'