Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import numpy as np
  2. import argparse
  3. import glob
  4. import cv2
  5. import sys
  6. #function that draws the smallest circle enclosing the contour
  7.  
  8. def draw_Cirles(contour):
  9. (x, y), radius = cv2.minEnclosingCircle(contour)
  10. center = (int(x), int(y))
  11. radius = int(radius)
  12. print radius
  13. if(radius > 30 and radius <180):
  14. cv2.circle(image, center, radius, (0, 255, 0), 2)
  15. contour_list.append(contour)
  16.  
  17. def threshold(threshold, minusValue, image):
  18. # define the list of boundaries (pixel color)
  19. boundaries = [
  20. ([threshold - minusValue, threshold - minusValue, threshold - minusValue], [255, 255, 255]) # TODO: adjust threshold accordingly!!!
  21. ]
  22. # Loop over the boundaries
  23. for (lower, upper) in boundaries:
  24. # create NumPy arrays from the boundaries
  25. lower = np.array(lower, dtype="uint8")
  26. upper = np.array(upper, dtype="uint8")
  27. # find the colors within the specified boundaries and apply
  28. # the mask
  29. mask = cv2.inRange(image, lower, upper)
  30. output = cv2.bitwise_and(image, image, mask=mask)
  31. return output
  32.  
  33. def processImage(image):
  34. #threshold(maxVal,80)
  35. # Apply bilateral filtered image
  36. bilateral_filtered_image = cv2.bilateralFilter(image, 5, 175, 175)
  37. cv2.imshow("bilateral", bilateral_filtered_image)
  38. # Canny edge magic
  39. edge_detected_image = cv2.Canny(bilateral_filtered_image, 75, 200)
  40. cv2.imshow("Canny", edge_detected_image)
  41. return edge_detected_image
  42.  
  43. def findContours(image):
  44. # Now lets find the countors
  45. _, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  46. # Sexy loop combined with approxPolyDP
  47. for contour in contours:
  48. approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
  49. area = cv2.contourArea(contour)
  50. draw_Cirles(contour)
  51. # if ((len(approx) > 8) & (len(approx) < 23) & (area > 6000) & (area < 99999) ):
  52. # print area
  53. # contour_list.append(contour)
  54. def findMinMaxLoc(image,blurRadius):
  55. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  56. gray = cv2.GaussianBlur(gray, (blurRadius, blurRadius), 0)
  57. (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
  58. print maxVal
  59. copy = image.copy()
  60. cv2.circle(copy, maxLoc, 45, (255, 0, 0), 2)
  61. # display the results
  62. cv2.imshow("Robust", gray)
  63. return minVal,maxVal
  64. contour_list = []
  65. image = cv2.resize(cv2.imread('iot1.png'),(640,480))
  66. cv2.imshow("original",image)
  67. # apply a Gaussian blur to the image then find the brightest region
  68. minVal,maxVal = findMinMaxLoc(image,49)
  69. processedImage = processImage(threshold(maxVal,90,image))
  70. contours = findContours(processedImage)
  71. cv2.drawContours(image, contour_list, -1, (0,255,0), 2)
  72. if(len(contour_list) > 0):
  73. print(str(len(contour_list)) + " persons were found in this picture")
  74. cv2.imshow("Result", image)
  75. cv2.waitKey(0)
  76. else:
  77. cv2.imshow("Result", image)
  78. print("got nothing")
  79. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement