main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import cv2
  2. import numpy as np
  3. import zmq
  4. from skimage.measure import label
  5. import math
  6. def euclidian(x1, y1, x2, y2):
  7. return ((x1 - x2)**2 + (y1 - y2)**2)**0.5
  8. cv2.namedWindow("Image", cv2.WINDOW_GUI_NORMAL)
  9. cv2.namedWindow("Image2", cv2.WINDOW_GUI_NORMAL)
  10. context = zmq.Context()
  11. socket = context.socket(zmq.SUB)
  12. socket.setsockopt(zmq.SUBSCRIBE, b"")
  13. port = 5055
  14. socket.connect(f"tcp://192.168.0.113:{port}")
  15. n = 0
  16. while True:
  17. bts = socket.recv()
  18. n += 1
  19. arr = np.frombuffer(bts, np.uint8)
  20. imagege = cv2.imdecode(arr, cv2.IMREAD_COLOR)
  21. key = cv2.waitKey(10)
  22. if key == ord("q"):
  23. break
  24. image = cv2.cvtColor(imagege.copy(), cv2.COLOR_BGR2HSV)
  25. image = image[:,:,1]
  26. img = image.copy()
  27. _, thras = cv2.threshold(img, 70, 255, cv2.THRESH_BINARY)
  28. image = cv2.threshold(image, 70, 255, cv2.THRESH_BINARY)[1]
  29. image = cv2.dilate(image, None, iterations=3)
  30. # sssssss
  31. # ssssssss
  32. labeled = label(image)
  33. cntrs = list(cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0])
  34. circles = 0
  35. cubicCount = 0
  36. for c in cntrs:
  37. (curr_x, curr_y), r = cv2.minEnclosingCircle(c)
  38. area = cv2.contourArea(c)
  39. circleArea = math.pi * r ** 2
  40. if area <= 2000:
  41. continue
  42. if area / circleArea >= 0.9:
  43. circles += 1
  44. else:
  45. cubicCount += 1
  46. distance_map = cv2.distanceTransform(thras, cv2.DIST_L2, 5)
  47. ret ,dist_thres= cv2.threshold(distance_map, 0.6 * np.max(distance_map), 255, cv2.THRESH_BINARY)
  48. # ret, dist_thres = cv2.threshold(distance_map, 0.5, 25, cv2.THRESH_BINARY)
  49. # ret, markers = cv2.connectedComponents(dist_thres.astype('uint8'))
  50. # segments = cv2.wetershed(image, markers + 1)
  51. confuse = cv2.subtract(thras, dist_thres.astype('uint8'))
  52. ret, markers = cv2.connectedComponents(dist_thres.astype('uint8'))
  53. markers += 1
  54. markers[confuse == 255] = 0
  55. segments = cv2.watershed(imagege, markers)
  56. cnts, hierrache = cv2.findContours(segments, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
  57. circlesA = 0
  58. cubicCountA = 0
  59. for i in range(len(cnts)):
  60. if hierrache[0][i][3] != -1: continue
  61. c = cnts[i]
  62. (curr_x, curr_y), r = cv2.minEnclosingCircle(c)
  63. area = cv2.contourArea(c)
  64. circleArea = math.pi * r ** 2
  65. if area <= 2000:
  66. continue
  67. if area / circleArea >= 0.9:
  68. circlesA += 1
  69. else:
  70. cubicCountA += 1
  71. for i in range(len(cnts)):
  72. if hierrache[0][i][3] == -1:
  73. cv2.drawContours(imagege, cnts, i, (0, 255, 0), 10)
  74. cv2.putText(image, f"Circles count = {circles}, quadro count = {cubicCount}", (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
  75. 0.7, (127, 255, 255))
  76. cv2.putText(image, f"Image = {n}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX,
  77. 0.7, (127, 255, 255))
  78. cv2.imshow("Image", image)
  79. cv2.putText(imagege, f"Circles count = {circlesA}, quadro count = {cubicCountA}", (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
  80. 0.7, (127, 255, 255))
  81. cv2.imshow("Image2", imagege)
  82. cv2.destroyAllWindows()