233 lines
7.4 KiB
C++
233 lines
7.4 KiB
C++
#include "DoublePointerAlgorithm.h"
|
||
#include <iostream>
|
||
#include <SegDetector.h>
|
||
#include <NanoDetector.h>
|
||
#include <opencv2\imgproc\types_c.h>
|
||
#include <opencv2/imgproc/imgproc_c.h>
|
||
#include "LineHandle.h"
|
||
#include<numeric>
|
||
#include<windows.h>
|
||
using namespace std;
|
||
using namespace cv;
|
||
int DoublePointerAlgorithm::GetResult(cv::Mat sourceMat, bool IsShowWnd )
|
||
{
|
||
Mat mat;
|
||
sourceMat.copyTo(mat);
|
||
int matCols = 500;
|
||
Size size;
|
||
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;
|
||
vector<Vec3f> circles;
|
||
vector<Vec4i> mylines;
|
||
|
||
//Canny(grayMat, cannyMat, 50, 100);//边缘检测
|
||
//if (IsShowWnd) {
|
||
// imshow("边缘检测图", cannyMat);
|
||
// waitKey(1);
|
||
|
||
//}
|
||
// HoughCircles(cannyMat, circles, HOUGH_GRADIENT, 10, 1, 100, 100, 0,20);
|
||
//cvtColor(cannyMat, dst_img, CV_GRAY2BGR);
|
||
//HoughLines(,)
|
||
//vector<Vec4i> newLises;
|
||
//Mat resultCopy;
|
||
//grayMat.copyTo(resultCopy);
|
||
//for (size_t i = 0; i < circles.size(); i++)
|
||
//{
|
||
// if (abs(circles[i][0] - width / 2) < 50 && abs(circles[i][1] - height / 2) < 50)
|
||
// {
|
||
// 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)
|
||
// {
|
||
// imshow("test234", resultCopy);
|
||
// waitKey(300);
|
||
// }
|
||
|
||
// }
|
||
|
||
//}
|
||
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);
|
||
}
|
||
|
||
//des uc_pixel
|
||
HoughLinesP(grayMat, mylines, 1, CV_PI / 180, 100, 100, 3);
|
||
|
||
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));
|
||
for (size_t i = 0; i < mylines.size(); i++)
|
||
{
|
||
|
||
Vec4i cho_l = mylines[i];
|
||
// line(grayMat, Point(cho_l[0], cho_l[1]), Point(cho_l[2], cho_l[3]), Scalar(0, 0, 0), 1);
|
||
Point2f crossPoints;
|
||
lineHandle.crossPointsOfLines(cho_l, lineX, crossPoints);
|
||
//cv::circle(grayMat, crossPoints, 2, cv::Scalar(0, 0, 255), -1, cv::FILLED);
|
||
if (abs(crossPoints.x - width / 2) < 50)
|
||
{
|
||
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 - 80), ptCenter.y - cos(i * 1 * CV_PI / 180) * (c_cen / 2 - 80));
|
||
|
||
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) > 20)
|
||
{
|
||
if (i<20)
|
||
{
|
||
secondPoint = num_sum[i].first;
|
||
break;
|
||
}
|
||
else if (num_sum[i].second < 80*10*255)
|
||
{
|
||
secondPoint = num_sum[i].first;
|
||
break;
|
||
}
|
||
}
|
||
/*if (abs(num_sum[i].first - 360)<18 || abs(num_sum[i].first - 0) < 18)
|
||
{
|
||
|
||
}*/
|
||
}
|
||
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;
|
||
}
|
||
cout << "结果 :" << resultValue << endl;
|
||
//if (5 * (num_sum[0].second) < num_sum[1].second)
|
||
//{
|
||
// resultValue = num_sum[0].first + 10 * num_sum[0].first;
|
||
// cout << "结果 :" << resultValue << endl;
|
||
//}
|
||
//else
|
||
//{
|
||
// resultValue = num_sum[0].first + 10 * num_sum[1].first;
|
||
|
||
// cout << "结果 :" << resultValue << endl;
|
||
//}
|
||
//if (IsShowWnd)
|
||
//{
|
||
// //waitKey(1000*10);
|
||
// //destroyAllWindows();
|
||
//}
|
||
return resultValue;
|
||
}
|