Advertisement
kalin729

koda

May 26th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. from __future__ import print_function
  2. import cv2 as cv
  3. import numpy as np
  4. import argparse
  5. import random as rng
  6.  
  7. rng.seed(12345)
  8.  
  9. def thresh_callback(val):
  10. threshold = val
  11.  
  12. ## [Canny]
  13. # Detect edges using Canny
  14. canny_output = cv.Canny(src_gray, threshold, threshold * 2)
  15. ## [Canny]
  16.  
  17. ## [findContours]
  18. # Find contours
  19. contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
  20. ## [findContours]
  21.  
  22. ## [allthework]
  23. # Approximate contours to polygons + get bounding rects and circles
  24. contours_poly = [None]*len(contours)
  25. boundRect = [None]*len(contours)
  26. centers = [None]*len(contours)
  27. radius = [None]*len(contours)
  28. for i, c in enumerate(contours):
  29. contours_poly[i] = cv.approxPolyDP(c, 3, True)
  30. boundRect[i] = cv.boundingRect(contours_poly[i])
  31. centers[i], radius[i] = cv.minEnclosingCircle(contours_poly[i])
  32. ## [allthework]
  33.  
  34. ## [zeroMat]
  35. drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
  36. ## [zeroMat]
  37.  
  38. ## [forContour]
  39. # Draw polygonal contour + bonding rects + circles
  40. for i in range(len(contours)):
  41. color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
  42. cv.drawContours(drawing, contours_poly, i, color)
  43. cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \
  44. (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
  45. cv.circle(drawing, (int(centers[i][0]), int(centers[i][1])), int(radius[i]), color, 2)
  46. ## [forContour]
  47.  
  48. cnt = contours[0]
  49. x,y,w,h = cv.boundingRect(cnt)
  50. aspect_ratio = float(w)/h
  51.  
  52. print("Aspect Ratio: ", aspect_ratio)
  53.  
  54. area = cv.contourArea(cnt)
  55. x,y,w,h = cv.boundingRect(cnt)
  56. rect_area = w*h
  57. extent = float(area)/rect_area
  58.  
  59. print("Extent: ", extent)
  60.  
  61. area = cv.contourArea(cnt)
  62. hull = cv.convexHull(cnt)
  63. hull_area = cv.contourArea(hull)
  64. solidity = float(area)/hull_area
  65.  
  66. print("Solidity: ", solidity)
  67.  
  68. area = cv.contourArea(cnt)
  69. equi_diameter = np.sqrt(4*area/np.pi)
  70.  
  71. print("Equivalent Diameter: ", equi_diameter)
  72.  
  73. ## [showDrawings]
  74. # Show in a window
  75. cv.imshow('Contours', drawing)
  76. ## [showDrawings]
  77.  
  78. ## [setup]
  79. # Load source image
  80. parser = argparse.ArgumentParser(description='Code for Creating Bounding boxes and circles for contours tutorial.')
  81. parser.add_argument('--input', help='Path to input image.', default='snimka.jpg')
  82. args = parser.parse_args()
  83.  
  84. src = cv.imread(cv.samples.findFile(args.input))
  85. if src is None:
  86. print('Could not open or find the image:', args.input)
  87. exit(0)
  88.  
  89. # Convert image to gray and blur it
  90. src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
  91. src_gray = cv.blur(src_gray, (3,3))
  92. ## [setup]
  93.  
  94. ## [createWindow]
  95. # Create Window
  96. source_window = 'Source'
  97. cv.namedWindow(source_window)
  98. cv.imshow(source_window, src)
  99. ## [createWindow]
  100. ## [trackbar]
  101. max_thresh = 255
  102. thresh = 100 # initial threshold
  103. cv.createTrackbar('Canny thresh:', source_window, thresh, max_thresh, thresh_callback)
  104. thresh_callback(thresh)
  105. ## [trackbar]
  106.  
  107. cv.waitKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement