Skip to content

Commit afc2790

Browse files
authored
Merge pull request #98 from zivid/2025-04-07-update-csharp-samples
Samples: Update to SDK 2.15.0
2 parents 8670472 + 51793b3 commit afc2790

File tree

25 files changed

+433
-370
lines changed

25 files changed

+433
-370
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# C\# samples
22

3-
This repository contains csharp code samples for Zivid SDK v2.14.2. For
3+
This repository contains csharp code samples for Zivid SDK v2.15.0. For
44
tested compatibility with earlier SDK versions, please check out
55
[accompanying
66
releases](https://github.com/zivid/zivid-csharp-samples/tree/master/../../releases).

source/Applications/Advanced/Downsample/Downsample.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,38 @@ static int Main()
1717
var dataFile =
1818
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/Zivid/Zivid3D.zdf";
1919
Console.WriteLine("Reading ZDF frame from file: " + dataFile);
20-
var frame = new Zivid.NET.Frame(dataFile);
2120

22-
Console.WriteLine("Getting point cloud from frame");
23-
var pointCloud = frame.PointCloud;
21+
using (var frame = new Zivid.NET.Frame(dataFile))
22+
{
23+
Console.WriteLine("Getting point cloud from frame");
24+
var pointCloud = frame.PointCloud;
2425

25-
Console.WriteLine("Size of point cloud before downsampling: " + pointCloud.Size + " data point");
26+
Console.WriteLine("Size of point cloud before downsampling: " + pointCloud.Size + " data point");
2627

27-
Console.WriteLine("Downsampling point cloud");
28-
Console.WriteLine("This does not modify the current point cloud but returns");
29-
Console.WriteLine("the downsampled point cloud as a new point cloud instance.");
30-
var downsampledPointCloud = pointCloud.Downsampled(Zivid.NET.PointCloud.Downsampling.By2x2);
28+
Console.WriteLine("Downsampling point cloud");
29+
Console.WriteLine("This does not modify the current point cloud but returns");
30+
Console.WriteLine("the downsampled point cloud as a new point cloud instance.");
31+
var downsampledPointCloud = pointCloud.Downsampled(Zivid.NET.PointCloud.Downsampling.By2x2);
3132

32-
Console.WriteLine("Size of point cloud after downsampling: " + downsampledPointCloud.Size + " data points");
33+
Console.WriteLine("Size of point cloud after downsampling: " + downsampledPointCloud.Size + " data points");
3334

34-
Console.WriteLine("Downsampling point cloud (in-place)");
35-
Console.WriteLine("This modifies the current point cloud.");
36-
pointCloud.Downsample(Zivid.NET.PointCloud.Downsampling.By2x2);
35+
Console.WriteLine("Downsampling point cloud (in-place)");
36+
Console.WriteLine("This modifies the current point cloud.");
37+
pointCloud.Downsample(Zivid.NET.PointCloud.Downsampling.By2x2);
3738

38-
Console.WriteLine("Size of point cloud after downsampling: " + pointCloud.Size + " data points");
39+
Console.WriteLine("Size of point cloud after downsampling: " + pointCloud.Size + " data points");
3940

40-
Console.WriteLine("Setting up visualization");
41-
var visualizer = new Zivid.NET.Visualization.Visualizer();
41+
Console.WriteLine("Setting up visualization");
42+
var visualizer = new Zivid.NET.Visualization.Visualizer();
4243

43-
Console.WriteLine("Visualizing point cloud");
44-
visualizer.Show(pointCloud);
45-
visualizer.ShowMaximized();
46-
visualizer.ResetToFit();
44+
Console.WriteLine("Visualizing point cloud");
45+
visualizer.Show(pointCloud);
46+
visualizer.ShowMaximized();
47+
visualizer.ResetToFit();
4748

48-
Console.WriteLine("Running visualizer. Blocking until window closes.");
49-
visualizer.Run();
49+
Console.WriteLine("Running visualizer. Blocking until window closes.");
50+
visualizer.Run();
51+
}
5052
}
5153
catch (Exception ex)
5254
{

source/Applications/Advanced/HandEyeCalibration/HandEyeCalibration/HandEyeCalibration.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,39 +103,42 @@ public static void HandleAddPose(ref uint currentPoseId, ref List<HandEyeInput>
103103
Console.Write("Detecting calibration object in point cloud");
104104
if (calibrationObject.Equals("c", StringComparison.CurrentCultureIgnoreCase))
105105
{
106-
var frame = Zivid.NET.Calibration.Detector.CaptureCalibrationBoard(camera);
107-
var detectionResult = Detector.DetectCalibrationBoard(frame);
108-
109-
if (detectionResult.Valid())
106+
using (var frame = Zivid.NET.Calibration.Detector.CaptureCalibrationBoard(camera))
110107
{
111-
Console.WriteLine("Calibration board detected");
112-
handEyeInput.Add(new HandEyeInput(robotPose, detectionResult));
113-
++currentPoseId;
114-
}
115-
else
116-
{
117-
Console.WriteLine("Failed to detect calibration board, ensure that the entire board is in the view of the camera");
108+
var detectionResult = Detector.DetectCalibrationBoard(frame);
109+
110+
if (detectionResult.Valid())
111+
{
112+
Console.WriteLine("Calibration board detected");
113+
handEyeInput.Add(new HandEyeInput(robotPose, detectionResult));
114+
++currentPoseId;
115+
}
116+
else
117+
{
118+
Console.WriteLine("Failed to detect calibration board, ensure that the entire board is in the view of the camera");
119+
}
118120
}
119121
}
120122
else if (calibrationObject.Equals("m", StringComparison.CurrentCultureIgnoreCase))
121123
{
122-
var frame = AssistedCapture(camera);
123-
124-
var markerDictionary = Zivid.NET.MarkerDictionary.Aruco4x4_50;
125-
var markerIds = new List<int> { 1, 2, 3 };
126-
127-
Console.WriteLine("Detecting arUco marker IDs " + string.Join(", ", markerIds));
128-
var detectionResult = Detector.DetectMarkers(frame, markerIds, markerDictionary);
129-
130-
if (detectionResult.Valid())
131-
{
132-
Console.WriteLine("ArUco marker(s) detected: " + detectionResult.DetectedMarkers().Length);
133-
handEyeInput.Add(new HandEyeInput(robotPose, detectionResult));
134-
++currentPoseId;
135-
}
136-
else
124+
using (var frame = AssistedCapture(camera))
137125
{
138-
Console.WriteLine("Failed to detect any ArUco markers, ensure that at least one ArUco marker is in the view of the camera");
126+
var markerDictionary = Zivid.NET.MarkerDictionary.Aruco4x4_50;
127+
var markerIds = new List<int> { 1, 2, 3 };
128+
129+
Console.WriteLine("Detecting arUco marker IDs " + string.Join(", ", markerIds));
130+
var detectionResult = Detector.DetectMarkers(frame, markerIds, markerDictionary);
131+
132+
if (detectionResult.Valid())
133+
{
134+
Console.WriteLine("ArUco marker(s) detected: " + detectionResult.DetectedMarkers().Length);
135+
handEyeInput.Add(new HandEyeInput(robotPose, detectionResult));
136+
++currentPoseId;
137+
}
138+
else
139+
{
140+
Console.WriteLine("Failed to detect any ArUco markers, ensure that at least one ArUco marker is in the view of the camera");
141+
}
139142
}
140143
}
141144
}

source/Applications/Advanced/HandEyeCalibration/UtilizeHandEyeCalibration/UtilizeHandEyeCalibration.cs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -112,51 +112,54 @@ static int Main()
112112
var dataFile =
113113
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/Zivid/" + fileName;
114114
Console.WriteLine("Reading point cloud from file: " + dataFile);
115-
var frame = new Zivid.NET.Frame(dataFile);
116-
var pointCloud = frame.PointCloud;
117115

118-
loopContinue = true;
119-
while (loopContinue)
116+
using (var frame = new Zivid.NET.Frame(dataFile))
120117
{
121-
switch (Interaction.EnterCommand())
118+
var pointCloud = frame.PointCloud;
119+
120+
loopContinue = true;
121+
while (loopContinue)
122122
{
123-
case Command.TransformSinglePoint:
123+
switch (Interaction.EnterCommand())
124+
{
125+
case Command.TransformSinglePoint:
124126

125-
Console.WriteLine("Transforming single point");
127+
Console.WriteLine("Transforming single point");
126128

127-
var xyz = pointCloud.CopyPointsXYZW();
129+
var xyz = pointCloud.CopyPointsXYZW();
128130

129-
var pickingPoint = new float[] { xyz[imageCoordinateY, imageCoordinateX, 0],
130-
xyz[imageCoordinateY, imageCoordinateX, 1],
131-
xyz[imageCoordinateY, imageCoordinateX, 2],
132-
xyz[imageCoordinateY, imageCoordinateX, 3] };
131+
var pickingPoint = new float[] { xyz[imageCoordinateY, imageCoordinateX, 0],
132+
xyz[imageCoordinateY, imageCoordinateX, 1],
133+
xyz[imageCoordinateY, imageCoordinateX, 2],
134+
xyz[imageCoordinateY, imageCoordinateX, 3] };
133135

134-
Vector<float> pointInCameraFrame = CreateVector.DenseOfArray(pickingPoint);
135-
Console.WriteLine("Point coordinates in camera reference frame: " + pointInCameraFrame);
136+
Vector<float> pointInCameraFrame = CreateVector.DenseOfArray(pickingPoint);
137+
Console.WriteLine("Point coordinates in camera reference frame: " + pointInCameraFrame);
136138

137-
Console.WriteLine("Transforming (picking) point from camera to robot base reference frame");
138-
Vector<float> pointInBaseFrame = transformBaseToCameraMath * pointInCameraFrame;
139+
Console.WriteLine("Transforming (picking) point from camera to robot base reference frame");
140+
Vector<float> pointInBaseFrame = transformBaseToCameraMath * pointInCameraFrame;
139141

140-
Console.WriteLine("Point coordinates in robot base reference frame: " + pointInBaseFrame);
142+
Console.WriteLine("Point coordinates in robot base reference frame: " + pointInBaseFrame);
141143

142-
loopContinue = false;
143-
break;
144+
loopContinue = false;
145+
break;
144146

145-
case Command.TransformPointCloud:
147+
case Command.TransformPointCloud:
146148

147-
Console.WriteLine("Transforming point cloud");
149+
Console.WriteLine("Transforming point cloud");
148150

149-
var transformBaseToCamera = mathDotNetToZivid(transformBaseToCameraMath);
150-
pointCloud.Transform(transformBaseToCamera);
151+
var transformBaseToCamera = mathDotNetToZivid(transformBaseToCameraMath);
152+
pointCloud.Transform(transformBaseToCamera);
151153

152-
var saveFile = "ZividGemTransformed.zdf";
153-
Console.WriteLine("Saving point cloud to file: " + saveFile);
154-
frame.Save(saveFile);
154+
var saveFile = "ZividGemTransformed.zdf";
155+
Console.WriteLine("Saving point cloud to file: " + saveFile);
156+
frame.Save(saveFile);
155157

156-
loopContinue = false;
157-
break;
158+
loopContinue = false;
159+
break;
158160

159-
case Command.Unknown: Console.WriteLine("Entered unknown command"); break;
161+
case Command.Unknown: Console.WriteLine("Entered unknown command"); break;
162+
}
160163
}
161164
}
162165
}

source/Applications/Advanced/MultiCameraCalibration/MultiCameraCalibration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ static int Main()
3030
{
3131
Console.WriteLine("Detecting checkerboard in point cloud");
3232
var detectionResult = Detector.DetectCalibrationBoard(frame);
33-
if (detectionResult)
33+
if (detectionResult.Valid())
3434
{
3535
detectionResults.Add(detectionResult);
3636
serialNumbers.Add(serialNumber);
3737
}
3838
else
3939
{
4040
throw new System.InvalidOperationException(
41-
"Could not detect checkerboard. Please ensure it is visible from all cameras.");
41+
"Could not detect checkerboard. Please ensure it is visible from all cameras. " + detectionResult.StatusDescription());
4242
}
4343
}
4444
}

source/Applications/Advanced/ROIBoxViaArucoMarker/ROIBoxViaArucoMarker.cs

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -35,75 +35,79 @@ static int Main()
3535
};
3636
settings.Color = settings2D;
3737

38-
var originalFrame = camera.Capture2D3D(settings);
39-
var pointCloud = originalFrame.PointCloud;
40-
41-
Console.WriteLine("Displaying the original point cloud");
42-
VisualizeZividPointCloud(originalFrame);
43-
44-
Console.WriteLine("Configuring ROI box based on bin size and checkerboard placement");
45-
float roiBoxLength = 545F;
46-
float roiBoxWidth = 345F;
47-
float roiBoxHeight = 150F;
48-
49-
// Coordinates are relative to the checkerboard origin which lies in the intersection between the four checkers
50-
// in the top-left corner of the checkerboard: Positive x-axis is "East", y-axis is "South" and z-axis is "Down"
51-
var roiBoxLowerRightCornerInCheckerboardFrame = new Zivid.NET.PointXYZ
52-
{
53-
x = 240F,
54-
y = 30F,
55-
z = 5F
56-
};
57-
var roiBoxUpperRightCornerInCheckerboardFrame = new Zivid.NET.PointXYZ
38+
using (var originalFrame = camera.Capture2D3D(settings))
5839
{
59-
x = roiBoxLowerRightCornerInCheckerboardFrame.x,
60-
y = roiBoxLowerRightCornerInCheckerboardFrame.y - roiBoxWidth,
61-
z = roiBoxLowerRightCornerInCheckerboardFrame.z
62-
};
63-
var roiBoxLowerLeftCornerInCheckerboardFrame = new Zivid.NET.PointXYZ
64-
{
65-
x = roiBoxLowerRightCornerInCheckerboardFrame.x - roiBoxLength,
66-
y = roiBoxLowerRightCornerInCheckerboardFrame.y,
67-
z = roiBoxLowerRightCornerInCheckerboardFrame.z
68-
69-
};
70-
71-
var pointOInCheckerboardFrame = roiBoxLowerRightCornerInCheckerboardFrame;
72-
var pointAInCheckerboardFrame = roiBoxUpperRightCornerInCheckerboardFrame;
73-
var pointBInCheckerboardFrame = roiBoxLowerLeftCornerInCheckerboardFrame;
74-
75-
Console.WriteLine("Configuring ArUco marker");
76-
var markerDictionary = Zivid.NET.MarkerDictionary.Aruco4x4_50;
77-
var markerId = new List<int> { 1 };
78-
79-
Console.WriteLine("Detecting ArUco marker");
80-
var detectionResult = Detector.DetectMarkers(originalFrame, markerId, markerDictionary);
81-
82-
if (!detectionResult.Valid())
83-
{
84-
Console.WriteLine("No ArUco markers detected");
85-
return 1;
40+
var pointCloud = originalFrame.PointCloud;
41+
42+
Console.WriteLine("Displaying the original point cloud");
43+
VisualizeZividPointCloud(originalFrame);
44+
45+
Console.WriteLine("Configuring ROI box based on bin size and checkerboard placement");
46+
float roiBoxLength = 545F;
47+
float roiBoxWidth = 345F;
48+
float roiBoxHeight = 150F;
49+
50+
// Coordinates are relative to the checkerboard origin which lies in the intersection between the four checkers
51+
// in the top-left corner of the checkerboard: Positive x-axis is "East", y-axis is "South" and z-axis is "Down"
52+
var roiBoxLowerRightCornerInCheckerboardFrame = new Zivid.NET.PointXYZ
53+
{
54+
x = 240F,
55+
y = 30F,
56+
z = 5F
57+
};
58+
var roiBoxUpperRightCornerInCheckerboardFrame = new Zivid.NET.PointXYZ
59+
{
60+
x = roiBoxLowerRightCornerInCheckerboardFrame.x,
61+
y = roiBoxLowerRightCornerInCheckerboardFrame.y - roiBoxWidth,
62+
z = roiBoxLowerRightCornerInCheckerboardFrame.z
63+
};
64+
var roiBoxLowerLeftCornerInCheckerboardFrame = new Zivid.NET.PointXYZ
65+
{
66+
x = roiBoxLowerRightCornerInCheckerboardFrame.x - roiBoxLength,
67+
y = roiBoxLowerRightCornerInCheckerboardFrame.y,
68+
z = roiBoxLowerRightCornerInCheckerboardFrame.z
69+
70+
};
71+
72+
var pointOInCheckerboardFrame = roiBoxLowerRightCornerInCheckerboardFrame;
73+
var pointAInCheckerboardFrame = roiBoxUpperRightCornerInCheckerboardFrame;
74+
var pointBInCheckerboardFrame = roiBoxLowerLeftCornerInCheckerboardFrame;
75+
76+
Console.WriteLine("Configuring ArUco marker");
77+
var markerDictionary = Zivid.NET.MarkerDictionary.Aruco4x4_50;
78+
var markerId = new List<int> { 1 };
79+
80+
Console.WriteLine("Detecting ArUco marker");
81+
var detectionResult = Detector.DetectMarkers(originalFrame, markerId, markerDictionary);
82+
83+
if (!detectionResult.Valid())
84+
{
85+
Console.WriteLine("No ArUco markers detected");
86+
return 1;
87+
}
88+
89+
Console.WriteLine("Estimating pose of detected ArUco marker");
90+
var cameraToMarkerTransform = new Zivid.NET.Matrix4x4(detectionResult.DetectedMarkers()[0].Pose().ToMatrix());
91+
92+
Console.WriteLine("Transforming the ROI base frame points to the camera frame");
93+
var roiPointsInCameraFrame = TransformPoints(
94+
new List<Zivid.NET.PointXYZ> { pointOInCheckerboardFrame, pointAInCheckerboardFrame, pointBInCheckerboardFrame },
95+
cameraToMarkerTransform);
96+
97+
Console.WriteLine("Setting the ROI");
98+
settings.RegionOfInterest.Box.Enabled = true;
99+
settings.RegionOfInterest.Box.PointO = roiPointsInCameraFrame[0];
100+
settings.RegionOfInterest.Box.PointA = roiPointsInCameraFrame[1];
101+
settings.RegionOfInterest.Box.PointB = roiPointsInCameraFrame[2];
102+
settings.RegionOfInterest.Box.Extents = new Zivid.NET.Range<double>(-10, roiBoxHeight);
103+
104+
using (var roiFrame = camera.Capture2D3D(settings))
105+
{
106+
107+
Console.WriteLine("Displaying the ROI-filtered point cloud");
108+
VisualizeZividPointCloud(roiFrame);
109+
}
86110
}
87-
88-
Console.WriteLine("Estimating pose of detected ArUco marker");
89-
var cameraToMarkerTransform = new Zivid.NET.Matrix4x4(detectionResult.DetectedMarkers()[0].Pose().ToMatrix());
90-
91-
Console.WriteLine("Transforming the ROI base frame points to the camera frame");
92-
var roiPointsInCameraFrame = TransformPoints(
93-
new List<Zivid.NET.PointXYZ> { pointOInCheckerboardFrame, pointAInCheckerboardFrame, pointBInCheckerboardFrame },
94-
cameraToMarkerTransform);
95-
96-
Console.WriteLine("Setting the ROI");
97-
settings.RegionOfInterest.Box.Enabled = true;
98-
settings.RegionOfInterest.Box.PointO = roiPointsInCameraFrame[0];
99-
settings.RegionOfInterest.Box.PointA = roiPointsInCameraFrame[1];
100-
settings.RegionOfInterest.Box.PointB = roiPointsInCameraFrame[2];
101-
settings.RegionOfInterest.Box.Extents = new Zivid.NET.Range<double>(-10, roiBoxHeight);
102-
103-
var roiFrame = camera.Capture2D3D(settings);
104-
105-
Console.WriteLine("Displaying the ROI-filtered point cloud");
106-
VisualizeZividPointCloud(roiFrame);
107111
}
108112
catch (Exception ex)
109113
{

0 commit comments

Comments
 (0)