Case Studies

  1. Introduction
  2. Face Detection
  3. Webcam Face Detection
  4. Object Tracking in Video
  5. Eye Tracking
  6. Handwriting Recognition with HOG
  7. Plant Classification
  8. Building an Amazon.com Cover Search
  9. Conclusions

Load Display Save

  1. import argparse
  2. import cv2
  3. ap = argparse.ArgumentParser()
  4. ap.add_argument("-i", "--image", required = True, help = "Path to the image")
  5. args = vars(ap.parse_args())
  6. #load
  7. image = cv2.imread(args["image"])
  8. w, h, c = image.shape
  9. #display
  10. cv2.imshow("Image", image)
  11. cv2.waitKey(0)
  12. #save
  13. cv2.imwrite("newimage.jpg", image)

Drawing

  1. import numpy as np
  2. import cv2
  3. canvas = np.zeros((300, 300, 3), dtype = "uint8")
  4. #line
  5. green = (0, 255, 0)
  6. cv2.line(canvas, (0, 0), (300, 300), green)
  7. cv2.imshow("Canvas", canvas)
  8. cv2.waitKey(0)
  9. red = (0, 0, 255)
  10. cv2.line(canvas, (300, 0), (0, 300), red, 3)
  11. cv2.imshow("Canvas", canvas)
  12. cv2.waitKey(0)
  13. #rectangle
  14. cv2.rectangle(canvas, (10, 10), (60, 60), green)
  15. cv2.imshow("Canvas", canvas
  16. cv2.waitKey(0)
  17. cv2.rectangle(canvas, (50, 200), (200, 225), red, 5)
  18. cv2.imshow("Canvas", canvas)
  19. cv2.waitKey(0)
  20. blue = (255, 0, 0)
  21. cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1)
  22. cv2.imshow("Canvas", canvas)
  23. cv2.waitKey(0)
  24. #circle
  25. canvas = np.zeros((300, 300, 3), dtype = "uint8")
  26. (centerX, centerY) = (canvas.shape[1] // 2, canvas.shape[0] // 2)
  27. white = (255, 255, 255)
  28. for r in range(0, 175, 25):
  29. cv2.circle(canvas, (centerX, centerY), r, white)
  30. cv2.imshow("Canvas", canvas)
  31. cv2.waitKey(0)
  32. #abstract drawing
  33. for i in range(0, 25):
  34. radius = np.random.randint(5, high = 200)
  35. color = np.random.randint(0, high = 256, size = (3,)).tolist()
  36. pt = np.random.randint(0, high = 300, size = (2,))
  37. cv2.circle(canvas, tuple(pt), radius, color, -1)
  38. cv2.imshow("Canvas", canvas)
  39. cv2.waitKey(0)

Image Processing

translation

  1. import numpy as np
  2. import argparse
  3. import imutils
  4. import cv2
  5. ap = argparse.ArgumentParser()
  6. ap.add_argument("-i", "--image", required = True, help = "Path to the image")
  7. args = vars(ap.parse_args())
  8. image = cv2.imread(args["image"])
  9. cv2.imshow("Original", image)
  10. M = np.float32([[1, 0, 25], [0, 1, 50]])
  11. shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
  12. cv2.imshow("Shifted Down and Right", shifted)
  13. M = np.float32([[1, 0, -50], [0, 1, -90]])
  14. shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
  15. cv2.imshow("Shifted Up and Left", shifted)

rotation

  1. import numpy as np
  2. import argparse
  3. import imutils
  4. import cv2
  5. ap = argparse.ArgumentParser()
  6. ap.add_argument("-i", "--image", default='33.jpg', help = "Path to the image")
  7. args = vars(ap.parse_args())
  8. image = cv2.imread(args["image"])
  9. cv2.imshow("Original", image)
  10. (h, w) = image.shape[:2]
  11. center=(w//2,h//2)
  12. M = cv2.getRotationMatrix2D(center, 45, 1.0)
  13. rotated = cv2.warpAffine(image, M, (w, h))
  14. cv2.imshow("Rotated by 45 Degrees", rotated)
  15. M = cv2.getRotationMatrix2D(center, -90, 1.0)
  16. rotated = cv2.warpAffine(image, M, (w, h))
  17. cv2.imshow("Rotated by -90 Degrees", rotated)
  18. cv2.waitKey()

resizing

  1. import numpy as np
  2. import argparse
  3. import imutils
  4. import cv2
  5. ap = argparse.ArgumentParser()
  6. ap.add_argument("-i", "--image", default='33.jpg', help = "Path to the image")
  7. args = vars(ap.parse_args())
  8. image = cv2.imread(args["image"])
  9. cv2.imshow("Original", image)
  10. r = 150.0 / image.shape[1]
  11. dim = (150, int(image.shape[0] * r))
  12. resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
  13. cv2.imshow("Resized (Width)", resized)
  14. r = 50.0 / image.shape[0]
  15. dim = (int(image.shape[1] * r), 50)
  16. resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
  17. cv2.imshow("Resized (Height)", resized)
  18. cv2.waitKey(0)

flipping

  1. import argparse
  2. import cv2
  3. ap = argparse.ArgumentParser()
  4. ap.add_argument("-i", "--image", default='33.jpg', help = "Path to the image")
  5. args = vars(ap.parse_args())
  6. image = cv2.imread(args["image"])
  7. cv2.imshow("Original", image)
  8. flipped = cv2.flip(image, 1)
  9. cv2.imshow("Flipped Horizontally", flipped)
  10. flipped = cv2.flip(image, 0)
  11. cv2.imshow("Flipped Vertically", flipped)
  12. flipped = cv2.flip(image, -1)
  13. cv2.imshow("Flipped Horizontally & Vertically", flipped)
  14. cv2.waitKey(0)

Histograms

Smoothing and Blurring

Thresholding

Gradients and Edge Detection

Contours