123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include "DataProcess.h"
- void average(uint8_t* pSrc, int width, int height, int widthstep, double& ave)
- {
- double value = 0;
- int i, j, k = 0, tmp = 0;
- uint8_t* pTmp;
- pTmp = pSrc;
- for (i = 0; i < width; i++)
- {
- for (j = 0; j < height; j++)
- {
- tmp = (int)(pTmp[j * widthstep + i]);
- value += tmp;
- }
- }
- value = value / (width * height);
- ave = value;
- }
- void covariance(uint8_t* pSrc, int width, int height, int widthstep, double ave, double& cov)
- {
- float value = 0;
- int i, j, tmp = 0;
- uint8_t* pTmp;
- pTmp = pSrc;
- for (i = 0; i < width; i++)
- {
- for (j = 0; j < height; j++)
- {
- tmp = (int)(pTmp[j * widthstep + i]);;
- value += (tmp - ave) * (tmp - ave);
- }
- }
- value = value / (width * height);
- cov = value;
- }
- void calcorrcoef(uint8_t* pSrc1, uint8_t* pSrc2, int width, int height, int widthstep, double& corr)
- {
- int i, j, num = 0, tmp1 = 0, tmp2 = 0;
- uint8_t* pTmp1 = pSrc1;
- uint8_t* pTmp2 = pSrc2;
- double ave1, ave2, cov1, cov2, std1, std2, cov = 0;
- corr = 0;
- average(pTmp1, width, height, widthstep, ave1);
- average(pTmp2, width, height, widthstep, ave2);
- covariance(pTmp1, width, height, widthstep, ave1, cov1);
- covariance(pTmp2, width, height, widthstep, ave2, cov2);
- for (i = 0; i < width; i++)
- {
- for (j = 0; j < height; j++)
- {
- tmp1 = pTmp1[j * widthstep + i];
- tmp2 = pTmp2[j * widthstep + i];
- cov += (tmp1 - ave1) * (tmp2 - ave2);
- }
- }
- num = width * height;
- std1 = sqrt(cov1);
- std2 = sqrt(cov2);
- corr = cov / (num) / std1 / std2;
- }
- int calDiff(uint8_t* pSrc1, uint8_t* pSrc2, int width, int height, double& sum)
- {
- uchar a, b;
- int c;
- int nim = 0;
- int nMax = -255, nMin = 255;
- uint8_t* pTmp1, * pTmp2;
- cv::Mat result1 = cv::Mat(height, width, CV_8UC1);
- sum = 0;
- pTmp1 = pSrc1;
- pTmp2 = pSrc2;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- c = (int)(pTmp1[0] - pTmp2[0]);
- if (c > nMax) nMax = c;
- if (c < nMin) nMin = c;
- pTmp1++;
- pTmp2++;
- }
- }
- double scale = 0;
- if (nMax - nMin > 255)
- {
- scale = (255 / (double)(nMax - nMin));
- }
- else if (nMax - nMin < 255)
- {
- scale = 1.0;
- //方法2:scale = (255 / (double)(nMax - nMin));
- }
- else if (nMax == nMin)
- {
- }
- pTmp1 = pSrc1;
- pTmp2 = pSrc2;
- int diffNum = 0;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)//col
- {
- c = (int)(pTmp1[0] - pTmp2[0]);
- if (c == 0)
- {
- result1.at<uchar>(i, j) = 0;
- }
- else
- {
- sum = sum + (c - nMin) * scale;
- result1.at<uchar>(i, j) = (c - nMin) * scale;
- diffNum++;
- }
- pTmp1++;
- pTmp2++;
- }
- }
- sum = sum / width / height;
- double ave = 0, corr = 0;
- ave = sum / diffNum;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)//col
- {
- c = result1.at<uchar>(i, j);
- if (c > 0)
- {
- corr += (c - ave) * (c - ave);
- }
- }
- }
- corr = corr / diffNum;
- return corr;
- }
- void varVector(std::vector<double>& vData, double& var)
- {
- var = 0;
- int i;
- int num = vData.size();
- int calNum = 0;
- double tmp[30] = { 0 };
- for (i = 0; i < num; i++)
- {
- if (vData[i] != 0)
- {
- tmp[calNum] = vData[i];
- calNum++;
- }
- }
- if (calNum > 2)
- {
- double sum = 0;
- for (i = 0; i < calNum; i++)
- {
- sum += tmp[i];
- }
- double mean = sum / calNum;
- double val = 0;
- for (i = 0; i < calNum; i++)
- {
- var += (tmp[i] - mean) * (tmp[i] - mean);
- val = std::abs(tmp[i] - mean);
- }
- var = var / calNum;
- }
- }
- void cannyVector(std::vector<double>& vData, double& cannyCorr)
- {
- cannyCorr = 0;
- int i;
- int num = vData.size();
- if (num > 1)
- {
- double sum = 0;
- for (i = 0; i < num; i++)
- {
- sum += vData[i];
- }
- double mean = sum / num;
- double val = 0;
- for (i = 0; i < num; i++)
- {
- cannyCorr += (vData[i] - mean) * (vData[i] - mean);
- val = std::abs(vData[i] - mean);
- }
- cannyCorr = cannyCorr / num;
- }
- }
- void cannyProcess(cv::Mat& src, double& ratio)
- {
- cv::Mat canny = cv::Mat::zeros(src.rows, src.cols, CV_8UC1);
- //threshold1和threshold2 当中的小阈值用来控制边缘连接,大的阈值用来控制强边缘的初始分割。
- Canny(src, canny, 80, 150, 3);
- int c = 0, num = 0;
- int rowNumber = src.rows;//height
- int colNumber = src.cols;
- for (int i = 0; i < rowNumber; i++)
- {
- uchar* data = canny.ptr<uchar>(i);
- for (int j = 0; j < colNumber; j++)
- {
- c = data[j];
- if (c > 0)
- {
- num++;
- }
- }
- }
- ratio = num * 1000 / (rowNumber * colNumber);
- canny.release();
- }
|