Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

affine transformation #2940

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Affine transformation/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Affine Transformation
This python script will allow us to change perspective of an image using Affine transformation.

## Setup Instructions
### Install python3
sudo apt-get install python3
### Install pip (package installer for python)
sudo apt-get install python3-pip
### Install Numpy library with pip
pip3 install numpy
### Install OpenCV library with pip
pip3 install opencv-python
### Install tkinter library
sudo apt-get install python3-tk

## Details/Output
User selects an input image and the script changes its perspective using Affine transformation.
In Affine transformation, parallel lines still stay parallel after transformation.
(**Note** The positions of any 3 points (should not be collinear) on the original image and the positions where they would be present after the transformation should be specified in the code.

## Author
Github: invigorzz313
Binary file added Affine transformation/Sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions Affine transformation/afffine_transformation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import cv2
import numpy as np
import tkinter as tk
from tkinter.filedialog import *

photo = askopenfilename() # reading the input image
img = cv2.imread(photo)
rows, columns, ch = img.shape
window = tk.Tk()
window.title("Perspective transform")
window.geometry('350x200')


# pts1 is an array storing coordinates of 3 points on the original image
pts1 = np.float32([[50,50],[200,50],[50,200]])
# pts2 is an array storing coordinates of 3 positions where the above points should be after the transformation
pts2 = np.float32([[10,100],[200,50],[100,250]])

Mat = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img, Mat, (500,500))

label = tk.Label(window, text="Points chosen on original image: " + str(pts1)).grid(row=0, column=1)
label = tk.Label(window, text="Points on transformed image: " + str(pts2)).grid(row=1, column=1)
label = tk.Label(window, text="The coordinates can be changed in the code ").grid(row=2, column=1)
# displaying the images
cv2.imshow("Original Image", img)
cv2.imshow("Transformed Image", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

window.mainloop()