#include<iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("F:\\PIN_LENGTH\\3.bmp");
Mat dst = Mat::zeros(src.size(), src.type());
//点
Mat ROI_src_point = src(Range(1650,1750), Range(650,1650)); //ROI端点
Mat ROI_src_point_gray;
cvtColor(ROI_src_point, ROI_src_point_gray, COLOR_BGR2GRAY, 1); //ROI端点的灰度图
ROI_src_point_gray = ROI_src_point_gray > 150; //阀值二值化
//线
Mat ROI_src_line = src(Range(1400, 1450), Range(650, 1650)); //ROI直线
Mat ROI_src_line_gray;
cvtColor(ROI_src_line, ROI_src_line_gray, COLOR_BGR2GRAY, 1); //ROI直线的灰度图
ROI_src_line_gray = ROI_src_line_gray > 150; //阀值二值化
//**********************************【找出点所在轮廓】**********************************************
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(ROI_src_point_gray, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//用最小矩形包围轮廓
vector<Rect> boundRect( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
boundRect[i] = boundingRect( Mat(contours[i]) );
}
vector<Point> pin_point;
//筛选出符合要求的矩形, 并计算出尖点
for( int i = 0; i< boundRect.size(); i++ )
{
if(boundRect[i].br().y - boundRect[i].tl().y > 20)
{
rectangle( ROI_src_point, boundRect[i].tl(), boundRect[i].br(), Scalar(0,0,255), 2, 8, 0 );
float aa = boundRect[i].br().x - (boundRect[i].br().x - boundRect[i].tl().x) / 2.0;
float bb = boundRect[i].br().y;
Point p = Point(aa,bb);
pin_point.push_back(p);
}
}
//对尖点进行排序(轮廓选举出的为逆排序,我不知道其他情况轮廓的产生顺序, 最好进行一下排序)
vector<Point> xx;
vector<Point>::reverse_iterator it;
for(it = pin_point.rbegin(); it != pin_point.rend(); it++)
{
xx.push_back(*it);
}
//冒泡排序
for (int i = 0; i < xx.size() - 1; i++)
{
for(int j = i + 1; j < xx.size(); j++)
{
if(xx[i].x > xx[j].x)
{
Point p;
p = xx[i];
xx[i] = xx[j];
xx[j] = p;
cout << endl<< p << endl;
}
}
}
for(int i = 0; i < xx.size(); i ++) //最终xx为需要的点集
{
xx[i].x += 650;
xx[i].y += 1650;
}
for(int i = 0; i < xx.size(); i++)
{
circle(src, xx[i], 30,Scalar(0,0,255),0);
circle(src, xx[i],2,Scalar(0,255,0),-1);
}
//************************************【找出线的点集】********************************************************
vector<vector<Point>> contours_line;
vector<Vec4i> hierarchy_line;
findContours(ROI_src_line_gray, contours_line, hierarchy_line, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//用最小矩形包围轮廓
vector<Rect> boundRect_line( contours_line.size() );
for( int i = 0; i < contours_line.size(); i++ )
{
boundRect_line[i] = boundingRect( Mat(contours_line[i]) );
}
vector<Point> line_point;
//筛选出符合要求的矩形, 并计算出尖点
for( int i = 0; i< boundRect_line.size(); i++ )
{
if(boundRect_line[i].br().y == 49 && boundRect_line[i].br().x - boundRect_line[i].tl().x > 10) //筛选条件(49为1450-1400 从0开始的)
{
rectangle( ROI_src_line, boundRect_line[i].tl(), boundRect_line[i].br(), Scalar(0,0,255), 2, 8, 0 );
cout << boundRect_line[i].tl() << " " << boundRect_line[i].br() << endl;
float aa = boundRect_line[i].br().x;
float bb = boundRect_line[i].tl().y;
Point p = Point(aa,bb);
line_point.push_back(boundRect_line[i].tl());
line_point.push_back(p);
cout << p << endl;
}
}
for(int i = 0; i < line_point.size(); i ++) //最终line_point为需要的点集
{
line_point[i].x += 650;
line_point[i].y += 1400;
}
for(int i = 0; i < line_point.size(); i++)
{
circle(src, line_point[i], 10,Scalar(0,0,255),0);
circle(src, line_point[i],2,Scalar(0,255,0),-1);
}
//拟合直线
Vec4f line_para;
cv::fitLine(line_point, line_para, CV_DIST_L2, 0, 0.01, 0.01);
cout << endl<<line_para;
//获取点斜式的点和斜率
cv::Point point0;
point0.x = line_para[2];
point0.y = line_para[3];
double k = line_para[1] / line_para[0];
//计算直线的端点(y = k(x - x0) + y0)
cv::Point point1, point2;
point1.x = 0;
point1.y = k * (0 - point0.x) + point0.y;
point2.x = src.cols-100;
point2.y = k * (src.cols-100 - point0.x) + point0.y;
cv::line(src, point1,point2, cv::Scalar(255,0,0), 1, 8, 0); //画出这条线
//********************************************【求出端点到直线的距离】**************************************
vector<double> solution;
double A = point1.y - point2.y;
double B = point2.x - point1.x;
double C = point1.x * point2.y - point2.x * point1.y;
cout <<"***********************"<<endl;
for(int i = 0; i < xx.size(); i++)
{
double b = fabs(A * xx[i].x + B * xx[i].y + C);
double n = b / sqrt(A * A + B * B);
solution.push_back(n);
cout << n << endl;
string s = to_string(n);
putText(src, s,xx[i],FONT_HERSHEY_SIMPLEX,0.5,Scalar(150,150,0),2,8); //把数值写在旁边
}
namedWindow("nn", WINDOW_NORMAL);
imshow("nn",src);
imshow("over", ROI_src_point);
imshow("hh", ROI_src_line);
waitKey(0);
}
下面这个是用canny检测, 而不是阈值, 但是是像素精度, 需要把它改成亚像素精度, 这样就要看opencv源代码了。
比如boundingrect() 如何输出亚像素精度坐标, 以及我用到的其他函数, 边缘检测, 都需要修改。
#include<iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("F:\\PIN_LENGTH\\9.bmp");
Mat dst = Mat::zeros(src.size(), src.type());
//点
Mat ROI_src_point = src(Range(1650,1750), Range(650,1650)); //ROI端点
Mat ROI_src_point_;
cvtColor(ROI_src_point, ROI_src_point_, COLOR_BGR2GRAY, 1); //ROI端点的灰度图
Mat ROI_src_point_gray; //canny检测, 用双边滤波(保边)
bilateralFilter(ROI_src_point_, ROI_src_point_gray, 10, 15, 5);
Canny(ROI_src_point_gray, ROI_src_point_gray, 100, 200, 3, true);
//线
Mat ROI_src_line = src(Range(1400, 1450), Range(650, 1650)); //ROI直线
Mat ROI_src_line_ ;
Mat ROI_src_line_gray;
cvtColor(ROI_src_line, ROI_src_line_ , COLOR_BGR2GRAY, 1); //ROI直线的灰度图
bilateralFilter(ROI_src_line_, ROI_src_line_gray, 10, 15, 5); //双边滤波, canny检测
Canny(ROI_src_line_gray, ROI_src_line_gray, 100, 200, 3, true);
//**********************************【找出点所在轮廓】**********************************************
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(ROI_src_point_gray, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
//用最小矩形包围轮廓
vector<Rect> boundRect( contours.size() );
for(unsigned int i = 0; i < contours.size(); i++ )
{
boundRect[i] = boundingRect( Mat(contours[i]) );
}
vector<Point> pin_point;
//筛选出符合要求的矩形, 并计算出尖点
for(unsigned int i = 0; i< boundRect.size(); i++ )
{
if(boundRect[i].br().y - boundRect[i].tl().y > 10 && boundRect[i].br().x - boundRect[i].tl().x > 13 && boundRect[i].tl().y == 1)
{
rectangle(ROI_src_point, boundRect[i].tl(), boundRect[i].br(), Scalar(0,0,255), 2, 8, 0 );
cout << boundRect[i].br().x - boundRect[i].tl().x << "********" << endl;
double aa = boundRect[i].br().x - (boundRect[i].br().x - boundRect[i].tl().x) / 2.0;
double bb = boundRect[i].br().y;
Point2f p = Point2f(aa,bb);
pin_point.push_back(p);
}
}
cout << "^^^^^^^^^^^^^^^^^^"<< endl << pin_point << endl;
//对尖点进行排序(轮廓选举出的为逆排序,我不知道其他情况轮廓的产生顺序, 最好进行一下排序)
vector<Point> xx;
vector<Point>::reverse_iterator it;
for(it = pin_point.rbegin(); it != pin_point.rend(); it++)
{
xx.push_back(*it);
}
//冒泡排序
for (unsigned int i = 0; i < xx.size() - 1; i++)
{
for(unsigned int j = i + 1; j < xx.size(); j++)
{
if(xx[i].x > xx[j].x)
{
Point p;
p = xx[i];
xx[i] = xx[j];
xx[j] = p;
cout << endl<< p << endl;
}
}
}
for(unsigned int i = 0; i < xx.size(); i ++) //最终xx为需要的点集
{
xx[i].x += 650;
xx[i].y += 1650;
}
for(unsigned int i = 0; i < xx.size(); i++)
{
circle(src, xx[i], 30,Scalar(0,0,255),0);
circle(src, xx[i],2,Scalar(0,255,0),-1);
}
//************************************【找出线的点集】********************************************************
vector<vector<Point>> contours_line;
vector<Vec4i> hierarchy_line;
findContours(ROI_src_line_gray, contours_line, hierarchy_line, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
//用最小矩形包围轮廓
vector<Rect> boundRect_line( contours_line.size() );
for( unsigned int i = 0; i < contours_line.size(); i++ )
{
boundRect_line[i] = boundingRect( Mat(contours_line[i]) );
}
vector<Point> line_point;
//筛选出符合要求的矩形, 并计算出尖点
for(unsigned int i = 0; i< boundRect_line.size(); i++ )
{
if(boundRect_line[i].br().y == 49 && boundRect_line[i].br().x - boundRect_line[i].tl().x > 15) //筛选条件(49为1450-1400 从0开始的)
{
rectangle( ROI_src_line, boundRect_line[i].tl(), boundRect_line[i].br(), Scalar(0,0,255), 2, 8, 0 );
cout << boundRect_line[i].tl() << " " << boundRect_line[i].br() << endl;
float aa = boundRect_line[i].br().x;
float bb = boundRect_line[i].tl().y;
Point2f p = Point2f(aa,bb);
line_point.push_back(boundRect_line[i].tl());
line_point.push_back(p);
cout << p << endl;
}
}
for(unsigned int i = 0; i < line_point.size(); i ++) //最终line_point为需要的点集
{
line_point[i].x += 650;
line_point[i].y += 1400;
}
for(unsigned int i = 0; i < line_point.size(); i++)
{
circle(src, line_point[i], 10,Scalar(0,0,255),0);
circle(src, line_point[i],2,Scalar(0,255,0),-1);
}
//拟合直线
Vec4f line_para;
cv::fitLine(line_point, line_para, CV_DIST_L2, 0, 0.01, 0.01);
cout << endl<<line_para;
//获取点斜式的点和斜率
cv::Point2f point0;
point0.x = line_para[2];
point0.y = line_para[3];
double k = line_para[1] / line_para[0];
//计算直线的端点(y = k(x - x0) + y0)
cv::Point2f point1, point2;
point1.x = 0;
point1.y = k * (0 - point0.x) + point0.y;
point2.x = src.cols-100;
point2.y = k * (src.cols-100 - point0.x) + point0.y;
cv::line(src, point1,point2, cv::Scalar(255,0,0), 1, 8, 0); //画出这条线
//********************************************【求出端点到直线的距离】**************************************
vector<double> solution;
double A = point1.y - point2.y;
double B = point2.x - point1.x;
double C = point1.x * point2.y - point2.x * point1.y;
cout <<"***********************"<<endl;
for(unsigned int i = 0; i < xx.size(); i++)
{
double b = fabs(A * xx[i].x + B * xx[i].y + C);
double n = b / sqrt(A * A + B * B);
solution.push_back(n);
cout << n << endl;
string s = to_string(n);
putText(src, s,xx[i],FONT_HERSHEY_SIMPLEX,0.5,Scalar(150,150,0),2,8); //把数值写在旁边
}
namedWindow("nn", WINDOW_NORMAL);
imshow("nn",src);
namedWindow("canny", WINDOW_NORMAL);
imshow("canny", ROI_src_point);
namedWindow("hh", WINDOW_NORMAL);
imshow("hh", ROI_src_line);
cout << "canny检测";
waitKey(0);
}