Skip to content

Commit 79593fb

Browse files
Merge pull request avinashkranjan#2892 from invigorzz313/perspective
perspective transformation
2 parents c74aa31 + 716c64a commit 79593fb

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

Perspective_transform/ReadMe.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Perspective Transformation
2+
This python script will allow us to change perspective of an image.
3+
4+
## Setup Instructions
5+
### Install python3
6+
sudo apt-get install python3
7+
### Install pip (package installer for python)
8+
sudo apt-get install python3-pip
9+
### Install Numpy library with pip
10+
pip3 install numpy
11+
### Install OpenCV library with pip
12+
pip3 install opencv-python
13+
### Install tkinter library
14+
sudo apt-get install python3-tk
15+
16+
## Details/Output
17+
User selects an input image and the script changes its perspective and displays it.
18+
(**Note** The positions of any 4 points (no 3 of them should be collinear) on the original image and the positions where they would be present after the transformation should be specified in the code.
19+
20+
## Author
21+
Github: invigorzz313

Perspective_transform/Sample.png

439 KB
Loading
372 KB
Loading
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import cv2
2+
import numpy as np
3+
import tkinter as tk
4+
from tkinter.filedialog import *
5+
6+
photo = askopenfilename() # reading the input image
7+
img = cv2.imread(photo)
8+
img = cv2.resize(img,(500,500))
9+
10+
window = tk.Tk()
11+
window.title("Perspective transform")
12+
window.geometry('350x200')
13+
14+
15+
# pts1 is an array storing coordinates of 4 points on the original image
16+
pts1 = np.float32([[103,97],[390,93],[85,351],[412,352]])
17+
# pts2 is an array storing coordinates of 4 positions where the above points should be after the transformation
18+
pts2 = np.float32([[103,97],[390,93],[133,400],[390,400]])
19+
20+
Mat = cv2.getPerspectiveTransform(pts1,pts2)
21+
dst = cv2.warpPerspective(img, Mat, (500,500))
22+
23+
label = tk.Label(window, text="Points chosen on original image: " + str(pts1)).grid(row=0, column=1)
24+
label = tk.Label(window, text="Points on transformed image: " + str(pts2)).grid(row=1, column=1)
25+
label = tk.Label(window, text="The coordinates can be changed in the code ").grid(row=2, column=1)
26+
# displaying the images
27+
cv2.imshow("Original Image", img)
28+
cv2.imshow("Transformed Image", dst)
29+
cv2.waitKey(0)
30+
cv2.destroyAllWindows()
31+
32+
window.mainloop()

0 commit comments

Comments
 (0)