123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using OpenCvSharp;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SmartCoalApplication.Base.CommTool
- {
- public class HistTools
- {
- /// <summary>
- /// 灰度图
- /// </summary>
- /// <param name="mat"></param>
- public static float[] CalcHist(Mat gray)
- {
- float[] hist_int = new float[255];
- Mat hist = new Mat();
- //设置计算直方图的维度
- int dims = 1;
- int[] histSize = { 255 };
- Rangef[] pranges = new Rangef[1];
- pranges[0] = new Rangef(0.0F, 256.0F);
- //计算直方图
- Cv2.CalcHist(new Mat[1] { gray }, new int[] { 0 }, new Mat(), hist, dims, histSize, pranges);
- //创建数据,便于计算
- for (int w = 0; w < hist.Height; w++)
- {
- hist_int[w] = hist.At<float>(0, w);
- }
- return hist_int;
- }
- /// <summary>
- /// 直方图平滑
- /// </summary>
- /// <param name="arr"></param>
- /// <param name="step"></param>
- /// <returns></returns>
- public static int[] Smooth(float[] arr, int step, int num=256)
- {
- int start = step - step / 2;
- int end = num - step / 2;
- double temp = 0;
- int[] res = new int[num];
- for (int i = start; i < end; i++)
- {
- temp = 0;
- for (int j = 0 - step / 2; j < step / 2; j++)
- {
- temp += arr[i + j];
- }
- temp /= step;
- res[i] = (int)temp;
- }
- return res;
- }
- /// <summary>
- /// 寻找峰
- /// </summary>
- /// <param name="hists_new"></param>
- /// <param name="leftValue"></param>
- /// <param name="rightValue"></param>
- /// <param name="mountainsList"></param>
- /// <param name="mountainsArrList"></param>
- /// <param name="mountainsArrListBackUp"></param>
- /// <param name="str"></param>
- /// <param name="smoothStep"></param>
- public static void FindTopAndBetweenWithOutWT123(int[] hists_new, int leftValue, int rightValue, List<int> mountainsList, List<int[]> mountainsArrList, List<int[]> mountainsArrListBackUp, List<string> str, int smoothStep)
- {
- //获得去除左右值的最高波峰
- int topValue = leftValue;
- for (int y = leftValue; y < rightValue; y++)
- {
- if (hists_new[y] > hists_new[topValue])
- {
- topValue = y;
- }
- }
- if (topValue == 0 || topValue == leftValue || topValue == rightValue)
- return;
- mountainsList.Add(topValue);
- //获得波峰左右的波谷
- int leftTrough = topValue;
- int leftTroughBackUp = 0;
- //从波峰点向左侧找波谷点
- for (int y = topValue - 1; y > leftValue; y--)
- {
- if (hists_new[y] >= 0 && hists_new[y] <= hists_new[leftTrough])
- {
- leftTrough = y;
- }
- else
- {
- break;
- }
- }
- //微调
- //if (smoothStep >= 30)
- {
- for (int y = leftTrough + 1; y < topValue; y++)
- {
- if (hists_new[y] == hists_new[leftTrough])// || hists_new[y] < hists_new[topValue] / 10
- {
- leftTroughBackUp = y;
- }
- }
- }
- if (leftTroughBackUp == 0) leftTroughBackUp = leftTrough;
- int rightTrough = topValue;
- int rightTroughBackUp = 0;
- //从波峰点向右侧找波谷点
- for (int y = topValue + 1; y < rightValue; y++)
- {
- if (hists_new[y] >= 0 && hists_new[y] <= hists_new[rightTrough])
- {
- rightTrough = y;
- }
- else
- {
- break;
- }
- }
- //微调
- //if (smoothStep >= 30)
- {
- for (int y = rightTrough - 1; y > topValue; y--)
- {
- if (hists_new[y] == hists_new[rightTrough])// || hists_new[y] < hists_new[topValue] / 10
- {
- rightTroughBackUp = y;
- }
- }
- }
- if (rightTroughBackUp == 0) rightTroughBackUp = rightTrough;
- int[] arr = new int[2];
- arr[0] = leftTroughBackUp;
- arr[1] = rightTroughBackUp;
- mountainsArrList.Add(arr);
- int[] arr1 = new int[2];
- arr1[0] = leftTroughBackUp > 0 ? leftTroughBackUp : leftTrough;
- arr1[1] = rightTroughBackUp > 0 ? rightTroughBackUp : rightTrough;
- mountainsArrListBackUp.Add(arr1);
- if (leftValue < leftTrough - 1)
- {
- if (!str.Contains(leftValue + "|" + leftTrough))
- {
- str.Add(leftValue + "|" + leftTrough);
- FindTopAndBetweenWithOutWT123(hists_new, leftValue, leftTrough, mountainsList, mountainsArrList, mountainsArrListBackUp, str, smoothStep);
- }
- }
- if (rightValue > rightTrough + 1)
- {
- if (!str.Contains(rightValue + "|" + rightTrough))
- {
- str.Add(rightValue + "|" + rightTrough);
- FindTopAndBetweenWithOutWT123(hists_new, rightTrough, rightValue, mountainsList, mountainsArrList, mountainsArrListBackUp, str, smoothStep);
- }
- }
- }
- }
- }
|