edge-detector.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import cv2
  2. import depthai as dai
  3. # Create pipeline
  4. pipeline = dai.Pipeline()
  5. # Define sources and outputs
  6. camRgb = pipeline.create(dai.node.ColorCamera)
  7. monoLeft = pipeline.create(dai.node.MonoCamera)
  8. monoRight = pipeline.create(dai.node.MonoCamera)
  9. edgeDetectorLeft = pipeline.create(dai.node.EdgeDetector)
  10. edgeDetectorRight = pipeline.create(dai.node.EdgeDetector)
  11. edgeDetectorRgb = pipeline.create(dai.node.EdgeDetector)
  12. xoutEdgeLeft = pipeline.create(dai.node.XLinkOut)
  13. xoutEdgeRight = pipeline.create(dai.node.XLinkOut)
  14. xoutEdgeRgb = pipeline.create(dai.node.XLinkOut)
  15. xinEdgeCfg = pipeline.create(dai.node.XLinkIn)
  16. edgeLeftStr = "edge left"
  17. edgeRightStr = "edge right"
  18. edgeRgbStr = "edge rgb"
  19. edgeCfgStr = "edge cfg"
  20. xoutEdgeLeft.setStreamName(edgeLeftStr)
  21. xoutEdgeRight.setStreamName(edgeRightStr)
  22. xoutEdgeRgb.setStreamName(edgeRgbStr)
  23. xinEdgeCfg.setStreamName(edgeCfgStr)
  24. # Properties
  25. camRgb.setBoardSocket(dai.CameraBoardSocket.RGB)
  26. camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
  27. monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  28. monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
  29. monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
  30. monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
  31. edgeDetectorRgb.setMaxOutputFrameSize(camRgb.getVideoWidth() * camRgb.getVideoHeight())
  32. # Linking
  33. monoLeft.out.link(edgeDetectorLeft.inputImage)
  34. monoRight.out.link(edgeDetectorRight.inputImage)
  35. camRgb.video.link(edgeDetectorRgb.inputImage)
  36. edgeDetectorLeft.outputImage.link(xoutEdgeLeft.input)
  37. edgeDetectorRight.outputImage.link(xoutEdgeRight.input)
  38. edgeDetectorRgb.outputImage.link(xoutEdgeRgb.input)
  39. xinEdgeCfg.out.link(edgeDetectorLeft.inputConfig)
  40. xinEdgeCfg.out.link(edgeDetectorRight.inputConfig)
  41. xinEdgeCfg.out.link(edgeDetectorRgb.inputConfig)
  42. # Connect to device and start pipeline
  43. with dai.Device(pipeline) as device:
  44. # Output/input queues
  45. edgeLeftQueue = device.getOutputQueue(edgeLeftStr, 8, False)
  46. edgeRightQueue = device.getOutputQueue(edgeRightStr, 8, False)
  47. edgeRgbQueue = device.getOutputQueue(edgeRgbStr, 8, False)
  48. edgeCfgQueue = device.getInputQueue(edgeCfgStr)
  49. print("Switch between sobel filter kernels using keys '1' and '2'")
  50. while(True):
  51. edgeLeft = edgeLeftQueue.get()
  52. edgeRight = edgeRightQueue.get()
  53. edgeRgb = edgeRgbQueue.get()
  54. edgeLeftFrame = edgeLeft.getFrame()
  55. edgeRightFrame = edgeRight.getFrame()
  56. edgeRgbFrame = edgeRgb.getFrame()
  57. # Show the frame
  58. cv2.imshow(edgeLeftStr, edgeLeftFrame)
  59. cv2.imshow(edgeRightStr, edgeRightFrame)
  60. cv2.imshow(edgeRgbStr, edgeRgbFrame)
  61. key = cv2.waitKey(1)
  62. if key == 27:
  63. break
  64. if key == ord('1'):
  65. print("Switching sobel filter kernel.")
  66. cfg = dai.EdgeDetectorConfig()
  67. sobelHorizontalKernel = [[1, 0, -1], [2, 0, -2], [1, 0, -1]]
  68. sobelVerticalKernel = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
  69. cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel)
  70. edgeCfgQueue.send(cfg)
  71. if key == ord('2'):
  72. print("Switching sobel filter kernel.")
  73. cfg = dai.EdgeDetectorConfig()
  74. sobelHorizontalKernel = [[3, 0, -3], [10, 0, -10], [3, 0, -3]]
  75. sobelVerticalKernel = [[3, 10, 3], [0, 0, 0], [-3, -10, -3]]
  76. cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel)
  77. edgeCfgQueue.send(cfg)