Last active
June 6, 2018 05:03
-
-
Save tomitrescak/c31cee9c898ad7def32c4aaef8c1234d to your computer and use it in GitHub Desktop.
ZED C# Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Emgu.CV.Structure; | |
using sl; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace ZedTester | |
{ | |
public partial class Form1 : Form | |
{ | |
ZEDCamera zed; | |
InitParameters initParameters; | |
RuntimeParameters runtimeParameters; | |
bool newFrameAvailable; | |
public Form1() | |
{ | |
InitializeComponent(); | |
zed = ZEDCamera.GetInstance(); | |
initParameters = new InitParameters(); | |
initParameters.resolution = RESOLUTION.HD720; | |
initParameters.depthMode = DEPTH_MODE.QUALITY; | |
initParameters.depthStabilization = true; | |
initParameters.enableRightSideMeasure = false; // isStereoRig; | |
initParameters.depthMinimumDistance = 0.1f; | |
// create the camera | |
zed.CreateCamera(true); | |
// find version | |
var versionZED = "[SDK]: " + sl.ZEDCamera.GetSDKVersion().ToString() + " [Plugin]: " + sl.ZEDCamera.PluginVersion.ToString(); | |
this.versionInfo.Text = versionZED; | |
new Thread(InitZED).Start(); | |
} | |
bool zedReady = false; | |
ERROR_CODE LastInitStatus = ERROR_CODE.ERROR_CODE_LAST; | |
ERROR_CODE PreviousInitStatus; | |
Thread threadOpening; | |
Thread threadGrab; | |
bool openingLaunched; | |
bool running; | |
bool requestNewFrame; | |
object grabLock = new object(); | |
ulong cameraTimeStamp; | |
void OpenZEDInBackground() | |
{ | |
openingLaunched = true; | |
LastInitStatus = zed.Init(ref initParameters); | |
openingLaunched = false; | |
} | |
void InitZED() | |
{ | |
zedReady = false; | |
while (LastInitStatus != sl.ERROR_CODE.SUCCESS) | |
{ | |
//Initialize the camera | |
if (!openingLaunched) | |
{ | |
threadOpening = new Thread(new ThreadStart(OpenZEDInBackground)); | |
if (LastInitStatus != sl.ERROR_CODE.SUCCESS) | |
{ | |
PreviousInitStatus = LastInitStatus; | |
} | |
threadOpening.Start(); | |
} | |
Thread.Sleep(300); | |
} | |
//ZED has opened | |
if (LastInitStatus == sl.ERROR_CODE.SUCCESS) | |
{ | |
threadOpening.Join(); | |
//If not already launched, launch the grabbing thread | |
if (!running) | |
{ | |
running = true; | |
requestNewFrame = true; | |
threadGrab = new Thread(new ThreadStart(ThreadedZEDGrab)); | |
threadGrab.Start(); | |
} | |
zedReady = true; | |
} | |
} | |
private void ThreadedZEDGrab() | |
{ | |
runtimeParameters = new sl.RuntimeParameters(); | |
runtimeParameters.sensingMode = SENSING_MODE.STANDARD; | |
runtimeParameters.enableDepth = true; | |
// Don't change this ReferenceFrame. If we need normals in world frame, then we will do the convertion ourselves. | |
runtimeParameters.measure3DReferenceFrame = sl.REFERENCE_FRAME.CAMERA; | |
while (running) | |
{ | |
if (zed == null) | |
return; | |
AcquireImages(); | |
} | |
} | |
private void AcquireImages() | |
{ | |
if (requestNewFrame && zedReady) | |
{ | |
/// call grab() to request a new frame | |
sl.ERROR_CODE e = zed.Grab(ref runtimeParameters); | |
lock (grabLock) | |
{ | |
if (e == sl.ERROR_CODE.CAMERA_NOT_DETECTED) | |
{ | |
this.errorInfo.Text = "Camera not detected or disconnected."; | |
Thread.Sleep(10); | |
requestNewFrame = false; | |
} | |
else if (e == sl.ERROR_CODE.SUCCESS) | |
{ | |
//Save the timestamp | |
cameraTimeStamp = zed.GetCameraTimeStamp(); | |
this.errorInfo.Invoke((MethodInvoker)delegate { | |
// Running on the UI thread | |
this.errorInfo.Text = cameraTimeStamp.ToString(); | |
}); | |
// Indicate that a new frame is available and pause the thread until a new request is called | |
newFrameAvailable = true; | |
requestNewFrame = false; | |
int new_width = zed.ImageWidth; | |
int new_height = zed.ImageHeight; | |
var resolution = new Resolution((uint) new_width, (uint) new_height); | |
ZEDMat image_zed = new ZEDMat((uint) new_width, (uint) new_height, ZEDMat.MAT_TYPE.MAT_8U_C4); | |
zed.RetrieveImage(image_zed, VIEW.LEFT, ZEDMat.MEM.MEM_CPU, resolution); | |
// this I can do ... | |
image_zed.Write("save.jpg"); | |
Emgu.CV.Image<Bgr, byte> image = new Emgu.CV.Image<Bgr, byte>((int) new_width, (int) new_height); | |
//TODO: ??? | |
} | |
} | |
} | |
else | |
{ | |
//to avoid "overheat" | |
Thread.Sleep(1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment