Blame view

OS.Spin/OS.Spin.Common/Helper.cs 6.8 KB
8ca6e89d   Tuo Wenbo   20211021
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  using OpenCvSharp;
  using System;
  using System.Diagnostics;
  using System.Drawing;
  using System.Runtime.InteropServices;
  
  namespace OS.Spin.Common
  {
      public class Helper
      {
          ///// <summary>
          ///// 检查图片是否存在
          ///// </summary>
          ///// <param name="filePath"></param>
          //public static void CheckExist(object filePath)
          //{
          //    if (!File.Exists(filePath.ToString()))
          //    {
          //        Cache.GetInstance().SaveCount++;
          //    }
          //}
  
          public static double GetProcessUsedMemory()
          {
              double usedMemory = 0;
              usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
              return usedMemory;
          }
          #region 清理非托管资源
          [System.Runtime.InteropServices.DllImport("Kernel32")]
          private extern static Boolean CloseHandle(IntPtr handle);
          #endregion
  
          #region 
          [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
          public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
          /// <summary>
          /// 释放内存
          /// </summary>
          public static void ClearMemory()
          {
              GC.Collect();
              GC.WaitForPendingFinalizers();
              if (Environment.OSVersion.Platform == PlatformID.Win32NT)
              {
                  SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
              }
          }
          #endregion
  
          /// <summary>
          /// 通过传入的图片计算增益值
          /// </summary>
          /// <param name="imagePath"></param>
          /// <returns></returns>
          public static int GetGainValue(string imagePath)
          {
              try
              {
                  Rect re = new Rect(new OpenCvSharp.Point(1100, 50), new OpenCvSharp.Size(200, 200));
                  Mat imageRoi = null;
                  Mat _cameraMat = null;
                  Mat _mat = new Mat(imagePath);
                  if (_mat.Channels() == 3)
                  {
                      _cameraMat = new Mat(_mat, re);
                      imageRoi = new Mat(200, 200, MatType.CV_8UC3);
                      Cv2.CvtColor(_cameraMat, imageRoi, ColorConversionCodes.BGR2GRAY);
                  }
                  else
                  {
                      imageRoi = new Mat(_mat, re);
                  }
                  int gainResult = 0;
                  double avgGain = Cv2.Mean(imageRoi).Val0;
                  int th_max = 230, th_min = 130, th_mid = 180;
                  if (avgGain > th_mid)
                  {
                      if (avgGain > th_max)
                          avgGain = th_max;
                      gainResult = 20 - (int)((avgGain - th_mid) / 5);
                  }
                  else
                  {
                      if (avgGain < th_min)
                          avgGain = th_min;
                      gainResult = 20 + (int)((th_mid - avgGain) / 5);
                  }
                  //imageRoi.Release();
                  //_cameraMat.Release();
                  //_mat.Release();
                  return gainResult;
              }
              catch (Exception ex)
              {
                  LogisTrac.WriteLog(string.Format("GetGainValue(string imagePath):{0}", ex.Message));
                  return 0;
              }
          }
  
          /// <summary>
          /// 通过传入的图片计算增益值
          /// </summary>
          /// <param name="_mat"></param>
          /// <returns></returns>
          public static int GetGainValue(Mat _mat)
          {
              try
              {
                  Rect re = new Rect(new OpenCvSharp.Point(1100, 50), new OpenCvSharp.Size(200, 200));
                  Mat imageRoi = null;
                  Mat _cameraMat = null;
                  if (_mat.Channels() == 3)
                  {
                      _cameraMat = new Mat(_mat, re);
                      imageRoi = new Mat(200, 200, MatType.CV_8UC3);
                      Cv2.CvtColor(_cameraMat, imageRoi, ColorConversionCodes.BGR2GRAY);
                  }
                  else
                  {
                      imageRoi = new Mat(_mat, re);
                  }
                  int gainResult = 0;
                  double avgGain = Cv2.Mean(imageRoi).Val0;
                  int th_max = 230, th_min = 130, th_mid = 180;
                  if (avgGain > th_mid)
                  {
                      if (avgGain > th_max)
                          avgGain = th_max;
                      gainResult = 20 - (int)((avgGain - th_mid) / 5);
                  }
                  else
                  {
                      if (avgGain < th_min)
                          avgGain = th_min;
                      gainResult = 20 + (int)((th_mid - avgGain) / 5);
                  }
                  //imageRoi.Release();
                  //_cameraMat.Release();
                  return gainResult;
              }
              catch (Exception ex)
              {
                  LogisTrac.WriteLog(string.Format("GetGainValue(Mat _mat):{0}", ex.Message));
                  return 0;
              }
          }
  
          /// <summary>
          /// 普通图片获取增益值(非Mat
          /// </summary>
          /// <param name="filePath"></param>
          /// <returns></returns>
          public static int GetGain(string filePath)
          {
              try
              {
                  double[] a = new double[3] { 0, 0, 0 };
                  Bitmap curBitmap = (Bitmap)System.Drawing.Image.FromFile(filePath);
                  int wide = curBitmap.Width;
                  int height = curBitmap.Height;
                  Color srcColor;
                  double RR = 0;
                  double GG = 0;
                  double BB = 0;
  
                  for (int y3 = 50; y3 < (50 + 200); y3++)
                  {
                      for (int x3 = 1100; x3 < (1100 + 200); x3++)
                      {
                          srcColor = curBitmap.GetPixel(x3, y3);
                          RR = RR + srcColor.R;
                          GG = GG + srcColor.G;
                          BB = BB + srcColor.B;
                      }
                  }
                  a[0] = Math.Round(RR / (1100 * 50), 2);
                  double mean_gain = a[0];
                  int th_max = 230, th_min = 130, th_mid = 180;
                  if (mean_gain > th_mid)
                  {
                      if (mean_gain > th_max)
                          mean_gain = th_max;
                      return 20 - (int)((mean_gain - th_mid) / 5);
                  }
                  else
                  {
                      if (mean_gain < th_min)
                          mean_gain = th_min;
                      return 20 + (int)((th_mid - mean_gain) / 5);
                  }
              }
              catch (Exception ex)
              {
                  LogisTrac.WriteLog(string.Format("GetGain(string filePath):{0}", ex.Message));
                  return 0;
              }
          }
      }
  }