GenImage.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Runtime.InteropServices;
  6. namespace TeamAAS.Camera.Images
  7. {
  8. [Serializable]
  9. /// <summary>
  10. /// 通用图像类 — 不依赖任何视觉库,仅保存原始像素数据。
  11. /// 可在各视觉库插件(VisionPro/Halcon/OpenCV等)中做类型转换。
  12. /// </summary>
  13. public class GenImage
  14. {
  15. public int Width { get; }
  16. public int Height { get; }
  17. public int Stride { get; }
  18. public PixelType Format { get; }
  19. public byte[] Data { get; }
  20. /// <summary>
  21. /// 元数据字典 — 存储设备参数、标定信息、采集时间等。
  22. /// 例如: "ExposureTime", "LaserPower", "ScanRate", "CalibrationDate" 等。
  23. /// </summary>
  24. public Dictionary<string, object> Metadata { get; }
  25. public int BitsPerPixel
  26. {
  27. get
  28. {
  29. switch (Format)
  30. {
  31. case PixelType.Grey8: return 8;
  32. case PixelType.Grey16: return 16;
  33. case PixelType.RGB24:
  34. case PixelType.BGR24: return 24;
  35. case PixelType.BGRA32: return 32;
  36. case PixelType.Depth16: return 16;
  37. case PixelType.Depth16Conf8:
  38. case PixelType.Depth16Intensity8: return 24;
  39. default: return 8;
  40. }
  41. }
  42. }
  43. public int BytesPerPixel => BitsPerPixel / 8;
  44. public int ActualStride => Stride > 0 ? Stride : Width * BytesPerPixel;
  45. public GenImage(int width, int height, PixelType format, byte[] data, int stride = 0)
  46. : this(width, height, format, data, stride, null)
  47. {
  48. }
  49. public GenImage(int width, int height, PixelType format, byte[] data, int stride, Dictionary<string, object> metadata)
  50. {
  51. if (width <= 0) throw new ArgumentException("width must > 0", nameof(width));
  52. if (height <= 0) throw new ArgumentException("height must > 0", nameof(height));
  53. Width = width;
  54. Height = height;
  55. Format = format;
  56. Stride = stride;
  57. Data = data ?? throw new ArgumentNullException(nameof(data));
  58. Metadata = metadata ?? new Dictionary<string, object>();
  59. }
  60. public GenImage Clone()
  61. {
  62. var copy = new byte[Data.Length];
  63. Buffer.BlockCopy(Data, 0, copy, 0, Data.Length);
  64. return new GenImage(Width, Height, Format, copy, Stride, new Dictionary<string, object>(Metadata));
  65. }
  66. #region → Bitmap (标准像素格式)
  67. /// <summary>
  68. /// 转换为 System.Drawing.Bitmap(调用方负责 Dispose)。
  69. /// 深度图格式将自动使用伪彩映射。
  70. /// </summary>
  71. public Bitmap ToBitmap()
  72. {
  73. if (Format == PixelType.Depth16 || Format == PixelType.Depth16Conf8 || Format == PixelType.Depth16Intensity8)
  74. return ToBitmapPseudoColor();
  75. int srcStride = ActualStride;
  76. int dstBpp = BitsPerPixel == 16 ? 8 : BitsPerPixel;
  77. PixelFormat pf;
  78. switch (Format)
  79. {
  80. case PixelType.Grey8:
  81. case PixelType.Grey16:
  82. pf = PixelFormat.Format8bppIndexed;
  83. break;
  84. case PixelType.RGB24:
  85. case PixelType.BGR24:
  86. pf = PixelFormat.Format24bppRgb;
  87. break;
  88. case PixelType.BGRA32:
  89. pf = PixelFormat.Format32bppArgb;
  90. break;
  91. default:
  92. pf = PixelFormat.Format8bppIndexed;
  93. break;
  94. }
  95. int dstStride = ((Width * dstBpp + 31) / 32) * 4;
  96. var bmp = new Bitmap(Width, Height, pf);
  97. if (pf == PixelFormat.Format8bppIndexed)
  98. {
  99. var pal = bmp.Palette;
  100. for (int i = 0; i < 256; i++) pal.Entries[i] = Color.FromArgb(i, i, i);
  101. bmp.Palette = pal;
  102. }
  103. var rect = new Rectangle(0, 0, Width, Height);
  104. var bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, pf);
  105. try
  106. {
  107. IntPtr dstScan = bmpData.Scan0;
  108. int copyBytes = Math.Min(srcStride, dstStride);
  109. if (Format == PixelType.Grey16)
  110. {
  111. for (int y = 0; y < Height; y++)
  112. {
  113. int srcOff = y * srcStride;
  114. int dstOff = y * dstStride;
  115. for (int x = 0; x < Width; x++)
  116. {
  117. Marshal.WriteByte(dstScan, dstOff + x, Data[srcOff + x * 2 + 1]);
  118. }
  119. }
  120. }
  121. else if (Format == PixelType.RGB24)
  122. {
  123. for (int y = 0; y < Height; y++)
  124. {
  125. int srcOff = y * srcStride;
  126. int dstOff = y * dstStride;
  127. for (int x = 0; x < Width; x++)
  128. {
  129. int si = srcOff + x * 3;
  130. int di = dstOff + x * 3;
  131. Marshal.WriteByte(dstScan, di, Data[si + 2]);
  132. Marshal.WriteByte(dstScan, di + 1, Data[si + 1]);
  133. Marshal.WriteByte(dstScan, di + 2, Data[si]);
  134. }
  135. }
  136. }
  137. else
  138. {
  139. for (int y = 0; y < Height; y++)
  140. {
  141. Marshal.Copy(Data, y * srcStride, dstScan + y * dstStride, copyBytes);
  142. }
  143. }
  144. }
  145. finally
  146. {
  147. bmp.UnlockBits(bmpData);
  148. }
  149. return bmp;
  150. }
  151. #endregion
  152. #region → Bitmap (深度图伪彩)
  153. /// <summary>
  154. /// 深度图伪彩显示 — 将 Depth16/Depth16Conf8/Depth16Intensity8 映射为彩色 Bitmap。
  155. /// 使用 Jet colormap (蓝→青→绿→黄→红) 进行深度可视化。
  156. /// </summary>
  157. public Bitmap ToBitmapPseudoColor(ushort minDepth = 0, ushort maxDepth = 0)
  158. {
  159. if (Format != PixelType.Depth16 && Format != PixelType.Depth16Conf8 && Format != PixelType.Depth16Intensity8)
  160. return ToBitmap();
  161. int srcStride = ActualStride;
  162. int bpp = BytesPerPixel;
  163. if (maxDepth <= minDepth)
  164. maxDepth = FindMaxDepth();
  165. int dstStride = ((Width * 24 + 31) / 32) * 4;
  166. var bmp = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
  167. var rect = new Rectangle(0, 0, Width, Height);
  168. var bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
  169. try
  170. {
  171. IntPtr dstScan = bmpData.Scan0;
  172. float range = (float)(maxDepth - minDepth);
  173. if (range < 1) range = 1;
  174. for (int y = 0; y < Height; y++)
  175. {
  176. int srcOff = y * srcStride;
  177. int dstOff = y * dstStride;
  178. for (int x = 0; x < Width; x++)
  179. {
  180. ushort depth = (ushort)(Data[srcOff + x * bpp] | (Data[srcOff + x * bpp + 1] << 8));
  181. float t = (depth - minDepth) / range;
  182. if (t < 0) t = 0;
  183. if (t > 1) t = 1;
  184. Color jet = JetColormap(t);
  185. Marshal.WriteByte(dstScan, dstOff + x * 3, jet.B);
  186. Marshal.WriteByte(dstScan, dstOff + x * 3 + 1, jet.G);
  187. Marshal.WriteByte(dstScan, dstOff + x * 3 + 2, jet.R);
  188. }
  189. }
  190. }
  191. finally
  192. {
  193. bmp.UnlockBits(bmpData);
  194. }
  195. return bmp;
  196. }
  197. private ushort FindMaxDepth()
  198. {
  199. int bpp = BytesPerPixel;
  200. ushort max = 0;
  201. for (int y = 0; y < Height; y++)
  202. {
  203. int rowOff = y * ActualStride;
  204. for (int x = 0; x < Width; x++)
  205. {
  206. ushort d = (ushort)(Data[rowOff + x * bpp] | (Data[rowOff + x * bpp + 1] << 8));
  207. if (d > max) max = d;
  208. }
  209. }
  210. return max;
  211. }
  212. private static Color JetColormap(float t)
  213. {
  214. float r, g, b;
  215. if (t < 0.125f) { r = 0; g = 0; b = 0.5f + 4f * t; }
  216. else if (t < 0.375f) { r = 0; g = 4f * (t - 0.125f); b = 1f; }
  217. else if (t < 0.625f) { r = 4f * (t - 0.375f); g = 1f; b = 1f - 4f * (t - 0.375f); }
  218. else if (t < 0.875f) { r = 1f; g = 1f - 4f * (t - 0.625f); b = 0; }
  219. else { r = 1f - 4f * (t - 0.875f); g = 0; b = 0; }
  220. return Color.FromArgb(
  221. (int)(Math.Max(0, Math.Min(1, r)) * 255),
  222. (int)(Math.Max(0, Math.Min(1, g)) * 255),
  223. (int)(Math.Max(0, Math.Min(1, b)) * 255));
  224. }
  225. #endregion
  226. #region ← Bitmap
  227. /// <summary>
  228. /// 从 System.Drawing.Bitmap 创建 GenImage。
  229. /// </summary>
  230. public static GenImage FromBitmap(Bitmap bmp)
  231. {
  232. if (bmp == null) throw new ArgumentNullException(nameof(bmp));
  233. PixelType format;
  234. switch (bmp.PixelFormat)
  235. {
  236. case PixelFormat.Format8bppIndexed: format = PixelType.Grey8; break;
  237. case PixelFormat.Format24bppRgb: format = PixelType.BGR24; break;
  238. case PixelFormat.Format32bppArgb:
  239. case PixelFormat.Format32bppRgb: format = PixelType.BGRA32; break;
  240. default:
  241. using (var c = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb))
  242. using (var g = Graphics.FromImage(c))
  243. {
  244. g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
  245. return FromBitmap(c);
  246. }
  247. }
  248. int bpp = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
  249. int stride = bmp.Width * bpp;
  250. var data = new byte[stride * bmp.Height];
  251. var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  252. var bd = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
  253. try
  254. {
  255. int srcStride = bd.Stride;
  256. if (srcStride == stride)
  257. Marshal.Copy(bd.Scan0, data, 0, data.Length);
  258. else
  259. for (int y = 0; y < bmp.Height; y++)
  260. Marshal.Copy(bd.Scan0 + y * srcStride, data, y * stride, stride);
  261. }
  262. finally
  263. {
  264. bmp.UnlockBits(bd);
  265. }
  266. return new GenImage(bmp.Width, bmp.Height, format, data, stride);
  267. }
  268. #endregion
  269. #region 工厂方法
  270. public static GenImage CreateGrey8(int width, int height)
  271. => new GenImage(width, height, PixelType.Grey8, new byte[width * height]);
  272. public static GenImage CreateDepth16(int width, int height)
  273. => new GenImage(width, height, PixelType.Depth16, new byte[width * height * 2]);
  274. public static GenImage FromIntPtr(int width, int height, PixelType format, IntPtr buffer, int bufferStride = 0)
  275. {
  276. int bpp;
  277. switch (format)
  278. {
  279. case PixelType.Grey8: bpp = 1; break;
  280. case PixelType.Grey16:
  281. case PixelType.Depth16: bpp = 2; break;
  282. case PixelType.RGB24:
  283. case PixelType.BGR24:
  284. case PixelType.Depth16Conf8:
  285. case PixelType.Depth16Intensity8: bpp = 3; break;
  286. case PixelType.BGRA32: bpp = 4; break;
  287. default: bpp = 1; break;
  288. }
  289. int stride = bufferStride > 0 ? bufferStride : width * bpp;
  290. var data = new byte[stride * height];
  291. Marshal.Copy(buffer, data, 0, data.Length);
  292. return new GenImage(width, height, format, data, stride);
  293. }
  294. #endregion
  295. #region 元数据便捷访问
  296. public void SetMetadata(string key, object value) => Metadata[key] = value;
  297. public T GetMetadata<T>(string key, T defaultValue = default)
  298. {
  299. if (Metadata.TryGetValue(key, out var val) && val is T typed)
  300. return typed;
  301. return defaultValue;
  302. }
  303. #endregion
  304. #region 图像处理
  305. /// <summary>
  306. /// 对图像应用 Gamma 校正(直接修改像素数据)。
  307. /// 仅支持 Grey8、Grey16、RGB24、BGR24、BGRA32 格式。
  308. /// gamma=1.0 时不做任何操作;gamma>1.0 提亮暗部;gamma<1.0 压暗暗部。
  309. /// </summary>
  310. public void ApplyGamma(float gamma)
  311. {
  312. if (Math.Abs(gamma - 1.0f) < 0.001f) return;
  313. switch (Format)
  314. {
  315. case PixelType.Grey8:
  316. ApplyGammaGrey8(gamma);
  317. break;
  318. case PixelType.Grey16:
  319. ApplyGammaGrey16(gamma);
  320. break;
  321. case PixelType.RGB24:
  322. case PixelType.BGR24:
  323. case PixelType.BGRA32:
  324. ApplyGammaColor(gamma);
  325. break;
  326. }
  327. }
  328. private void ApplyGammaGrey8(float gamma)
  329. {
  330. int stride = ActualStride;
  331. for (int y = 0; y < Height; y++)
  332. {
  333. int rowOff = y * stride;
  334. for (int x = 0; x < Width; x++)
  335. {
  336. float norm = Data[rowOff + x] / 255f;
  337. float corrected = (float)Math.Pow(norm, gamma);
  338. Data[rowOff + x] = (byte)Math.Max(0, Math.Min(255, corrected * 255f));
  339. }
  340. }
  341. }
  342. private void ApplyGammaGrey16(float gamma)
  343. {
  344. int stride = ActualStride;
  345. for (int y = 0; y < Height; y++)
  346. {
  347. int rowOff = y * stride;
  348. for (int x = 0; x < Width; x++)
  349. {
  350. int pxOff = rowOff + x * 2;
  351. ushort val = (ushort)(Data[pxOff] | (Data[pxOff + 1] << 8));
  352. float norm = val / 65535f;
  353. float corrected = (float)Math.Pow(norm, gamma);
  354. ushort outVal = (ushort)Math.Max(0, Math.Min(65535, corrected * 65535f));
  355. Data[pxOff] = (byte)(outVal & 0xFF);
  356. Data[pxOff + 1] = (byte)(outVal >> 8);
  357. }
  358. }
  359. }
  360. private void ApplyGammaColor(float gamma)
  361. {
  362. int stride = ActualStride;
  363. int channels = BytesPerPixel;
  364. int colorChannels = Format == PixelType.BGRA32 ? 3 : channels;
  365. for (int y = 0; y < Height; y++)
  366. {
  367. int rowOff = y * stride;
  368. for (int x = 0; x < Width; x++)
  369. {
  370. int pxOff = rowOff + x * channels;
  371. for (int c = 0; c < colorChannels; c++)
  372. {
  373. float norm = Data[pxOff + c] / 255f;
  374. float corrected = (float)Math.Pow(norm, gamma);
  375. Data[pxOff + c] = (byte)Math.Max(0, Math.Min(255, corrected * 255f));
  376. }
  377. }
  378. }
  379. }
  380. #endregion
  381. }
  382. /// <summary>
  383. /// 通用图像像素格式
  384. /// </summary>
  385. public enum PixelType
  386. {
  387. /// <summary>8-bit 灰度</summary>
  388. Grey8,
  389. /// <summary>16-bit 灰度</summary>
  390. Grey16,
  391. /// <summary>24-bit RGB 交错</summary>
  392. RGB24,
  393. /// <summary>24-bit BGR 交错</summary>
  394. BGR24,
  395. /// <summary>32-bit BGRA 交错</summary>
  396. BGRA32,
  397. /// <summary>16-bit 深度图 (单通道)</summary>
  398. Depth16,
  399. /// <summary>16-bit 深度 + 8-bit 置信度 (3 字节/像素)</summary>
  400. Depth16Conf8,
  401. /// <summary>16-bit 深度 + 8-bit 强度 (3 字节/像素)</summary>
  402. Depth16Intensity8
  403. }
  404. }