ZEDCameraSettings.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using System.Runtime.InteropServices;
  3. /// <summary>
  4. /// Stores the camera settings (brightness/contrast, gain/exposure, etc.) and interfaces with the ZED
  5. /// when they need to be loaded or changed.
  6. /// Created by ZEDCamera and referenced by ZEDCameraSettingsEditor.
  7. /// </summary><remarks>
  8. /// The actual settings themselves are stored in an instance of CameraSettings, for easier manipulation.
  9. /// But this class provides accessors for each value within it.
  10. /// </remarks>
  11. public class ZEDCameraSettings
  12. {
  13. #region DLL Calls
  14. const string nameDll = sl.ZEDCommon.NameDLL;
  15. [DllImport(nameDll, EntryPoint = "sl_set_video_settings")]
  16. private static extern void dllz_set_video_settings(int id, int mode, int value, int usedefault);
  17. [DllImport(nameDll, EntryPoint = "sl_get_video_settings")]
  18. private static extern int dllz_get_video_settings(int id, int mode);
  19. #endregion
  20. /// <summary>
  21. /// Container for ZED camera settings, with constructors for easily creating default or specific values
  22. /// or making duplicate instances.
  23. /// </summary>
  24. public class CameraSettings
  25. {
  26. /// <summary>
  27. /// Holds an int for each setting, with indexes corresponding to sl.CAMERA_SETTINGS.
  28. /// </summary>
  29. public int[] settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
  30. /// <summary>
  31. /// Constructor. Call without arguments to set all values to default.
  32. /// </summary>
  33. /// <param name="brightness">Camera's brightness setting.</param>
  34. /// <param name="contrast">Camera's contrast setting.</param>
  35. /// <param name="hue">Camera's hue setting.</param>
  36. /// <param name="saturation">Camera's saturation setting.</param>
  37. /// <param name="sharpness">Camera's sharpness setting.</param>
  38. /// <param name="gamma">Camera's gamma setting.</param>
  39. /// <param name="whiteBalance">Camera's white balance setting. -1 means automatic.</param>
  40. /// <param name="gain">Camera's gain setting. -1 means automatic.</param>
  41. /// <param name="exposure">Camera's exposure setting. -1 means automatic.</param>
  42. public CameraSettings(int brightness = 4, int contrast = 4, int hue = 0, int saturation = 4, int sharpness = 3, int gamma = 5,int whiteBalance = -1, int gain = -1, int exposure = -1,int ledStatus = 1)
  43. {
  44. settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
  45. settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = brightness;
  46. settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = contrast;
  47. settings[(int)sl.CAMERA_SETTINGS.SATURATION] = saturation;
  48. settings[(int)sl.CAMERA_SETTINGS.HUE] = hue;
  49. settings[(int)sl.CAMERA_SETTINGS.SHARPNESS] = sharpness;
  50. settings[(int)sl.CAMERA_SETTINGS.GAMMA] = gamma;
  51. settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = whiteBalance;
  52. settings[(int)sl.CAMERA_SETTINGS.GAIN] = gain;
  53. settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = exposure;
  54. settings[(int)sl.CAMERA_SETTINGS.LED_STATUS] = ledStatus;
  55. }
  56. /// <summary>
  57. /// Constructor. Sets settings to match another CameraSettings passed in the argument.
  58. /// </summary>
  59. /// <param name="other"></param>
  60. public CameraSettings(CameraSettings other)
  61. {
  62. settings = new int[System.Enum.GetNames(typeof(sl.CAMERA_SETTINGS)).Length];
  63. settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = other.settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS];
  64. settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = other.settings[(int)sl.CAMERA_SETTINGS.CONTRAST];
  65. settings[(int)sl.CAMERA_SETTINGS.SATURATION] = other.settings[(int)sl.CAMERA_SETTINGS.SATURATION];
  66. settings[(int)sl.CAMERA_SETTINGS.HUE] = other.settings[(int)sl.CAMERA_SETTINGS.HUE];
  67. settings[(int)sl.CAMERA_SETTINGS.SHARPNESS] = other.settings[(int)sl.CAMERA_SETTINGS.SHARPNESS];
  68. settings[(int)sl.CAMERA_SETTINGS.GAMMA] = other.settings[(int)sl.CAMERA_SETTINGS.GAMMA];
  69. settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = other.settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE];
  70. settings[(int)sl.CAMERA_SETTINGS.GAIN] = other.settings[(int)sl.CAMERA_SETTINGS.GAIN];
  71. settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = other.settings[(int)sl.CAMERA_SETTINGS.EXPOSURE];
  72. settings[(int)sl.CAMERA_SETTINGS.LED_STATUS] = other.settings[(int)sl.CAMERA_SETTINGS.LED_STATUS];
  73. }
  74. /// <summary>
  75. /// Returns a new instance of CameraSettings with the same settings as the instance this function was called with.
  76. /// </summary>
  77. /// <returns>New instance of CameraSettings.</returns>
  78. public CameraSettings Clone()
  79. {
  80. return new CameraSettings(this);
  81. }
  82. /// <summary>
  83. /// ZED camera's brightness setting.
  84. /// </summary>
  85. public int Brightness
  86. {
  87. get
  88. {
  89. return settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS];
  90. }
  91. set
  92. {
  93. settings[(int)sl.CAMERA_SETTINGS.BRIGHTNESS] = value;
  94. }
  95. }
  96. /// <summary>
  97. /// ZED camera's saturation setting.
  98. /// </summary>
  99. public int Saturation
  100. {
  101. get
  102. {
  103. return settings[(int)sl.CAMERA_SETTINGS.SATURATION];
  104. }
  105. set
  106. {
  107. settings[(int)sl.CAMERA_SETTINGS.SATURATION] = value;
  108. }
  109. }
  110. /// <summary>
  111. /// ZED camera's hue setting.
  112. /// </summary>
  113. public int Hue
  114. {
  115. get
  116. {
  117. return settings[(int)sl.CAMERA_SETTINGS.HUE];
  118. }
  119. set
  120. {
  121. settings[(int)sl.CAMERA_SETTINGS.HUE] = value;
  122. }
  123. }
  124. /// <summary>
  125. /// ZED camera's contrast setting.
  126. /// </summary>
  127. public int Contrast
  128. {
  129. get
  130. {
  131. return settings[(int)sl.CAMERA_SETTINGS.CONTRAST];
  132. }
  133. set
  134. {
  135. settings[(int)sl.CAMERA_SETTINGS.CONTRAST] = value;
  136. }
  137. }
  138. /// <summary>
  139. /// ZED camera's contrast setting.
  140. /// </summary>
  141. public int Sharpness
  142. {
  143. get
  144. {
  145. return settings[(int)sl.CAMERA_SETTINGS.SHARPNESS];
  146. }
  147. set
  148. {
  149. settings[(int)sl.CAMERA_SETTINGS.SHARPNESS] = value;
  150. }
  151. }
  152. /// <summary>
  153. /// ZED camera's contrast setting.
  154. /// </summary>
  155. public int Gamma
  156. {
  157. get
  158. {
  159. return settings[(int)sl.CAMERA_SETTINGS.GAMMA];
  160. }
  161. set
  162. {
  163. settings[(int)sl.CAMERA_SETTINGS.GAMMA] = value;
  164. }
  165. }
  166. /// <summary>
  167. /// ZED camera's gain setting. -1 means automatic.
  168. /// </summary>
  169. public int Gain
  170. {
  171. get
  172. {
  173. return settings[(int)sl.CAMERA_SETTINGS.GAIN];
  174. }
  175. set
  176. {
  177. settings[(int)sl.CAMERA_SETTINGS.GAIN] = value;
  178. }
  179. }
  180. /// <summary>
  181. /// ZED camera's exposure setting. -1 means automatic.
  182. /// </summary>
  183. public int Exposure
  184. {
  185. get
  186. {
  187. return settings[(int)sl.CAMERA_SETTINGS.EXPOSURE];
  188. }
  189. set
  190. {
  191. settings[(int)sl.CAMERA_SETTINGS.EXPOSURE] = value;
  192. }
  193. }
  194. /// <summary>
  195. /// ZED camera's white balance setting. -1 means automatic.
  196. /// </summary>
  197. public int WhiteBalance
  198. {
  199. get
  200. {
  201. return settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE];
  202. }
  203. set
  204. {
  205. settings[(int)sl.CAMERA_SETTINGS.WHITEBALANCE] = value;
  206. }
  207. }
  208. /// <summary>
  209. /// ZED camera's LED status
  210. /// </summary>
  211. public int LEDStatus
  212. {
  213. get
  214. {
  215. return settings[(int)sl.CAMERA_SETTINGS.LED_STATUS];
  216. }
  217. set
  218. {
  219. settings[(int)sl.CAMERA_SETTINGS.LED_STATUS] = value;
  220. }
  221. }
  222. }
  223. /// <summary>
  224. /// Reference to the settings container object.
  225. /// </summary>
  226. private CameraSettings settings_;
  227. /// <summary>
  228. /// Reference to the settings container object.
  229. /// </summary>
  230. public CameraSettings Settings
  231. {
  232. get { return settings_.Clone(); }
  233. }
  234. /// <summary>
  235. /// Whether exposure is set to automatic.
  236. /// </summary>
  237. public bool auto = true;
  238. /// <summary>
  239. /// Whether white balance is set to automatic.
  240. /// </summary>
  241. public bool whiteBalanceAuto = true;
  242. /// <summary>
  243. /// Constructor. Creates a new instance of CameraSettings to contain all settings values.
  244. /// </summary>
  245. public ZEDCameraSettings()
  246. {
  247. settings_ = new CameraSettings();
  248. }
  249. /// <summary>
  250. /// Applies all settings from the container to the actual ZED camera.
  251. /// </summary>
  252. /// <param name="zedCamera">Current instance of the ZEDCamera wrapper.</param>
  253. public void SetSettings(sl.ZEDCamera zedCamera)
  254. {
  255. if (zedCamera != null)
  256. {
  257. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, settings_.Brightness);
  258. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, settings_.Contrast);
  259. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, settings_.Hue);
  260. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, settings_.Saturation);
  261. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SHARPNESS, settings_.Sharpness);
  262. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAMMA, settings_.Gamma);
  263. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, settings_.Gain);
  264. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, settings_.Exposure);
  265. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS, settings_.LEDStatus);
  266. if (settings_.WhiteBalance != -1)
  267. {
  268. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, settings_.WhiteBalance);
  269. }
  270. }
  271. }
  272. /// <summary>
  273. /// Applies all settings from the container to the actual ZED camera.
  274. /// </summary>
  275. /// <param name="zedCamera">Current instance of the ZEDCamera wrapper.</param>
  276. public void ResetCameraSettings(sl.ZEDCamera zedCamera)
  277. {
  278. if (zedCamera != null)
  279. {
  280. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, 4);
  281. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, 4);
  282. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.HUE, 0);
  283. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, 3);
  284. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.SHARPNESS, 3);
  285. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAMMA, 5);
  286. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE, 2600);
  287. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE, 0);
  288. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.GAIN, 0);
  289. zedCamera.SetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS, 1);
  290. }
  291. }
  292. /// <summary>
  293. /// Loads camera settings from a file, and sets them to the container and camera.
  294. /// File is loaded from the root project folder (one above Assets).
  295. /// </summary>
  296. /// <param name="zedCamera"></param>
  297. /// <param name="path"></param>
  298. public void LoadCameraSettings(sl.ZEDCamera zedCamera, string path = "ZED_Settings.conf")
  299. {
  300. string[] lines = null;
  301. try
  302. {
  303. lines = System.IO.File.ReadAllLines(path);
  304. }
  305. catch (System.Exception)
  306. {
  307. }
  308. if (lines == null) return;
  309. foreach (string line in lines)
  310. {
  311. string[] splittedLine = line.Split('=');
  312. if (splittedLine.Length == 2)
  313. {
  314. string key = splittedLine[0];
  315. string field = splittedLine[1];
  316. if (key == "brightness")
  317. {
  318. settings_.Brightness = int.Parse(field);
  319. }
  320. else if (key == "contrast")
  321. {
  322. settings_.Contrast = int.Parse(field);
  323. }
  324. else if (key == "hue")
  325. {
  326. settings_.Hue = int.Parse(field);
  327. }
  328. else if (key == "saturation")
  329. {
  330. settings_.Saturation = int.Parse(field);
  331. }
  332. else if (key == "sharpness")
  333. {
  334. settings_.Sharpness = int.Parse(field);
  335. }
  336. else if (key == "gamma")
  337. {
  338. settings_.Gamma = int.Parse(field);
  339. }
  340. else if (key == "whiteBalance")
  341. {
  342. settings_.WhiteBalance = int.Parse(field);
  343. }
  344. else if (key == "gain")
  345. {
  346. settings_.Gain = int.Parse(field);
  347. }
  348. else if (key == "exposure")
  349. {
  350. settings_.Exposure = int.Parse(field);
  351. }
  352. else if (key == "LED")
  353. {
  354. settings_.LEDStatus = int.Parse(field);
  355. }
  356. }
  357. }
  358. SetSettings(zedCamera);
  359. auto = (settings_.Exposure == -1);
  360. whiteBalanceAuto = (settings_.WhiteBalance == -1);
  361. }
  362. /// <summary>
  363. /// Retrieves current settings from the ZED camera.
  364. /// </summary>
  365. /// <param name="zedCamera"></param>
  366. public void RetrieveSettingsCamera(sl.ZEDCamera zedCamera)
  367. {
  368. if (zedCamera != null)
  369. {
  370. settings_.Brightness = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS);
  371. settings_.Contrast = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST);
  372. settings_.Hue = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.HUE);
  373. settings_.Saturation = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.SATURATION);
  374. settings_.Sharpness = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.SHARPNESS);
  375. settings_.Gamma = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.GAMMA);
  376. settings_.Gain = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.GAIN);
  377. settings_.Exposure = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.EXPOSURE);
  378. settings_.WhiteBalance = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.WHITEBALANCE);
  379. settings_.LEDStatus = zedCamera.GetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS);
  380. }
  381. }
  382. /// <summary>
  383. /// Applies an individual setting to the ZED camera.
  384. /// </summary>
  385. /// <param name="settings">Setting to be changed (brightness, contrast, gain, exposure, etc.)</param>
  386. /// <param name="value">New value for the setting.</param>
  387. /// <param name="usedefault">If true, ignores the value and applies the default setting.</param>
  388. public void SetCameraSettings(int cid, sl.CAMERA_SETTINGS settings, int value, bool usedefault = false)
  389. {
  390. settings_.settings[(int)settings] = !usedefault && value != -1 ? value : -1;
  391. dllz_set_video_settings(cid, (int)settings, value, System.Convert.ToInt32(usedefault));
  392. }
  393. /// <summary>
  394. /// Gets the value from an individual ZED camera setting (brightness, contrast, gain, exposure, etc.)
  395. /// </summary>
  396. /// <param name="settings">Setting to be retrieved.</param>
  397. /// <returns>Current value.</returns>
  398. public int GetCameraSettings(int cid, sl.CAMERA_SETTINGS settings)
  399. {
  400. return dllz_get_video_settings(cid, (int)settings);
  401. //settings_.settings[(int)settings] = dllz_get_camera_settings(cid, (int)settings);
  402. //return settings_.settings[(int)settings];
  403. }
  404. /// <summary>
  405. /// Saves all camera settings into a file into the specified path/name.
  406. /// </summary>
  407. /// <param name="path">Path and filename to save the file (ex. /Assets/ZED_Settings.conf)</param>
  408. public void SaveCameraSettings(string path)
  409. {
  410. using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
  411. {
  412. file.WriteLine("brightness=" + settings_.Brightness.ToString());
  413. file.WriteLine("contrast=" + settings_.Contrast.ToString());
  414. file.WriteLine("hue=" + settings_.Hue.ToString());
  415. file.WriteLine("saturation=" + settings_.Saturation.ToString());
  416. file.WriteLine("sharpness=" + settings_.Sharpness.ToString());
  417. file.WriteLine("gamma=" + settings_.Gamma.ToString());
  418. file.WriteLine("whiteBalance=" + settings_.WhiteBalance.ToString());
  419. file.WriteLine("gain=" + settings_.Gain.ToString());
  420. file.WriteLine("exposure=" + settings_.Exposure.ToString());
  421. file.WriteLine("LED=" + settings_.LEDStatus.ToString());
  422. file.Close();
  423. }
  424. }
  425. }