Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. import cv2.cv2 as cv2
  2.  
  3. img = cv2.imread("E:\Photos_for_TZ\gordon.jpg")
  4. img1 = cv2.imread("E:\Photos_for_TZ\gordon.jpg", 0)
  5.  
  6. ret, thresh = cv2.threshold(img1, 170, 255, cv2.THRESH_BINARY)
  7. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
  8. print('Число контуров функции threshold:' + str(len(contours)))
  9.  
  10. blur = cv2.GaussianBlur(img1,(1,1), cv2.BORDER_DEFAULT)
  11. canny = cv2.Canny(image = blur, threshold1 = 170, threshold2 = 230, apertureSize = 3, L2gradient = True)
  12. contours1, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
  13. print('Число контуров функции Canny:'+ str(len(contours1)) +'\n')
  14.  
  15. cv2.drawContours(img, contours1, -1, (30, 170, 230), 2)
  16.  
  17. cv2.imshow('Original', img)
  18. cv2.imshow('Grayscale', img1)
  19. cv2.imshow('Threshold', thresh)
  20. cv2.imshow('Canny', canny)
  21.  
  22. cv2.waitKey(0)
  23. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement