Friday, November 10, 2023

OpenCV and Image Processing - Warp Image contd...

In this post, as an example in Figure shown below, the left picture is taken by a tablet camera from the homepage of OpenCV.org, the picture looks distorted, the perspective warping will be used to correct it and make it aligned properly, the result will be shown in the right side of Figure shown below.


OpenCV provides two functions to perform perspective warping, cv2.getPerspectiveTransform() is to calculate the transformation matrix, it takes the four source points and four target points as input. Then cv2.warpPerspective() is to perform the perspective warping, it takes the original image, the transformation matrix and the output size.

We will take the four source points from the original image, which indicate the area to transform, and then use them to do the perspective warping. In the ImageProcessing class, add the function perspective_ warp():

1 def perspective_warp(self, points, width, height image=None):

2 if image is None:

3 image = self.image

4 pts_source = np.float32([points[0], points[1],

points[3], points[2]])

5 pts_target = np.float32([[0, 0],[width, 0],

[0,height],[width,height]])

6 matrix = cv2.getPerspectiveTransform(

pts_source, pts_target)

7 result = cv2.warpPerspective(

image,matrix,(width,height))

8 return result

Explanations:


Here are the source codes-

1 import cv2

2 import numpy as np

3 import common.Draw as dw

4 import common.ImageProcessing as ip

5 drawing = False

6 final_color = (0, 0, 255)

7 drawing_color = (0, 0, 125)

8 width, height = 320, 480

9 points = []

10 def on_mouse(event, x, y, flags, param):

11 global points, drawing, img, img_bk, iproc, warped_image

12 if event == cv2.EVENT_LBUTTONDOWN:

13 drawing = True

14 add_point(points, (x, y))

15 if len(points) == 4:

16 draw_polygon(img, points, (x, y), True)

17 drawing = False

18 img_bk = iproc.copy()

19 warped_image = iproc.perspective_warp(points,

width, height)

20 points.clear()

21 iproc.show("Perspective Warping", image=warped_image)

22 elif event == cv2.EVENT_MOUSEMOVE:

23 if drawing == True:

24 img = img_bk.copy()

25 draw_polygon(img, points, (x, y))

26 27 def add_point(points, curt_pt):

28 print("Adding point #%d with position(%d,%d)"

29 % (len(points), curt_pt[0], curt_pt[1]))

30 points.append(curt_pt)

31

32 def draw_polygon(img, points, curt_pt, is_final=False):

33 if (len(points) > 0):

34 if is_final == False:

35 dw.draw_polylines(img, np.array([points]),

False, final_color)

36 dw.draw_line(img, points[-1], curt_pt,

drawing_color)

37 else:

38 dw.draw_polylines(img, np.array([points]),

True, final_color)

39 for point in points:

40 dw.draw_circle(img, point, 2, final_color, 2)

41 dw.draw_text(img, str(point), point,

color=final_color,

font_scale=0.5)

42

43 def print_instruction(img):

44 txtInstruction = "Left click to specify four

points to warp image.

ESC to exit, 's' to save"

45 dw.draw_text(img,txtInstruction, (10, 20), 0.5,

(255, 255, 255))

46 print(txtInstruction)

47

48 if __name__ == "__main__":

49 global img, img_bk, iproc, warped_image

50 title = "Original Image"

51 iproc = ip.ImageProcessing(title,

"../res/skewed_image001.jpg")

52 img = iproc.image

53 print_instruction(img)

54 img_bk = iproc.copy()

55 cv2.setMouseCallback(title, on_mouse)

56 iproc.show()

57 while True:

58 iproc.show(image=img)

59 ch = cv2.waitKey(10)

60 if (ch & 0xFF) == 27:

61 break

62 elif ch == ord('s'):

63 # press 's' key to save image

64 filepath = "C:/temp/warp_image.png"

65 cv2.imwrite(filepath, warped_image)

66 print("File saved to " + filepath)

67 cv2.destroyAllWindows()

The source codes are not explained line by line here, because it’s basically the same as the one for drawing polygons discussed before. Execute the codes in WarpImage.py, left click on the image to specify the four points in the same way as we did in drawing polygons, the coordinates are shown in the image. After all four points are collected, it will draw the polygon, and then call perspective_warp() function to transform the specified area and show the resulting image.

Share:

0 comments:

Post a Comment