Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. from Tkinter import *
  2. from tkFileDialog import askopenfilename
  3. from PIL import Image, ImageTk
  4. import cv2
  5. import numpy as np
  6.  
  7. class PerspectiveTransform():
  8. def __init__(self, master):
  9. self.parent = master
  10. self.coord = [] # x,y coordinate
  11. self.file = '' #image path
  12.  
  13. #setting up a tkinter canvas with scrollbars
  14. self.frame = Frame(self.parent, bd=2, relief=SUNKEN)
  15. self.frame.grid_rowconfigure(0, weight=1)
  16. self.frame.grid_columnconfigure(0, weight=1)
  17. self.xscroll = Scrollbar(self.frame, orient=HORIZONTAL)
  18. self.xscroll.grid(row=1, column=0, sticky=E+W)
  19. self.yscroll = Scrollbar(self.frame)
  20. self.yscroll.grid(row=0, column=1, sticky=N+S)
  21. self.canvas = Canvas(self.frame, bd=0, xscrollcommand=self.xscroll.set, yscrollcommand=self.yscroll.set)
  22. self.canvas.grid(row=0, column=0, sticky=N+S+E+W)
  23. self.xscroll.config(command=self.canvas.xview)
  24. self.yscroll.config(command=self.canvas.yview)
  25. self.frame.pack(fill=BOTH,expand=1)
  26. self.addImage()
  27.  
  28. #mouseclick event and button
  29. self.canvas.bind("<Button 1>",self.insertCoords)
  30. self.canvas.bind("<Button 3>",self.removeCoords)
  31. self.addImgBtn = Button(self.frame, text="Browse", command=self.addImage)
  32. self.addImgBtn.grid(row=0,column=2,sticky =NE)
  33. self.saveBtn = Button(self.frame, text="Save", command=self.saveImage)
  34. self.saveBtn.grid(row=0,column=3,sticky =NE)
  35.  
  36. #adding the image
  37. def addImage(self):
  38. self.coord = []
  39. self.file = askopenfilename(parent=self.parent, initialdir="Examples\\cali\\",title='Choose an image.')
  40. self.img = ImageTk.PhotoImage(Image.open(self.file))
  41. self.canvas.create_image(0,0,image=self.img,anchor="nw")
  42. self.canvas.config(scrollregion=self.canvas.bbox(ALL), width=self.img.width(), height=self.img.height())
  43.  
  44.  
  45. #Save coord according to mouse left click
  46. def insertCoords(self, event):
  47. #outputting x and y coords to console
  48. self.coord.append([event.x, event.y+200])
  49. r=3
  50. self.canvas.create_oval(event.x - r, event.y - r, event.x + r, event.y + r, fill="#ff0000") #print circle
  51. if (len(self.coord) == 4):
  52. self.Transformer()
  53. self.canvas.delete("all")
  54. self.canvas.create_image(0,0,image=self.result,anchor="nw")
  55. self.canvas.image = self.result
  56.  
  57. #remove last inserted coord using mouse right click
  58. def removeCoords(self):
  59. del self.coord[-1]
  60.  
  61. def Transformer(self):
  62. frame = cv2.imread(self.file)
  63. frame_circle = frame.copy()
  64. #points = [[480,90],[680,90],[0,435],[960,435]]
  65. cv2.circle(frame_circle, tuple(self.coord[0]), 5, (0, 0, 255), -1)
  66. cv2.circle(frame_circle, tuple(self.coord[1]), 5, (0, 0, 255), -1)
  67. cv2.circle(frame_circle, tuple(self.coord[2]), 5, (0, 0, 255), -1)
  68. cv2.circle(frame_circle, tuple(self.coord[3]), 5, (0, 0, 255), -1)
  69.  
  70. widthA = np.sqrt(((self.coord[3][0] - self.coord[2][0]) ** 2) + ((self.coord[3][1] - self.coord[2][1]) ** 2))
  71. widthB = np.sqrt(((self.coord[1][0] - self.coord[0][0]) ** 2) + ((self.coord[1][1] - self.coord[0][1]) ** 2))
  72. maxWidth = max(int(widthA), int(widthB))
  73.  
  74. heightA = np.sqrt(((self.coord[1][0] - self.coord[3][0]) ** 2) + ((self.coord[1][1] - self.coord[3][1]) ** 2))
  75. heightB = np.sqrt(((self.coord[0][0] - self.coord[2][0]) ** 2) + ((self.coord[0][1] - self.coord[2][1]) ** 2))
  76. maxHeight = max(int(heightA), int(heightB))
  77.  
  78. print self.coord
  79. pts1 = np.float32(self.coord)
  80. pts2 = np.float32([[0, 0], [maxWidth-1, 0], [0, maxHeight-1], [maxWidth-1, maxHeight-1]])
  81. matrix = cv2.getPerspectiveTransform(pts1, pts2)
  82. self.result_cv = cv2.warpPerspective(frame, matrix, (maxWidth,maxHeight))
  83.  
  84. #cv2.imshow("Frame", frame_circle)
  85. #cv2.imshow("Perspective transformation", result_cv)
  86.  
  87. result_rgb = cv2.cvtColor(self.result_cv, cv2.COLOR_BGR2RGB)
  88. self.result = ImageTk.PhotoImage(image = Image.fromarray(result_rgb))
  89.  
  90. def saveImage(self):
  91. cv2.imwrite("Examples/saved.jpg", self.result_cv)
  92. print "Saved!"
  93.  
  94. #---------------------------------
  95. if __name__ == '__main__':
  96. root = Tk()
  97. #root.geometry("1360x740")
  98. transformer = PerspectiveTransform(root)
  99. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement