Inverse mapping with bilinear interpolation on an image












1












$begingroup$


The following is my solution for an inverse mapping with bilinear interpolation on an image. The original image is img and newmatrix is the transformed image. invRot is the inverse transformation matrix.



How can I optimize the nested for loops (or remove them altogether) to give me a better time complexity for large values of row and col?



newmatrix = np.zeros((row, col, 3), np.uint8)
for r in range(row):
for c in range(col):
if offset > 0:
offset = -1 * offset
pt = np.array([r+offset,c,1]) #Adjust the offset.
newpt = np.matmul(invRot, pt) #Reverse map by reverse rotation and pick up color.

#Check the bounds of the inverse pts we got and if they lie in the original image,
#then copy the color from that original pt to the new matrix/image.
if (newpt[0] >= 0 and newpt[0] < (yLen - 1) and newpt[1] >= 0 and newpt[1] < (xLen - 1)):
x = np.asarray(newpt[1])
y = np.asarray(newpt[0])

x0 = np.floor(x).astype(int)
x1 = x0 + 1
y0 = np.floor(y).astype(int)
y1 = y0 + 1

Ia = img[y0, x0]
Ib = img[y1, x0]
Ic = img[y0, x1]
Id = img[y1, x1]

color1 = (x1-x) * (y1-y) * Ia
color2 = (x1-x) * (y-y0) * Ib
color3 = (x-x0) * (y1-y) * Ic
color4 = (x-x0) * (y-y0) * Id

weightedAvgColor = color1 + color2 + color3 + color4
newmatrix[r][c] = weightedAvgColor









share|improve this question









New contributor




The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    1












    $begingroup$


    The following is my solution for an inverse mapping with bilinear interpolation on an image. The original image is img and newmatrix is the transformed image. invRot is the inverse transformation matrix.



    How can I optimize the nested for loops (or remove them altogether) to give me a better time complexity for large values of row and col?



    newmatrix = np.zeros((row, col, 3), np.uint8)
    for r in range(row):
    for c in range(col):
    if offset > 0:
    offset = -1 * offset
    pt = np.array([r+offset,c,1]) #Adjust the offset.
    newpt = np.matmul(invRot, pt) #Reverse map by reverse rotation and pick up color.

    #Check the bounds of the inverse pts we got and if they lie in the original image,
    #then copy the color from that original pt to the new matrix/image.
    if (newpt[0] >= 0 and newpt[0] < (yLen - 1) and newpt[1] >= 0 and newpt[1] < (xLen - 1)):
    x = np.asarray(newpt[1])
    y = np.asarray(newpt[0])

    x0 = np.floor(x).astype(int)
    x1 = x0 + 1
    y0 = np.floor(y).astype(int)
    y1 = y0 + 1

    Ia = img[y0, x0]
    Ib = img[y1, x0]
    Ic = img[y0, x1]
    Id = img[y1, x1]

    color1 = (x1-x) * (y1-y) * Ia
    color2 = (x1-x) * (y-y0) * Ib
    color3 = (x-x0) * (y1-y) * Ic
    color4 = (x-x0) * (y-y0) * Id

    weightedAvgColor = color1 + color2 + color3 + color4
    newmatrix[r][c] = weightedAvgColor









    share|improve this question









    New contributor




    The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      1












      1








      1





      $begingroup$


      The following is my solution for an inverse mapping with bilinear interpolation on an image. The original image is img and newmatrix is the transformed image. invRot is the inverse transformation matrix.



      How can I optimize the nested for loops (or remove them altogether) to give me a better time complexity for large values of row and col?



      newmatrix = np.zeros((row, col, 3), np.uint8)
      for r in range(row):
      for c in range(col):
      if offset > 0:
      offset = -1 * offset
      pt = np.array([r+offset,c,1]) #Adjust the offset.
      newpt = np.matmul(invRot, pt) #Reverse map by reverse rotation and pick up color.

      #Check the bounds of the inverse pts we got and if they lie in the original image,
      #then copy the color from that original pt to the new matrix/image.
      if (newpt[0] >= 0 and newpt[0] < (yLen - 1) and newpt[1] >= 0 and newpt[1] < (xLen - 1)):
      x = np.asarray(newpt[1])
      y = np.asarray(newpt[0])

      x0 = np.floor(x).astype(int)
      x1 = x0 + 1
      y0 = np.floor(y).astype(int)
      y1 = y0 + 1

      Ia = img[y0, x0]
      Ib = img[y1, x0]
      Ic = img[y0, x1]
      Id = img[y1, x1]

      color1 = (x1-x) * (y1-y) * Ia
      color2 = (x1-x) * (y-y0) * Ib
      color3 = (x-x0) * (y1-y) * Ic
      color4 = (x-x0) * (y-y0) * Id

      weightedAvgColor = color1 + color2 + color3 + color4
      newmatrix[r][c] = weightedAvgColor









      share|improve this question









      New contributor




      The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      The following is my solution for an inverse mapping with bilinear interpolation on an image. The original image is img and newmatrix is the transformed image. invRot is the inverse transformation matrix.



      How can I optimize the nested for loops (or remove them altogether) to give me a better time complexity for large values of row and col?



      newmatrix = np.zeros((row, col, 3), np.uint8)
      for r in range(row):
      for c in range(col):
      if offset > 0:
      offset = -1 * offset
      pt = np.array([r+offset,c,1]) #Adjust the offset.
      newpt = np.matmul(invRot, pt) #Reverse map by reverse rotation and pick up color.

      #Check the bounds of the inverse pts we got and if they lie in the original image,
      #then copy the color from that original pt to the new matrix/image.
      if (newpt[0] >= 0 and newpt[0] < (yLen - 1) and newpt[1] >= 0 and newpt[1] < (xLen - 1)):
      x = np.asarray(newpt[1])
      y = np.asarray(newpt[0])

      x0 = np.floor(x).astype(int)
      x1 = x0 + 1
      y0 = np.floor(y).astype(int)
      y1 = y0 + 1

      Ia = img[y0, x0]
      Ib = img[y1, x0]
      Ic = img[y0, x1]
      Id = img[y1, x1]

      color1 = (x1-x) * (y1-y) * Ia
      color2 = (x1-x) * (y-y0) * Ib
      color3 = (x-x0) * (y1-y) * Ic
      color4 = (x-x0) * (y-y0) * Id

      weightedAvgColor = color1 + color2 + color3 + color4
      newmatrix[r][c] = weightedAvgColor






      python performance image matrix numpy






      share|improve this question









      New contributor




      The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 11 mins ago









      200_success

      129k15153415




      129k15153415






      New contributor




      The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 10 hours ago









      The White WolfThe White Wolf

      61




      61




      New contributor




      The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      The White Wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          The White Wolf is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213533%2finverse-mapping-with-bilinear-interpolation-on-an-image%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          The White Wolf is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          The White Wolf is a new contributor. Be nice, and check out our Code of Conduct.













          The White Wolf is a new contributor. Be nice, and check out our Code of Conduct.












          The White Wolf is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f213533%2finverse-mapping-with-bilinear-interpolation-on-an-image%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

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар