Edge Detector
This example performs edge detection on 3 different inputs: left, right and RGB camera. HW accelerated sobel filter 3x3 is used. Sobel filter parameters can be changed by keys 1 and 2.Demo

Setup
This example requires the DepthAI v3 API, see installation instructions.Source code
Python
Python
Python
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5
6# Create pipeline
7pipeline = dai.Pipeline()
8
9# Define cameras
10camRgb = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
11monoLeft = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B)
12monoRight = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C)
13
14# Request outputs
15frameWidth, frameHeight = 1920, 1080
16rgbOut = camRgb.requestOutput((frameWidth, frameHeight), type=dai.ImgFrame.Type.GRAY8)
17leftOut = monoLeft.requestOutput((640, 400), type=dai.ImgFrame.Type.GRAY8)
18rightOut = monoRight.requestOutput((640, 400), type=dai.ImgFrame.Type.GRAY8)
19
20# Define edge detectors
21edgeDetectorLeft = pipeline.create(dai.node.EdgeDetector)
22edgeDetectorRight = pipeline.create(dai.node.EdgeDetector)
23edgeDetectorRgb = pipeline.create(dai.node.EdgeDetector)
24edgeDetectorRgb.setMaxOutputFrameSize(frameWidth * frameHeight)
25
26# Create input queues
27edgeCfgLeftQueue = edgeDetectorLeft.inputConfig.createInputQueue()
28edgeCfgRightQueue = edgeDetectorRight.inputConfig.createInputQueue()
29edgeCfgRgbQueue = edgeDetectorRgb.inputConfig.createInputQueue()
30
31# Link camera outputs to edge detectors
32leftOut.link(edgeDetectorLeft.inputImage)
33rightOut.link(edgeDetectorRight.inputImage)
34rgbOut.link(edgeDetectorRgb.inputImage)
35
36# Create output queues
37edgeLeftQueue = edgeDetectorLeft.outputImage.createOutputQueue()
38edgeRightQueue = edgeDetectorRight.outputImage.createOutputQueue()
39edgeRgbQueue = edgeDetectorRgb.outputImage.createOutputQueue()
40
41# Start pipeline
42pipeline.start()
43print("Switch between sobel filter kernels using keys '1' and '2'")
44while pipeline.isRunning():
45 edgeLeft = edgeLeftQueue.get()
46 edgeRight = edgeRightQueue.get()
47 edgeRgb = edgeRgbQueue.get()
48
49 # Convert to OpenCV format
50 cv2.imshow("edge left", edgeLeft.getCvFrame())
51 cv2.imshow("edge right", edgeRight.getCvFrame())
52 cv2.imshow("edge rgb", edgeRgb.getCvFrame())
53
54 key = cv2.waitKey(1)
55 if key == ord('q'):
56 break
57
58 if key == ord('1'):
59 print("Switching sobel filter kernel.")
60 cfg = dai.EdgeDetectorConfig()
61 sobelHorizontalKernel = [[1, 0, -1], [2, 0, -2], [1, 0, -1]]
62 sobelVerticalKernel = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
63 cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel)
64 edgeCfgLeftQueue.send(cfg)
65 edgeCfgRightQueue.send(cfg)
66 edgeCfgRgbQueue.send(cfg)
67
68 if key == ord('2'):
69 print("Switching sobel filter kernel.")
70 cfg = dai.EdgeDetectorConfig()
71 sobelHorizontalKernel = [[3, 0, -3], [10, 0, -10], [3, 0, -3]]
72 sobelVerticalKernel = [[3, 10, 3], [0, 0, 0], [-3, -10, -3]]
73 cfg.setSobelFilterKernels(sobelHorizontalKernel, sobelVerticalKernel)
74 edgeCfgLeftQueue.send(cfg)
75 edgeCfgRightQueue.send(cfg)
76 edgeCfgRgbQueue.send(cfg)
Pipeline
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.