当前位置:   article > 正文

C# Onnx segment-anything 分割万物 一键抠图

C# Onnx segment-anything 分割万物 一键抠图

目录

介绍

效果

模型信息

sam_vit_b_decoder.onnx

sam_vit_b_encoder.onnx

项目

代码

下载


C# Onnx segment-anything 分割万物 一键抠图

介绍

github地址:GitHub - facebookresearch/segment-anything: The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model. 

效果

C# Onnx segment-anything 分割万物

模型信息

sam_vit_b_decoder.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:image_embeddings
tensor:Float[1, 256, 64, 64]
name:point_coords
tensor:Float[1, -1, 2]
name:point_labels
tensor:Float[1, -1]
name:mask_input
tensor:Float[1, 1, 256, 256]
name:has_mask_input
tensor:Float[1]
name:orig_im_size
tensor:Float[2]
---------------------------------------------------------------

Outputs
-------------------------
name:masks
tensor:Float[-1, -1, -1, -1]
name:iou_predictions
tensor:Float[-1, 4]
name:low_res_masks
tensor:Float[-1, -1, -1, -1]
---------------------------------------------------------------

sam_vit_b_encoder.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:image
tensor:Float[1, 3, 1024, 1024]
---------------------------------------------------------------

Outputs
-------------------------
name:image_embeddings
tensor:Float[1, 256, 64, 64]
---------------------------------------------------------------

项目

代码

  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Reflection;
  8. using System.Windows.Forms;
  9. namespace Onnx_Demo
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  18. string image_path = "";
  19. DateTime dt1 = DateTime.Now;
  20. DateTime dt2 = DateTime.Now;
  21. Mat image;
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. OpenFileDialog ofd = new OpenFileDialog();
  25. ofd.Filter = fileFilter;
  26. if (ofd.ShowDialog() != DialogResult.OK) return;
  27. pictureBox1.Image = null;
  28. image_path = ofd.FileName;
  29. pictureBox1.Image = new Bitmap(image_path);
  30. textBox1.Text = "";
  31. image = new Mat(image_path);
  32. pictureBox2.Image = null;
  33. pictureBox3.Image = null;
  34. sam.SetImage(image_path);
  35. image = new Mat(image_path);
  36. pictureBox1.Image = new Bitmap(image_path);
  37. }
  38. private void button2_Click(object sender, EventArgs e)
  39. {
  40. pictureBox3.Image = null;
  41. button2.Enabled = false;
  42. Application.DoEvents();
  43. List<MatInfo> masks_list = sam.GetPointMask(roi);
  44. if (masks_list.Count>0)
  45. {
  46. int MaxInde = 0;
  47. float maxiou_pred = masks_list[0].iou_pred;
  48. for (int i = 0; i < masks_list.Count; i++)
  49. {
  50. float temp = masks_list[i].iou_pred;
  51. if (temp > maxiou_pred)
  52. {
  53. MaxInde = i;
  54. }
  55. }
  56. pictureBox3.Image = BitmapConverter.ToBitmap(masks_list[MaxInde].mask);
  57. }
  58. button2.Enabled = true;
  59. }
  60. SamInferenceSession sam;
  61. private void Form1_Load(object sender, EventArgs e)
  62. {
  63. string encoderPath = "model/sam_vit_b_encoder.onnx";
  64. string decoderPath = "model/sam_vit_b_decoder.onnx";
  65. sam = new SamInferenceSession(encoderPath, decoderPath);
  66. sam.Initialize();
  67. image_path = "test_img/test.jpg";
  68. sam.SetImage(image_path);
  69. image = new Mat(image_path);
  70. pictureBox1.Image = new Bitmap(image_path);
  71. }
  72. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  73. {
  74. Common.ShowNormalImg(pictureBox1.Image);
  75. }
  76. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  77. {
  78. Common.ShowNormalImg(pictureBox2.Image);
  79. }
  80. SaveFileDialog sdf = new SaveFileDialog();
  81. private void button3_Click(object sender, EventArgs e)
  82. {
  83. if (pictureBox2.Image == null)
  84. {
  85. return;
  86. }
  87. Bitmap output = new Bitmap(pictureBox2.Image);
  88. sdf.Title = "保存";
  89. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
  90. if (sdf.ShowDialog() == DialogResult.OK)
  91. {
  92. switch (sdf.FilterIndex)
  93. {
  94. case 1:
  95. {
  96. output.Save(sdf.FileName, ImageFormat.Jpeg);
  97. break;
  98. }
  99. case 2:
  100. {
  101. output.Save(sdf.FileName, ImageFormat.Png);
  102. break;
  103. }
  104. case 3:
  105. {
  106. output.Save(sdf.FileName, ImageFormat.Bmp);
  107. break;
  108. }
  109. case 4:
  110. {
  111. output.Save(sdf.FileName, ImageFormat.Emf);
  112. break;
  113. }
  114. case 5:
  115. {
  116. output.Save(sdf.FileName, ImageFormat.Exif);
  117. break;
  118. }
  119. case 6:
  120. {
  121. output.Save(sdf.FileName, ImageFormat.Gif);
  122. break;
  123. }
  124. case 7:
  125. {
  126. output.Save(sdf.FileName, ImageFormat.Icon);
  127. break;
  128. }
  129. case 8:
  130. {
  131. output.Save(sdf.FileName, ImageFormat.Tiff);
  132. break;
  133. }
  134. case 9:
  135. {
  136. output.Save(sdf.FileName, ImageFormat.Wmf);
  137. break;
  138. }
  139. }
  140. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  141. }
  142. }
  143. bool m_mouseDown = false;
  144. bool m_mouseMove = false;
  145. System.Drawing.Point startPoint = new System.Drawing.Point();
  146. System.Drawing.Point endPoint = new System.Drawing.Point();
  147. Mat currentFrame = new Mat();
  148. Rect roi = new Rect();
  149. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  150. {
  151. if (pictureBox1.Image == null)
  152. return;
  153. if (!m_mouseDown) return;
  154. m_mouseMove = true;
  155. endPoint = e.Location;
  156. pictureBox1.Invalidate();
  157. }
  158. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  159. {
  160. if (!m_mouseDown || !m_mouseMove)
  161. return;
  162. Graphics g = e.Graphics;
  163. Pen p = new Pen(Color.Blue, 2);
  164. Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));
  165. g.DrawRectangle(p, rect);
  166. }
  167. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  168. {
  169. if (!m_mouseDown || !m_mouseMove)
  170. return;
  171. m_mouseDown = false;
  172. m_mouseMove = false;
  173. System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);
  174. System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);
  175. if (image_startPoint.X < 0)
  176. image_startPoint.X = 0;
  177. if (image_startPoint.Y < 0)
  178. image_startPoint.Y = 0;
  179. if (image_endPoint.X < 0)
  180. image_endPoint.X = 0;
  181. if (image_endPoint.Y < 0)
  182. image_endPoint.Y = 0;
  183. if (image_startPoint.X > image.Cols)
  184. image_startPoint.X = image.Cols;
  185. if (image_startPoint.Y > image.Rows)
  186. image_startPoint.Y = image.Rows;
  187. if (image_endPoint.X > image.Cols)
  188. image_endPoint.X = image.Cols;
  189. if (image_endPoint.Y > image.Rows)
  190. image_endPoint.Y = image.Rows;
  191. label1.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);
  192. int w = (image_endPoint.X - image_startPoint.X);
  193. int h = (image_endPoint.Y - image_startPoint.Y);
  194. if (w > 10 && h > 10)
  195. {
  196. roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);
  197. //Console.WriteLine(String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y));
  198. //test
  199. //OpenCvSharp.Point pointinfo = new OpenCvSharp.Point(910, 641);
  200. //roi = new Rect(pointinfo.X - 160, pointinfo.Y - 430, 380, 940);
  201. Mat roi_mat = image[roi];
  202. pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);
  203. }
  204. //pictureBox1.Invalidate();
  205. }
  206. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  207. {
  208. if (pictureBox1.Image == null)
  209. return;
  210. m_mouseDown = true;
  211. startPoint = e.Location;
  212. }
  213. private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)
  214. {
  215. System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
  216. Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);
  217. int zoomedWidth = pictureBox.Width;
  218. int zoomedHeight = pictureBox.Height;
  219. int imageWidth = pictureBox1.Image.Width;
  220. int imageHeight = pictureBox1.Image.Height;
  221. double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);
  222. double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);
  223. int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;
  224. int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;
  225. int zoomedX = point.X - black_left_width;
  226. int zoomedY = point.Y - black_top_height;
  227. System.Drawing.Point outPoint = new System.Drawing.Point();
  228. outPoint.X = (int)((double)zoomedX / zoomRatex);
  229. outPoint.Y = (int)((double)zoomedY / zoomRatey);
  230. return outPoint;
  231. }
  232. }
  233. }

下载

源码下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/191309
推荐阅读
相关标签
  

闽ICP备14008679号