Commit 0dddb282 authored by 付尧(20研)'s avatar 付尧(20研)

update simi

parent a30e485e
#include<opencv2\opencv.hpp>
#include "Global_variable.h"
using namespace cv;
using namespace std;
double distance(Point2d a, Point2d b)
......@@ -13,9 +14,18 @@ void KeyPointsToPoints(vector<KeyPoint> kpts, vector<Point2d> &pts, int minx, in
pts.push_back(kpts[i].pt);
}
}
bool sortwccenter(Point2d a, Point2d b)
double CosSimi(vector<double>cr1, vector<double>cr2)
{
double sum1 = 0, sum2 = 0, mul = 0;
for (int i = 0; i < cr1.size(); i++)
{
sum1 += cr1[i] * cr1[i];
sum2 += cr2[i] * cr2[i];
mul += cr1[i] * cr2[i];
}
double fm = pow(sum1 * sum2,0.5);
return mul / fm;
}
class EleCTag
{
......@@ -27,8 +37,8 @@ public:
};
void sortCornerWCenter(EleCTag &ECTag, int midx, int midy)//调整白圆和角点顺序
{
cout << midx << endl;
cout << midy << endl;
//cout << midx << endl;
//cout << midy << endl;
double vectmp1[2] = { ECTag.corner[2].x - ECTag.corner[1].x, ECTag.corner[2].y - ECTag.corner[1].y };
double vectmp2[2] = { ECTag.corner[1].x - ECTag.corner[0].x, ECTag.corner[1].y - ECTag.corner[0].y };
double t = vectmp1[0] * vectmp2[1] - vectmp1[1] * vectmp2[0];//叉乘
......@@ -90,7 +100,7 @@ void sortBCenter(EleCTag &ECTag)
if (distance(a, ECTag.wccenter[3]) < distance(b, ECTag.wccenter[3]))
return true;
});
cout << "ECTag.bccenter:" << ECTag.bccenter << endl;
//cout << "ECTag.bccenter:" << ECTag.bccenter << endl;
}
double c2ST(Point2d a, Point2d b, Point2d c)//计算三角形面积的2倍
{
......@@ -100,6 +110,7 @@ double c2ST(Point2d a, Point2d b, Point2d c)//
void countCR(EleCTag &ECTag)//计算交比
{
ECTag.cr.clear();
//cout << "================================" << endl;
for (int i = 0; i < 7; i++)
{
double st1 = c2ST(ECTag.wccenter[i], ECTag.bccenter[0], ECTag.bccenter[1]);
......@@ -107,20 +118,27 @@ void countCR(EleCTag &ECTag)//
double st3 = c2ST(ECTag.wccenter[i], ECTag.bccenter[0], ECTag.bccenter[2]);
double st4 = c2ST(ECTag.wccenter[i], ECTag.bccenter[1], ECTag.bccenter[3]);
double cr1 = st1 * st2 / st3 / st4;
cout << "cr:" << cr1 << endl;
//cout << "" << cr1 << ",";
ECTag.cr.push_back(cr1);
}
}
void EleCTag_detector(Mat img)
{
//string imgpath = "F:/11cTag/phone.bmp";
//Mat img = imread(imgpath, 0);
Mat local;
adaptiveThreshold(img, local, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 51, 11);
const clock_t begin_time = clock();
Mat img2,local;
adaptiveThreshold(img, img2, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 51, 11);
GaussianBlur(img2, local, Size(3, 3), 0);
float seconds = float(clock() - begin_time);
//cout << "adaptiveThreshold:" << seconds << endl;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(local, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
cout << hierarchy[0] << endl;
seconds = float(clock() - begin_time);
//cout << "findContours:" << seconds << endl;
//cout << hierarchy[0] << endl;
Mat imageContours = Mat::zeros(local.size(), CV_8UC1);
Mat Contours = Mat::zeros(local.size(), CV_8UC1); //绘制
Mat dstImg = Mat::zeros(local.size(), CV_8UC1); //绘制
......@@ -136,7 +154,6 @@ void EleCTag_detector(Mat img)
//Contours.at<uchar>(P) = 255;
//}
//cout << "i:" << i << endl;
approxPolyDP(Mat(contours[i]), contours_poly[i], arcLength(Mat(contours[i]), true)*0.02, true);
//矩形必须是四个点,必须是凸的,
if (contours_poly[i].size() == 4 && fabs(contourArea(Mat(contours_poly[i]))) > 1000 && isContourConvex(Mat(contours_poly[i])))
......@@ -148,28 +165,35 @@ void EleCTag_detector(Mat img)
int maxx = 0, maxy = 0, minx = INT_MAX, miny = INT_MAX;
for (int j = 0; j < 4; j++)
{
maxx = max(contours_poly[i][j].x, maxx);
maxy = max(contours_poly[i][j].y, maxy);
minx = min(contours_poly[i][j].x, minx);
miny = min(contours_poly[i][j].y, miny);
maxx = (max)(contours_poly[i][j].x, maxx);
maxy = (max)(contours_poly[i][j].y, maxy);
minx = (min)(contours_poly[i][j].x, minx);
miny = (min)(contours_poly[i][j].y, miny);
}
int xlen = maxx - minx;
int ylen = maxy - miny;
int ylen = maxy - miny ;
Rect rect(minx, miny, xlen, ylen);
Mat roi = Mat(imgtmp, rect);
Mat local_roi= Mat(local, rect);
Mat showroi = roi.clone();
SimpleBlobDetector::Params params;//检测白色圆
//imshow("ROI2", showroi);
//waitKey(0);
//imshow("local_roi", local_roi);
//waitKey(0);
params.filterByColor = true;
params.blobColor = 255;
params.filterByCircularity = true; //斑点圆度的限制变量
params.minCircularity = 0.2f; //斑点的最小圆度
params.filterByArea = true;
params.maxArea = xlen * ylen / 10;
params.minConvexity = 0.8f;
//cout << "params.maxArea:" << params.maxArea << endl;
cv::Ptr<cv::SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
vector<KeyPoint> keyp;
detector->detect(roi, keyp);
//cout <<"keyp.size():"<< keyp.size() << endl;
if (keyp.size() == 7) //7个白色的圆
{
int midx = (maxx + minx) / 2;
......@@ -186,13 +210,19 @@ void EleCTag_detector(Mat img)
params2.blobColor = 0;
params2.filterByCircularity = true;
params2.minCircularity = 0.2f;
params2.minConvexity = 0.8f;
cv::Ptr<cv::SimpleBlobDetector> detector2 = SimpleBlobDetector::create(params2);
vector<KeyPoint> keyp2;
detector2->detect(roi, keyp2);
KeyPointsToPoints(keyp2, ECTag.bccenter, minx, miny);
sortBCenter(ECTag);
cout << "keyp2.size:" << keyp2.size() << endl;
//cout << "keyp2.size:" << keyp2.size() << endl;
countCR(ECTag);
for (int k = 0; k < Global_variable::TagCR.rows; k++)
{
double simi = CosSimi(ECTag.cr, Global_variable::TagCR.row(k));
//cout << "simi:" << simi << endl;
}
//imshow("ROI", roi);
//waitKey(0);
}
......@@ -200,6 +230,8 @@ void EleCTag_detector(Mat img)
//drawContours(imageContours, contours, i, Scalar(255), 1, 8, hierarchy);
}
//seconds = float(clock() - begin_time);
//cout << "others:" << seconds << endl;
//cv::namedWindow("approx", 0);
//imshow("approx", dstImg);
//imshow("Contours Image", imageContours); //轮廓
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment