How to fit image right in homemade rotation algorithm in python











up vote
0
down vote

favorite












I have made this algorithm myself based on how to rotate an image.



import cv2
import math
import numpy as np

class rotation:
angle = 60.0
x = 100
y = 100
img = cv2.imread('FNAF.png',0)
width,height = img.shape
def showImage(name, self):
cv2.imshow(name, self.img)
def printWidthHeight(self):
print(self.width)
print(self.height)
def rotateImage(self):
ForwardMap = np.zeros((self.width,self.height),dtype="uint8")
BackwardMap = np.zeros((self.width,self.height),dtype="uint8")

for i in range(self.width):
for j in range(self.height):
# forward mapping
for_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) - (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
for_y = (i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
for_x = int(for_x)
for_y = int(for_y)
# backward mapping should change the forward mapping to the original image
back_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) + (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
back_y = -(i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
back_x = int(back_x)
back_y = int(back_y)
if for_x in range(self.width) and for_y in range(self.height):
ForwardMap[i, j] = self.img[for_x, for_y]
else:
pass
if back_x in range(self.width) and back_y in range(self.height):
BackwardMap[i, j] = self.img[back_x, back_y]
else:
pass
cv2.imshow('Forward Mapping', ForwardMap)
cv2.imshow('Backward Mapping', BackwardMap)
def demo():
rotation.showImage('normal', rotation)
rotation.printWidthHeight(rotation)
rotation.rotateImage(rotation)
cv2.waitKey(0)
cv2.destroyAllWindows

if __name__ == '__main__':
demo()


My problem is now that I want to make the rotated pictures (both for forward and backwards mapping) fit JUST right so there is no unnecessary use of space. Can anybody help me with this? Much appreciated.



If you have any suggestions to optimize my code, feel free to comment on that as well.










share|improve this question






















  • Could you show a picture - what you want to do? About optimization - you are using opencv that rotates images very fast.
    – MBo
    Nov 21 at 18:09















up vote
0
down vote

favorite












I have made this algorithm myself based on how to rotate an image.



import cv2
import math
import numpy as np

class rotation:
angle = 60.0
x = 100
y = 100
img = cv2.imread('FNAF.png',0)
width,height = img.shape
def showImage(name, self):
cv2.imshow(name, self.img)
def printWidthHeight(self):
print(self.width)
print(self.height)
def rotateImage(self):
ForwardMap = np.zeros((self.width,self.height),dtype="uint8")
BackwardMap = np.zeros((self.width,self.height),dtype="uint8")

for i in range(self.width):
for j in range(self.height):
# forward mapping
for_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) - (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
for_y = (i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
for_x = int(for_x)
for_y = int(for_y)
# backward mapping should change the forward mapping to the original image
back_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) + (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
back_y = -(i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
back_x = int(back_x)
back_y = int(back_y)
if for_x in range(self.width) and for_y in range(self.height):
ForwardMap[i, j] = self.img[for_x, for_y]
else:
pass
if back_x in range(self.width) and back_y in range(self.height):
BackwardMap[i, j] = self.img[back_x, back_y]
else:
pass
cv2.imshow('Forward Mapping', ForwardMap)
cv2.imshow('Backward Mapping', BackwardMap)
def demo():
rotation.showImage('normal', rotation)
rotation.printWidthHeight(rotation)
rotation.rotateImage(rotation)
cv2.waitKey(0)
cv2.destroyAllWindows

if __name__ == '__main__':
demo()


My problem is now that I want to make the rotated pictures (both for forward and backwards mapping) fit JUST right so there is no unnecessary use of space. Can anybody help me with this? Much appreciated.



If you have any suggestions to optimize my code, feel free to comment on that as well.










share|improve this question






















  • Could you show a picture - what you want to do? About optimization - you are using opencv that rotates images very fast.
    – MBo
    Nov 21 at 18:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have made this algorithm myself based on how to rotate an image.



import cv2
import math
import numpy as np

class rotation:
angle = 60.0
x = 100
y = 100
img = cv2.imread('FNAF.png',0)
width,height = img.shape
def showImage(name, self):
cv2.imshow(name, self.img)
def printWidthHeight(self):
print(self.width)
print(self.height)
def rotateImage(self):
ForwardMap = np.zeros((self.width,self.height),dtype="uint8")
BackwardMap = np.zeros((self.width,self.height),dtype="uint8")

for i in range(self.width):
for j in range(self.height):
# forward mapping
for_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) - (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
for_y = (i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
for_x = int(for_x)
for_y = int(for_y)
# backward mapping should change the forward mapping to the original image
back_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) + (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
back_y = -(i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
back_x = int(back_x)
back_y = int(back_y)
if for_x in range(self.width) and for_y in range(self.height):
ForwardMap[i, j] = self.img[for_x, for_y]
else:
pass
if back_x in range(self.width) and back_y in range(self.height):
BackwardMap[i, j] = self.img[back_x, back_y]
else:
pass
cv2.imshow('Forward Mapping', ForwardMap)
cv2.imshow('Backward Mapping', BackwardMap)
def demo():
rotation.showImage('normal', rotation)
rotation.printWidthHeight(rotation)
rotation.rotateImage(rotation)
cv2.waitKey(0)
cv2.destroyAllWindows

if __name__ == '__main__':
demo()


My problem is now that I want to make the rotated pictures (both for forward and backwards mapping) fit JUST right so there is no unnecessary use of space. Can anybody help me with this? Much appreciated.



If you have any suggestions to optimize my code, feel free to comment on that as well.










share|improve this question













I have made this algorithm myself based on how to rotate an image.



import cv2
import math
import numpy as np

class rotation:
angle = 60.0
x = 100
y = 100
img = cv2.imread('FNAF.png',0)
width,height = img.shape
def showImage(name, self):
cv2.imshow(name, self.img)
def printWidthHeight(self):
print(self.width)
print(self.height)
def rotateImage(self):
ForwardMap = np.zeros((self.width,self.height),dtype="uint8")
BackwardMap = np.zeros((self.width,self.height),dtype="uint8")

for i in range(self.width):
for j in range(self.height):
# forward mapping
for_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) - (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
for_y = (i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
for_x = int(for_x)
for_y = int(for_y)
# backward mapping should change the forward mapping to the original image
back_x = (i - self.x) * math.cos(self.angle*(math.pi/180)) + (j - self.y) * math.sin(self.angle*(math.pi/180)) + self.x
back_y = -(i - self.x) * math.sin(self.angle*(math.pi/180)) + (j - self.y) * math.cos(self.angle*(math.pi/180)) + self.x
back_x = int(back_x)
back_y = int(back_y)
if for_x in range(self.width) and for_y in range(self.height):
ForwardMap[i, j] = self.img[for_x, for_y]
else:
pass
if back_x in range(self.width) and back_y in range(self.height):
BackwardMap[i, j] = self.img[back_x, back_y]
else:
pass
cv2.imshow('Forward Mapping', ForwardMap)
cv2.imshow('Backward Mapping', BackwardMap)
def demo():
rotation.showImage('normal', rotation)
rotation.printWidthHeight(rotation)
rotation.rotateImage(rotation)
cv2.waitKey(0)
cv2.destroyAllWindows

if __name__ == '__main__':
demo()


My problem is now that I want to make the rotated pictures (both for forward and backwards mapping) fit JUST right so there is no unnecessary use of space. Can anybody help me with this? Much appreciated.



If you have any suggestions to optimize my code, feel free to comment on that as well.







python image opencv math image-processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 15:50









Zorn01

132




132












  • Could you show a picture - what you want to do? About optimization - you are using opencv that rotates images very fast.
    – MBo
    Nov 21 at 18:09


















  • Could you show a picture - what you want to do? About optimization - you are using opencv that rotates images very fast.
    – MBo
    Nov 21 at 18:09
















Could you show a picture - what you want to do? About optimization - you are using opencv that rotates images very fast.
– MBo
Nov 21 at 18:09




Could you show a picture - what you want to do? About optimization - you are using opencv that rotates images very fast.
– MBo
Nov 21 at 18:09












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Your original image has 4 corners, coordinates: (0,0), (w-1, 0), (0,h-1) and (w-1, h-1). Since your transformation is affine, why not transform these coordinates into destination coordinates?




  • (0,0) → (x1, y1)

  • (w-1, 0) → (x2, y2)

  • (0, h-1) → (x3, y3)

  • (w-1, h-1) → (x4, y4)


Your destination image size is then:



width  = max(x1, x2, x3, x4) - min(x1, x2, x3, x4)
height = max(y1, y2, y3, y4) - min(y1, y2, y3, y4)





share|improve this answer





















    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',
    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%2f53415786%2fhow-to-fit-image-right-in-homemade-rotation-algorithm-in-python%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








    up vote
    1
    down vote



    accepted










    Your original image has 4 corners, coordinates: (0,0), (w-1, 0), (0,h-1) and (w-1, h-1). Since your transformation is affine, why not transform these coordinates into destination coordinates?




    • (0,0) → (x1, y1)

    • (w-1, 0) → (x2, y2)

    • (0, h-1) → (x3, y3)

    • (w-1, h-1) → (x4, y4)


    Your destination image size is then:



    width  = max(x1, x2, x3, x4) - min(x1, x2, x3, x4)
    height = max(y1, y2, y3, y4) - min(y1, y2, y3, y4)





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Your original image has 4 corners, coordinates: (0,0), (w-1, 0), (0,h-1) and (w-1, h-1). Since your transformation is affine, why not transform these coordinates into destination coordinates?




      • (0,0) → (x1, y1)

      • (w-1, 0) → (x2, y2)

      • (0, h-1) → (x3, y3)

      • (w-1, h-1) → (x4, y4)


      Your destination image size is then:



      width  = max(x1, x2, x3, x4) - min(x1, x2, x3, x4)
      height = max(y1, y2, y3, y4) - min(y1, y2, y3, y4)





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Your original image has 4 corners, coordinates: (0,0), (w-1, 0), (0,h-1) and (w-1, h-1). Since your transformation is affine, why not transform these coordinates into destination coordinates?




        • (0,0) → (x1, y1)

        • (w-1, 0) → (x2, y2)

        • (0, h-1) → (x3, y3)

        • (w-1, h-1) → (x4, y4)


        Your destination image size is then:



        width  = max(x1, x2, x3, x4) - min(x1, x2, x3, x4)
        height = max(y1, y2, y3, y4) - min(y1, y2, y3, y4)





        share|improve this answer












        Your original image has 4 corners, coordinates: (0,0), (w-1, 0), (0,h-1) and (w-1, h-1). Since your transformation is affine, why not transform these coordinates into destination coordinates?




        • (0,0) → (x1, y1)

        • (w-1, 0) → (x2, y2)

        • (0, h-1) → (x3, y3)

        • (w-1, h-1) → (x4, y4)


        Your destination image size is then:



        width  = max(x1, x2, x3, x4) - min(x1, x2, x3, x4)
        height = max(y1, y2, y3, y4) - min(y1, y2, y3, y4)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 21:20









        AJNeufeld

        6,71811337




        6,71811337






























            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%2f53415786%2fhow-to-fit-image-right-in-homemade-rotation-algorithm-in-python%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

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...