#include "ArresterCircularZeroThreeALGO.h" using namespace std; using namespace cv; double ArresterCircularZeroThreeALGO::GetResult(cv::Mat sourceMat, bool IsShowWnd) { Mat mat; sourceMat.copyTo(mat); if (IsShowWnd) { imshow("ԭͼ", mat); waitKey(1); } int matCols = 400; Size size(400, 190); resize(mat, mat, size, (float)matCols / mat.cols, (float)matCols / mat.cols); /* if (mat.cols - matCols >0) { resize(mat, mat, size, (float)matCols/mat.cols , (float)matCols / mat.cols); } else if (mat.cols - matCols < 0) { 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, 201, 1); if (IsShowWnd) { imshow("¶þÖµ»¯Í¼adaptiveThreshold", grayMat); waitKey(1); } Mat biMat; bilateralFilter(grayMat, biMat, 9, 50, 50); if (IsShowWnd) { imshow("Â˲¨È¥Ôëͼ", biMat); waitKey(1); } //Mat gaussMat; GaussianBlur(biMat, grayMat, Size(3, 3), 0, 0); if (IsShowWnd) { imshow("¸ß˹ģºýͼ", grayMat); waitKey(1); } medianBlur(grayMat, grayMat, 3);//ÖÐÖµÂ˲¨ if (IsShowWnd) { imshow("ÖÐÖµÂ˲¨Í¼", grayMat); waitKey(1); } double t1 = getTickCount(); Mat ROI_1 = Mat(height, width, CV_8UC3, Scalar::all(255)); cvtColor(ROI_1, ROI_1, COLOR_BGR2GRAY); // ת»»Îª»Ò¶Èͼ vector vImgs; Mat result; vImgs.push_back(grayMat); vImgs.push_back(ROI_1); vconcat(vImgs, result); //´¹Ö±·½ÏòÆ´½Ó //hconcat(vImgs, result); //ˮƽ·½ÏòÆ´½Ó if (IsShowWnd) { imshow("Æ´½Óͼ", result); waitKey(1); } vector circles; Mat whiteMat(height, width, CV_8UC3, Scalar::all(255)); //Mat resizeMat(,) /* for (size_t i = 0; i < 200; i++) { hough_value += i; HoughCircles(grayMat, circles, HOUGH_GRADIENT, 1, 10, 100, hough_value); }*/ Mat cannyMat; Canny(result, cannyMat, 50, 100);//±ßÔµ¼ì²â if (IsShowWnd) { imshow("±ßÔµ¼ì²âͼ", cannyMat); waitKey(1); } HoughCircles(cannyMat, circles, HOUGH_GRADIENT, 3, 30, 100, 200, 50, 200); vector mylines; //HoughLinesP(grayMat, mylines, 1, CV_PI / 180, 100,5, 1); //for (size_t i = 0; i < mylines.size(); i++) //{ // Vec4i cho_l = mylines[i]; // //cho_l // line(whiteMat, Point(cho_l[0], cho_l[1]), Point(cho_l[2], cho_l[3]), Scalar(255, 0, 0), 1); //} vector rightCircles; Mat resultCopy; result.copyTo(resultCopy); int distance2Center = 50; for (size_t i = 0; i < circles.size(); i++) { if (circles[i][1] > height) { if (abs(circles[i][0] - width / 2) < distance2Center) //Ô²Ðĺá×ø±êºÍ×Ý×ø±êÔÚÆ´½ÓºóͼÏñÖÐÐĸ½½ü { if (abs(circles[i][1] - height) < distance2Center) { rightCircles.push_back(circles[i]); if (IsShowWnd) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); // circle center circle(resultCopy, center, 3, Scalar(0, 0, 0), -1, 5, 0); // circle outline circle(resultCopy, center, radius, Scalar(0, 0, 0), 3, 3, 0); } } } } } if (IsShowWnd) { namedWindow("»ô·òԲͼ"); imshow("»ô·òԲͼ", resultCopy); waitKey(1); } adaptiveThreshold(result, result, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 151, 1); if (IsShowWnd) { imshow("¶þ´Î¶þÖµ»¯Í¼", result); waitKey(1); } //int w = result.cols; //int h = result.rows; //uchar* uc_pixel; //for (int row = 0; row < h; row++) { // uc_pixel = result.data + row * result.step; // for (int col = 0; col < w; col++) { // //uc_pixel[0]; // //sum += uc_pixel[0]; // uc_pixel[1] = 255; // uc_pixel[2] = 255; // uc_pixel[0] = 255; // uc_pixel += 3; // } //} vector zeroPositions; vector> rightNumSums; for (size_t j = 0; j < rightCircles.size(); j++) { Point ptCenter(cvRound(rightCircles[j][0]), cvRound(rightCircles[j][1])); int radius = cvRound(rightCircles[j][2]); Mat darkMatCopy; result.copyTo(darkMatCopy); vector> num_sum; //vector<,> for (size_t i = 300; i < 421; i++) { //Point ptStart(ptCenter.x - radius - 10, ptCenter.y); Point ptStart1(ptCenter.x + sin(i * CV_PI / 180) * (radius + 15), ptCenter.y - cos(i * CV_PI / 180) * (radius + 15)); Point ptStart2(ptCenter.x + sin(i * CV_PI / 180) * (radius - 35), ptCenter.y - cos(i * CV_PI / 180) * (radius - 35)); line(darkMatCopy, ptStart2, ptStart1, Scalar(0, 0, 0), 3); if (IsShowWnd) { imshow("SemicircularMeter", darkMatCopy); //Sleep(1000); waitKey(100); /*string str; std::cin >> str;*/ } int matdis = abs(LineHandle::CalcMatSum(darkMatCopy) - LineHandle::CalcMatSum(result)); /* cout << "ÏñËØ×ÛºÏdarkMatCopy" << i << "£º" << LineHandle::CalcMatSum(darkMatCopy) << endl; cout << "ÏñËØ×ÛºÏresult" << i << "£º" << LineHandle::CalcMatSum(result) << endl; cout << "ÏñËØ×ÛºÏcha " << i << "£º" << matdis << endl;*/ num_sum.push_back(make_pair(i, matdis)); if (num_sum.size() > 3) { if (num_sum[num_sum.size() - 3].second - matdis > 20000 && zeroPositions.size() <= j + 1) { zeroPositions.push_back(i); } } result.copyTo(darkMatCopy); } std::sort(num_sum.begin(), num_sum.end(), LineHandle::ComparePairSecond); if (num_sum.size() > 0) { rightNumSums.push_back(num_sum[0]); } } std::sort(rightNumSums.begin(), rightNumSums.end(), LineHandle::ComparePairSecond); if (rightNumSums.size() == 0) { return 0.01; } std::sort(zeroPositions.begin(), zeroPositions.end()); if (zeroPositions.size() == 0) { zeroPositions.push_back(310); } if (zeroPositions[0] > 330) { zeroPositions[0] = 310; } float resutValue = (float)(rightNumSums[0].first - zeroPositions[0]) / 30 + 0.01; if (abs(resutValue) > 3) { resutValue = 0.01; } return abs(resutValue); }