image-perspective-transformation-image perspective transformation (projection transformation)
Article Directory
Foreword:
Recently, perspective transformation may be used to do a data preprocessing. Therefore, I remember that I wrote a perspective transformation script before. I ran it today and it was not perfect enough. Therefore, I extracted it separately from the blog that was previously mixed in the paper reading for Everyone's reference~ Do
n't say much, the example picture above!
Come and learn together!
The main function:
Python processes images, including image translation, image rotation, image zoom, image flip, and perspective transformation. Select the four key points in the picture and the points to be transformed to generate a new perspective view
Use translate,scale,flip,rotation to turn one picture into multiple pictures.
Reference link:
https://github.com/dapsjj/dealAllPicture uses the code of this big guy in the main frame. I added the functions of perspective transformation and mouse interaction.
My code link:
https://github.com/kaixindelele/image-perspective-transformation
Core code:
Perspective transformation function, first of all to get the four points of the source area and the target area
def pic_perspective(self,):
...
pts1 = []
pts2 = []
cv2.setMouseCallback("image", self.mouse, param=(image, pts1, pts2))
cv2.waitKey(0)
cv2.destroyAllWindows()
print("pts1:", pts1)
pts1 = np.float32(pts1[:4])
print("pts2:", pts2)
pts2 = np.float32(pts2[:4])
assert len(pts1)==4, "每个只允许四个点"
# 生成透视变换矩阵
M = cv2.getPerspectiveTransform(pts1, pts2)
# 进行透视变换
dst = cv2.warpPerspective(image, M, (image.shape[1], image.shape[0]))
cv2.imwrite('dst.jpg', dst)
# matplotlib默认以RGB通道显示,所以需要用[:, :, ::-1]翻转一下
plt.subplot(121), plt.imshow(image[:, :, ::-1]), plt.title('input')
plt.subplot(122), plt.imshow(dst[:, :, ::-1]), plt.title('output')
plt.show()
# tkinter.messagebox.showinfo('提示', '透视变换的图片处理完毕!')
return dst
Obtain the coordinates of the mouse:
def mouse(self, event, x, y, flags, param):
image = param[0]
pts1 = param[1]
pts2 = param[2]
# 如果摁住左键,将坐标赋值给pts1
if event == cv2.EVENT_LBUTTONDOWN:
pts1.append([x, y])
xy = "%d,%d" % (x, y)
cv2.circle(image, (x, y), 4, (0, 255, 255), thickness = -1)
cv2.putText(image, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 255, 255), thickness = 2)
cv2.imshow("image", image)
# 如果摁住右键,将坐标赋值给pts2
if event == cv2.EVENT_RBUTTONDOWN:
pts2.append([x, y])
xy = "%d,%d" % (x, y)
cv2.circle(image, (x, y), 4, (255, 0, 255), thickness = -1)
cv2.putText(image, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
1.0, (255, 0, 255), thickness = 2)
cv2.imshow("image", image)
contact details:
ps: Welcome students who are intensive to join the group to study together:
Deep Reinforcement Learning-DRL: 799378128
Welcome to follow Zhihu account: Alchemy apprentices who have not yet started
CSDN account: https://blog.csdn.net/hehedadaq
Minimal spinup+HER+PER code implementation: https://github.com/kaixindelele/DRLib