DataProcess.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "DataProcess.h"
  2. void average(uint8_t* pSrc, int width, int height, int widthstep, double& ave)
  3. {
  4. double value = 0;
  5. int i, j, k = 0, tmp = 0;
  6. uint8_t* pTmp;
  7. pTmp = pSrc;
  8. for (i = 0; i < width; i++)
  9. {
  10. for (j = 0; j < height; j++)
  11. {
  12. tmp = (int)(pTmp[j * widthstep + i]);
  13. value += tmp;
  14. }
  15. }
  16. value = value / (width * height);
  17. ave = value;
  18. }
  19. void covariance(uint8_t* pSrc, int width, int height, int widthstep, double ave, double& cov)
  20. {
  21. float value = 0;
  22. int i, j, tmp = 0;
  23. uint8_t* pTmp;
  24. pTmp = pSrc;
  25. for (i = 0; i < width; i++)
  26. {
  27. for (j = 0; j < height; j++)
  28. {
  29. tmp = (int)(pTmp[j * widthstep + i]);;
  30. value += (tmp - ave) * (tmp - ave);
  31. }
  32. }
  33. value = value / (width * height);
  34. cov = value;
  35. }
  36. void calcorrcoef(uint8_t* pSrc1, uint8_t* pSrc2, int width, int height, int widthstep, double& corr)
  37. {
  38. int i, j, num = 0, tmp1 = 0, tmp2 = 0;
  39. uint8_t* pTmp1 = pSrc1;
  40. uint8_t* pTmp2 = pSrc2;
  41. double ave1, ave2, cov1, cov2, std1, std2, cov = 0;
  42. corr = 0;
  43. average(pTmp1, width, height, widthstep, ave1);
  44. average(pTmp2, width, height, widthstep, ave2);
  45. covariance(pTmp1, width, height, widthstep, ave1, cov1);
  46. covariance(pTmp2, width, height, widthstep, ave2, cov2);
  47. for (i = 0; i < width; i++)
  48. {
  49. for (j = 0; j < height; j++)
  50. {
  51. tmp1 = pTmp1[j * widthstep + i];
  52. tmp2 = pTmp2[j * widthstep + i];
  53. cov += (tmp1 - ave1) * (tmp2 - ave2);
  54. }
  55. }
  56. num = width * height;
  57. std1 = sqrt(cov1);
  58. std2 = sqrt(cov2);
  59. corr = cov / (num) / std1 / std2;
  60. }
  61. int calDiff(uint8_t* pSrc1, uint8_t* pSrc2, int width, int height, double& sum)
  62. {
  63. uchar a, b;
  64. int c;
  65. int nim = 0;
  66. int nMax = -255, nMin = 255;
  67. uint8_t* pTmp1, * pTmp2;
  68. cv::Mat result1 = cv::Mat(height, width, CV_8UC1);
  69. sum = 0;
  70. pTmp1 = pSrc1;
  71. pTmp2 = pSrc2;
  72. for (int i = 0; i < height; i++)
  73. {
  74. for (int j = 0; j < width; j++)
  75. {
  76. c = (int)(pTmp1[0] - pTmp2[0]);
  77. if (c > nMax) nMax = c;
  78. if (c < nMin) nMin = c;
  79. pTmp1++;
  80. pTmp2++;
  81. }
  82. }
  83. double scale = 0;
  84. if (nMax - nMin > 255)
  85. {
  86. scale = (255 / (double)(nMax - nMin));
  87. }
  88. else if (nMax - nMin < 255)
  89. {
  90. scale = 1.0;
  91. //方法2:scale = (255 / (double)(nMax - nMin));
  92. }
  93. else if (nMax == nMin)
  94. {
  95. }
  96. pTmp1 = pSrc1;
  97. pTmp2 = pSrc2;
  98. int diffNum = 0;
  99. for (int i = 0; i < height; i++)
  100. {
  101. for (int j = 0; j < width; j++)//col
  102. {
  103. c = (int)(pTmp1[0] - pTmp2[0]);
  104. if (c == 0)
  105. {
  106. result1.at<uchar>(i, j) = 0;
  107. }
  108. else
  109. {
  110. sum = sum + (c - nMin) * scale;
  111. result1.at<uchar>(i, j) = (c - nMin) * scale;
  112. diffNum++;
  113. }
  114. pTmp1++;
  115. pTmp2++;
  116. }
  117. }
  118. sum = sum / width / height;
  119. double ave = 0, corr = 0;
  120. ave = sum / diffNum;
  121. for (int i = 0; i < height; i++)
  122. {
  123. for (int j = 0; j < width; j++)//col
  124. {
  125. c = result1.at<uchar>(i, j);
  126. if (c > 0)
  127. {
  128. corr += (c - ave) * (c - ave);
  129. }
  130. }
  131. }
  132. corr = corr / diffNum;
  133. return corr;
  134. }
  135. void varVector(std::vector<double>& vData, double& var)
  136. {
  137. var = 0;
  138. int i;
  139. int num = vData.size();
  140. int calNum = 0;
  141. double tmp[30] = { 0 };
  142. for (i = 0; i < num; i++)
  143. {
  144. if (vData[i] != 0)
  145. {
  146. tmp[calNum] = vData[i];
  147. calNum++;
  148. }
  149. }
  150. if (calNum > 2)
  151. {
  152. double sum = 0;
  153. for (i = 0; i < calNum; i++)
  154. {
  155. sum += tmp[i];
  156. }
  157. double mean = sum / calNum;
  158. double val = 0;
  159. for (i = 0; i < calNum; i++)
  160. {
  161. var += (tmp[i] - mean) * (tmp[i] - mean);
  162. val = std::abs(tmp[i] - mean);
  163. }
  164. var = var / calNum;
  165. }
  166. }
  167. void cannyVector(std::vector<double>& vData, double& cannyCorr)
  168. {
  169. cannyCorr = 0;
  170. int i;
  171. int num = vData.size();
  172. if (num > 1)
  173. {
  174. double sum = 0;
  175. for (i = 0; i < num; i++)
  176. {
  177. sum += vData[i];
  178. }
  179. double mean = sum / num;
  180. double val = 0;
  181. for (i = 0; i < num; i++)
  182. {
  183. cannyCorr += (vData[i] - mean) * (vData[i] - mean);
  184. val = std::abs(vData[i] - mean);
  185. }
  186. cannyCorr = cannyCorr / num;
  187. }
  188. }
  189. void cannyProcess(cv::Mat& src, double& ratio)
  190. {
  191. cv::Mat canny = cv::Mat::zeros(src.rows, src.cols, CV_8UC1);
  192. //threshold1和threshold2 当中的小阈值用来控制边缘连接,大的阈值用来控制强边缘的初始分割。
  193. Canny(src, canny, 80, 150, 3);
  194. int c = 0, num = 0;
  195. int rowNumber = src.rows;//height
  196. int colNumber = src.cols;
  197. for (int i = 0; i < rowNumber; i++)
  198. {
  199. uchar* data = canny.ptr<uchar>(i);
  200. for (int j = 0; j < colNumber; j++)
  201. {
  202. c = data[j];
  203. if (c > 0)
  204. {
  205. num++;
  206. }
  207. }
  208. }
  209. ratio = num * 1000 / (rowNumber * colNumber);
  210. canny.release();
  211. }