Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5.  
  6. def blur_image(image, amount=3):
  7. '''Blurs the image
  8. Does not affect the original image'''
  9. kernel = np.ones((amount, amount), np.float32) / (amount**2)
  10. return cv2.filter2D(image, -1, kernel)
  11.  
  12. def get_contours():
  13.  
  14. def progowanie(x):
  15. print("Progowanie: " + str(x))
  16.  
  17. def rozmycie(x):
  18. print("rozmycie: " + str(x))
  19.  
  20. img = cv2.imread('pies.jpg', 0)
  21. img_c = cv2.imread('pies.jpg')
  22.  
  23. cv2.namedWindow('image')
  24.  
  25. cv2.createTrackbar('progowanie1', 'image', 39, 255, progowanie)
  26.  
  27. cv2.createTrackbar('progowanie2', 'image', 38, 255, progowanie)
  28.  
  29. cv2.createTrackbar('rozmycie', 'image', 1, 7, rozmycie)
  30.  
  31. imgshow = img
  32.  
  33. while (1):
  34. cv2.imshow('image', imgshow)
  35. k = cv2.waitKey(1) & 0xFF
  36. # escape to end program
  37. if k == 27:
  38. break
  39.  
  40. # get current positions of four trackbars
  41. progowanie1 = cv2.getTrackbarPos('progowanie1', 'image')
  42.  
  43. progowanie2 = cv2.getTrackbarPos('progowanie2', 'image')
  44.  
  45. blur = cv2.getTrackbarPos('rozmycie', 'image')
  46.  
  47. if blur % 2 == 0:
  48. blur = blur + 1
  49. bluredImg = cv2.GaussianBlur(img, (blur, blur), 1.5)
  50. imgshow = cv2.Canny(bluredImg, progowanie1, progowanie2)
  51.  
  52.  
  53. im2, contours, hierarchy = cv2.findContours(blur_image(imgshow), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  54. cv2.drawContours(img_c, contours, -1, (0, 255, 0), 3)
  55. cv2.imshow('dst', img_c)
  56. if cv2.waitKey(0) & 0xff == 27:
  57. cv2.destroyAllWindows()
  58. cv2.destroyAllWindows()
  59. for con in contours:
  60. if cv2.contourArea(con)>3000:
  61. mask_image(con)
  62.  
  63.  
  64. def mask_image(contour):
  65. image = cv2.imread('pies.jpg', -1)
  66. mask = np.zeros(image.shape, dtype=np.uint8)
  67. roi_corners = np.array(contour, dtype=np.int32)
  68. channel_count = image.shape[2]
  69. ignore_mask_color = (255,) * channel_count
  70. cv2.fillConvexPoly(mask, roi_corners, ignore_mask_color)
  71. masked_image = cv2.bitwise_and(image, mask)
  72. epsilon = 0.1 * cv2.arcLength(contour, True)
  73. print(epsilon)
  74. cv2.imshow('dst', masked_image)
  75. if cv2.waitKey(0) & 0xff == 27:
  76. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement