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.
python image opencv math image-processing
add a comment |
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.
python image opencv math image-processing
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
add a comment |
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.
python image opencv math image-processing
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
python image opencv math image-processing
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 22 at 21:20
AJNeufeld
6,71811337
6,71811337
add a comment |
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.
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.
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%2f53415786%2fhow-to-fit-image-right-in-homemade-rotation-algorithm-in-python%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
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