ankit__nigam

building-pokedex-python-finding-game-boy-screen-step-4-6

Dec 7th, 2017
1,296
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import the necessary packages
  2.  
  3. import imutils
  4. from skimage import exposure
  5. #import exposure
  6. import numpy as np
  7. import argparse
  8. import cv2
  9.  
  10.  
  11. # load the query image, compute the ratio of the old height
  12. # to the new height, clone it, and resize it
  13. image = cv2.imread('gb.jpg')
  14. ratio = image.shape[0] / 300.0
  15. orig = image.copy()
  16. image = imutils.resize(image, height = 300)
  17.  
  18. # convert the image to grayscale, blur it, and find edges
  19. # in the image
  20. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  21. gray = cv2.bilateralFilter(gray, 11, 17, 17)
  22. edged = cv2.Canny(gray, 30, 200)
  23.  
  24. # find contours in the edged image, keep only the largest
  25. # ones, and initialize our screen contour
  26. cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  27. cnts = cnts[0] if imutils.is_cv2() else cnts[1]
  28. cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
  29. screenCnt = None
  30.  
  31. # loop over our contours
  32. for c in cnts:
  33.     # approximate the contour
  34.     peri = cv2.arcLength(c, True)
  35.     approx = cv2.approxPolyDP(c, 0.02 * peri, True)
  36.  
  37.     # if our approximated contour has four points, then
  38.     # we can assume that we have found our screen
  39.     if len(approx) == 4:
  40.         screenCnt = approx
  41.         break
  42.  
  43.  
  44. cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
  45. cv2.imshow("Game Boy Screen", image)
  46. cv2.waitKey(0)
RAW Paste Data