123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #include "DoublePointerCountALGO.h"
- #include "LineHandle.h"
- using namespace std;
- using namespace cv;
- int DoublePointerCountALGO::GetResult(cv::Mat sourceMat, bool IsShowWnd)
- {
- Mat mat;
- sourceMat.copyTo(mat);
- int matCols = 500;
- Size size(500, 420);
- resize(mat, mat, size, (float)matCols / mat.cols, (float)matCols / mat.cols);
- int width = mat.cols;// .width;
- int height = mat.rows;
- Mat grayMat;
- cvtColor(mat, grayMat, COLOR_BGR2GRAY); // 转换为灰度图
- //Mat thMat;
- adaptiveThreshold(grayMat, grayMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 2);
- if (IsShowWnd) {
- imshow("二值化图adaptiveThreshold", grayMat);
- }
- Mat biMat;
- bilateralFilter(grayMat, biMat, 9, 50, 50);
- if (IsShowWnd) {
- imshow("滤波去噪图", biMat);
- }
- //Mat gaussMat;
- GaussianBlur(biMat, grayMat, Size(3, 3), 0, 0);
- if (IsShowWnd) {
- imshow("高斯模糊图", grayMat);
- }
- medianBlur(grayMat, grayMat, 3);//中值滤波
- if (IsShowWnd) {
- imshow("中值滤波图", grayMat);
- }
- adaptiveThreshold(grayMat, grayMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 2);
- if (IsShowWnd) {
- imshow("二次二值化图", grayMat);
- }
- Mat cannyMat;
- int g_nthreshold = 39;
- int c_cen = min(height, width);
- int w = grayMat.cols;
- int h = grayMat.rows;
- uchar* uc_pixel;
- for (int row = 0; row < h; row++) {
- uc_pixel = grayMat.data + row * grayMat.step;
- for (int col = 0; col < w; col++) {
- uc_pixel[0] = 255 - uc_pixel[0];
- uc_pixel[1] = 255 - uc_pixel[1];
- uc_pixel[2] = 255 - uc_pixel[2];
- uc_pixel += grayMat.channels();
- }
- }
- if (IsShowWnd) {
- imshow("反色图", grayMat);
- }
- //LineHandle lineHandle;
- Vec4i lineX(0, height / 2, width, height / 2);
- Vec4i lineY(width / 2, 0, width / 2, height);
- /* line(grayMat, Point(0, height / 2), Point(width, height / 2), Scalar(0, 0, 0), 1);
- line(grayMat, Point(width / 2, 0), Point(width / 2, height), Scalar(0, 0, 0), 1);*/
- vector<Point2f> tu1;
- //Mat mat(,)
- Mat darkMat(height, width, CV_8UC3, Scalar(0, 0, 0));
- vector<Vec4i> mylines;
- HoughLinesP(grayMat, mylines, 1, CV_PI / 180, 100, min(height / 6, width / 6), 3);
- for (size_t i = 0; i < mylines.size(); i++)
- {
- Vec4i cho_l = mylines[i];
- Point2f crossXPoints;
- LineHandle::crossPointsOfLines(cho_l, lineX, crossXPoints);
- Point2f crossYPoints;
- LineHandle::crossPointsOfLines(cho_l, lineY, crossYPoints);
- if (abs(crossXPoints.x - width / 2) < width / 6 && abs(crossXPoints.y - height / 2) < height / 6)
- {
- line(darkMat, Point(cho_l[0], cho_l[1]), Point(cho_l[2], cho_l[3]), Scalar(255, 0, 0), 10);
- /* circle(grayMat, Point(cho_l[2], cho_l[3]), 2, cv::Scalar(255, 0, 0), -1, cv::FILLED);
- circle(grayMat, Point(cho_l[0], cho_l[1]), 2, cv::Scalar(0, 255, 255), -1, cv::FILLED);*/
- Point2f pt1(cho_l[2] - cho_l[0], cho_l[3] - cho_l[1]);
- Point2f pt2(1, 0);
- float theta = atan2(pt1.x, pt1.y) - atan2(pt2.x, pt2.y);
- if (theta > CV_PI)
- theta -= 2 * CV_PI;
- if (theta < -CV_PI)
- theta += 2 * CV_PI;
- theta = theta * 180.0 / CV_PI;
- float a = pow((cho_l[2] - width / 2), 2) + pow((cho_l[3] - height / 2), 2);
- tu1.push_back(Point2f(sqrtf(a), theta));
- }
- }
- /*vector< Point2f> sorttu;
- sort(tu1, sorttu,);*/
- Point ptStart(width / 2, 0);
- Point ptCenter(width / 2, height / 2);
- Mat darkMatCopy;
- darkMat.copyTo(darkMatCopy);
- vector<pair<int, int>> num_sum;
- //vector<,>
- for (size_t i = 0; i < 360; i++)
- {
- Point ptStart1(ptCenter.x + sin(i * 1 * CV_PI / 180) * (c_cen / 2), ptCenter.y - cos(i * 1 * CV_PI / 180) * (c_cen / 2));
- Point ptStart2(ptCenter.x + sin(i * 1 * CV_PI / 180) * (c_cen / 2 - 100), ptCenter.y - cos(i * 1 * CV_PI / 180) * (c_cen / 2 - 100));
- line(darkMatCopy, ptStart2, ptStart1, Scalar(255, 0, 0), 10);
- if (IsShowWnd)
- {
- imshow("test", darkMatCopy);
- waitKey(10);
- }
- int matdis = LineHandle::CalcMatSum(darkMatCopy) - LineHandle::CalcMatSum(darkMat);
- //cout << "像素综合cha " << i << ":" << matdis << endl;
- int angle = i;
- if (angle > 342)
- {
- angle = 360 - angle;
- }
- num_sum.push_back(make_pair(angle, matdis));
- darkMat.copyTo(darkMatCopy);
- }
- sort(num_sum.begin(), num_sum.end(), LineHandle::ComparePairSecond);
- int resultValue = 0;
- int secondPoint = -1;
- for (size_t i = 0; i < num_sum.size(); i++)
- {
- if (abs(num_sum[0].first - num_sum[i].first) > 18 && abs(num_sum[0].first - num_sum[i].first) < 342) //360° 0°附近特殊处理
- {
- if (num_sum[i].second < 3 * (num_sum[num_sum.size() - 1].second) / 4)
- {
- secondPoint = num_sum[i].first;
- break;
- }
- }
- }
- if (secondPoint != -1)
- {
- int seondScore = num_sum[0].first / 36;
- if (num_sum[0].first % 36 > 18)
- {
- seondScore += 1;
- }
- int firstScore = secondPoint / 36;
- if (secondPoint % 36 > 18)
- {
- firstScore += 1;
- }
- resultValue = firstScore * 10 + seondScore;
- }
- else if (secondPoint == -1)
- {
- int seondScore = num_sum[0].first / 36;
- if (num_sum[0].first % 36 > 18)
- {
- seondScore += 1;
- }
- resultValue = seondScore * 10 + seondScore;
- }
- return resultValue;
- }
|