BaseFunction.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "stdafx.h"
  2. #include "BaseFunction.h"
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include <opencv2/opencv.hpp>
  6. #include "OTSParticle.h"
  7. #include "OTSImageProcessParam.h"
  8. #include <OTSFieldData.h>
  9. using namespace cv;
  10. using namespace std;
  11. using namespace OTSDATA;
  12. /***** 求两点间距离*****/
  13. float getDistance(Point pointO, Point pointA)
  14. {
  15. float distance;
  16. distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2);
  17. distance = sqrtf(distance);
  18. return distance;
  19. }
  20. /***** 点到直线的距离:P到AB的距离*****/
  21. //P为线外一点,AB为线段两个端点
  22. float getDist_P2L(Point pointP, Point pointA, Point pointB)
  23. {
  24. //求直线方程
  25. int A = 0, B = 0, C = 0;
  26. A = pointA.y - pointB.y;
  27. B = pointB.x - pointA.x;
  28. C = pointA.x * pointB.y - pointA.y * pointB.x;
  29. //代入点到直线距离公式
  30. float distance = 0;
  31. distance = ((float)abs(A * pointP.x + B * pointP.y + C)) / ((float)sqrtf(A * A + B * B));
  32. return distance;
  33. }
  34. int Side(Point P1, Point P2, Point point)
  35. {
  36. /*Point P1 = line.P1;
  37. Point P2 = line.P2;*/
  38. return ((P2.y - P1.y) * point.x + (P1.x - P2.x) * point.y + (P2.x * P1.y - P1.x * P2.y));
  39. }
  40. void FindInnerCircleInContour(vector<Point> contour, Point& center, int& radius)
  41. {
  42. Rect r = boundingRect(contour);
  43. int nL = r.x, nR = r.br().x; //轮廓左右边界
  44. int nT = r.y, nB = r.br().y; //轮廓上下边界
  45. double dist = 0;
  46. double maxdist = 0;
  47. for (int i = nL; i < nR; i++) //列
  48. {
  49. for (int j = nT; j < nB; j++) //行
  50. {
  51. //计算轮廓内部各点到最近轮廓点的距离
  52. dist = pointPolygonTest(contour, Point(i, j), true);
  53. if (dist > maxdist)
  54. {
  55. //求最大距离,只有轮廓最中心的点才距离最大
  56. maxdist = dist;
  57. center = Point(i, j);
  58. }
  59. }
  60. }
  61. radius = maxdist; //圆半径
  62. }
  63. BOOL GetParticleAverageChord(std::vector<Point> listEdge, double a_PixelSize, double& dPartFTD)
  64. {
  65. // safety check
  66. double nx = 0, ny = 0;
  67. Moments mu;
  68. mu = moments(listEdge, false);
  69. nx = mu.m10 / mu.m00;
  70. ny = mu.m01 / mu.m00;
  71. //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
  72. Point ptCenter = Point((int)nx, (int)ny);
  73. // coordinate transformation
  74. Point ptPosition;
  75. int radiusNum = 0;
  76. // get ferret diameter
  77. double sumFltDiameter = 0;
  78. int interval;
  79. int edgePointNum = listEdge.size();
  80. if (edgePointNum > 10)
  81. {
  82. interval = edgePointNum / 10;//get one line per 10 degree aproxemately
  83. }
  84. else
  85. {
  86. interval = 1;
  87. }
  88. for (int i = 0; i < edgePointNum; i++)
  89. {
  90. Point pt = listEdge[i];
  91. ptPosition.x = abs(pt.x - ptCenter.x);
  92. ptPosition.y = abs(pt.y - ptCenter.y);
  93. if (i % interval == 0)//calculate one line per 10 point ,so to speed up.don't calculate all the diameter.
  94. {
  95. double r1 = sqrt(pow(ptPosition.x, 2) + pow(ptPosition.y, 2));
  96. sumFltDiameter += r1;
  97. radiusNum += 1;
  98. //line(cvImageData, ptCenter, pt, Scalar(nBlackColor), nThickness, nLineType);
  99. }
  100. }
  101. if (radiusNum == 0)
  102. {
  103. dPartFTD = 0;
  104. }
  105. else
  106. {
  107. dPartFTD = a_PixelSize * sumFltDiameter / radiusNum * 2;
  108. }
  109. //imshow("feret center", cvImageData);
  110. return TRUE;
  111. }
  112. void linearSmooth5(WORD wordIn[], WORD wordOut[], int N = 255)//smooth algorithm
  113. {
  114. double in[256];
  115. double out[256];
  116. double smoothCurveData[256];
  117. for (int i = 0; i < 256; i++)
  118. {
  119. in[i] = (double)wordIn[i];
  120. }
  121. int i;
  122. if (N < 5)
  123. {
  124. for (i = 0; i <= N - 1; i++)
  125. {
  126. out[i] = in[i];
  127. }
  128. }
  129. else
  130. {
  131. out[0] = (3.0 * in[0] + 2.0 * in[1] + in[2] - in[4]) / 5.0;
  132. out[1] = (4.0 * in[0] + 3.0 * in[1] + 2 * in[2] + in[3]) / 10.0;
  133. for (i = 2; i <= N - 3; i++)
  134. {
  135. out[i] = (in[i - 2] + in[i - 1] + in[i] + in[i + 1] + in[i + 2]) / 5.0;
  136. }
  137. out[N - 2] = (4.0 * in[N - 1] + 3.0 * in[N - 2] + 2 * in[N - 3] + in[N - 4]) / 10.0;
  138. out[N - 1] = (3.0 * in[N - 1] + 2.0 * in[N - 2] + in[N - 3] - in[N - 5]) / 5.0;
  139. }
  140. for (int i = 0; i < N; i++)
  141. {
  142. wordOut[i] = (WORD)out[i];
  143. }
  144. }
  145. void BlurImage(CBSEImgPtr inImg)
  146. {
  147. int rows, cols;
  148. cols = inImg->GetWidth();
  149. rows = inImg->GetHeight();
  150. BYTE* pPixel = inImg->GetImageDataPointer();
  151. Mat cvcopyImg = Mat(rows, cols, CV_8UC1, pPixel);
  152. //Mat blurImg;
  153. //medianBlur(cvcopyImg, cvcopyImg, 11);//get rid of the noise point.
  154. //cv::bilateralFilter
  155. cv::GaussianBlur(cvcopyImg, cvcopyImg, Size(5, 5), 2);
  156. //inImg->SetImageData(cvcopyImg.data, width, height);
  157. /*outImg = inImg;*/
  158. }
  159. Mat GetMatDataFromBseImg(CBSEImgPtr inImg)
  160. {
  161. int rows, cols;
  162. cols = inImg->GetWidth();
  163. rows = inImg->GetHeight();
  164. BYTE* pPixel = inImg->GetImageDataPointer();
  165. Mat cvcopyImg = Mat(rows, cols, CV_8UC1, pPixel);
  166. return cvcopyImg;
  167. }
  168. CBSEImgPtr GetBSEImgFromMat(Mat inImg)
  169. {
  170. CBSEImgPtr bse = CBSEImgPtr(new CBSEImg(CRect(0, 0, inImg.cols, inImg.rows)));
  171. BYTE* pPixel = inImg.data;
  172. bse->SetImageData(pPixel, inImg.cols, inImg.rows);
  173. return bse;
  174. }
  175. /***********************************************************
  176. 增强算法的原理在于先统计每个灰度值在整个图像中所占的比例
  177. 然后以小于当前灰度值的所有灰度值在总像素中所占的比例,作为增益系数
  178. 对每一个像素点进行调整。由于每一个值的增益系数都是小于它的所有值所占
  179. 的比例和。所以就使得经过增强之后的图像亮的更亮,暗的更暗。
  180. ************************************************************/
  181. void ImageStretchByHistogram(const Mat& src, Mat& dst)
  182. {
  183. //判断传入参数是否正常
  184. if (!(src.size().width == dst.size().width))
  185. {
  186. cout << "error" << endl;
  187. return;
  188. }
  189. double p[256], p1[256], num[256];
  190. memset(p, 0, sizeof(p));
  191. memset(p1, 0, sizeof(p1));
  192. memset(num, 0, sizeof(num));
  193. int height = src.size().height;
  194. int width = src.size().width;
  195. long wMulh = height * width;
  196. //统计每一个灰度值在整个图像中所占个数
  197. for (int x = 0; x < width; x++)
  198. {
  199. for (int y = 0; y < height; y++)
  200. {
  201. uchar v = src.at<uchar>(y, x);
  202. num[v]++;
  203. }
  204. }
  205. //使用上一步的统计结果计算每一个灰度值所占总像素的比例
  206. for (int i = 0; i < 256; i++)
  207. {
  208. p[i] = num[i] / wMulh;
  209. }
  210. //计算每一个灰度值,小于当前灰度值的所有灰度值在总像素中所占的比例
  211. //p1[i]=sum(p[j]); j<=i;
  212. for (int i = 0; i < 256; i++)
  213. {
  214. for (int k = 0; k <= i; k++)
  215. p1[i] += p[k];
  216. }
  217. //以小于当前灰度值的所有灰度值在总像素中所占的比例,作为增益系数对每一个像素点进行调整。
  218. for (int y = 0; y < height; y++)
  219. {
  220. for (int x = 0; x < width; x++) {
  221. uchar v = src.at<uchar>(y, x);
  222. dst.at<uchar>(y, x) = p1[v] * 255 + 0.5;
  223. }
  224. }
  225. return;
  226. }
  227. //调整图像对比度
  228. Mat AdjustContrastY(const Mat& img)
  229. {
  230. Mat out = Mat::zeros(img.size(), CV_8UC1);
  231. Mat workImg = img.clone();
  232. //对图像进行对比度增强
  233. ImageStretchByHistogram(workImg, out);
  234. return Mat(out);
  235. }