Sensor/Camera

[Python OpenCV] Perspective Transform example

전두선 2020. 11. 1. 15:20

 

input_image

 

output_image

 

output_dst

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import cv2
import numpy as np
 
 
frame = cv2.imread('./input_image.png')
 
left_top = [274,783]
right_top = [650,783]
left_bottom = [14,986]
right_bottom = [709,988]
 
= int(np.linalg.norm(np.array(right_top) - np.array(left_top)))
= int(np.linalg.norm(np.array(left_bottom) - np.array(left_top)))
print(f"{w} x {h}")
 
# Coordinates that you want to Perspective Transform
pts1 = np.float32([left_top,right_top,left_bottom,right_bottom])
# Size of the Transformed Image
pts2 = np.float32([[0,0],[w,0],[0,h],[w,h]])
 
print(f"pst1: {pts1}")
print(f"pst2: {pts2}")
 
for val in pts1:
    cv2.circle(frame,(val[0],val[1]),5,(0,0,255),-1)
= cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(frame,M,(int(w),int(h)))
 
cv2.imshow("paper", frame)
cv2.imshow("dst", dst)
 
cv2.imwrite("data.jpg", frame)
cv2.imwrite("data1.jpg", dst)
cv2.waitKey()
cv2.destroyAllWindows()
 
 

 


 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import cv2
import numpy as np
 
 
frame = cv2.imread('./input_image.png')
 
left_top = [650,661]
right_top = [693,661]
left_bottom = [840,992]
right_bottom = [997,992]
 
= int(np.linalg.norm(np.array(right_top) - np.array(left_top)))
= int(np.linalg.norm(np.array(left_bottom) - np.array(left_top)))
print(f"{w} x {h}")
 
# Coordinates that you want to Perspective Transform
pts1 = np.float32([left_top,right_top,left_bottom,right_bottom])
# Size of the Transformed Image
pts2 = np.float32([[0,0],[w,0],[0,h],[w,h]])
 
print(f"pst1: {pts1}")
print(f"pst2: {pts2}")
 
for val in pts1:
    cv2.circle(frame,(val[0],val[1]),5,(0,0,255),-1)
= cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(frame,M,(int(w),int(h)))
 
cv2.imshow("paper", frame)
cv2.imshow("dst", dst)
 
cv2.imwrite("data.jpg", frame)
cv2.imwrite("data1.jpg", dst)
cv2.waitKey()
cv2.destroyAllWindows()
 

 

'Sensor > Camera' 카테고리의 다른 글

[ROS] Visual-Inertial Calibration Using Kalibr  (1) 2021.01.20
[ROS] ORB SLAM3_Mono Camera  (1) 2021.01.12
[OpenCV] Vanishing point detection  (0) 2020.09.08
Homography or Projective Transformation  (0) 2020.08.17