opencv获取中心点位置代码:
Mat SrcMat(nImgWidth, nImgHeight, CV_8UC3);
UCharToMat(pSrcImg, nImgHeight, nImgWidth, 24, SrcMat);
Mat grayMat;
cvtColor(SrcMat, grayMat, COLOR_BGR2GRAY);
Mat binMat = cv::Mat::zeros(SrcMat.size(), CV_8UC1);
MYThresHold(grayMat.data, binMat.data, SrcMat.cols, SrcMat.rows, params.nGrayMin, params.nGrayMax);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(binMat, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
for (size_t i = 0; i < contours.size(); ++i) {
double fArea = contourArea(contours[i]);
if (fArea < params.nAreaMin || fArea > params.nAreaMax) {
continue;
}
Moments mm = moments(contours[i], false);
Point2f pnt = Point2f(mm.m10 / mm.m00, mm.m01 / mm.m00);//计算中心点
ptCenter.push_back(pnt);
}
获取手眼坐标变换系数:
Mat warpMat = estimateAffine2D(points_camera, points_robot);//仿射变换
double rms = 0;
if (warpMat.cols == 3 && warpMat.rows == 2)
{
double A = warpMat.ptr<double>(0)[0];
double B = warpMat.ptr<double>(0)[1];
double C = warpMat.ptr<double>(0)[2];
double D = warpMat.ptr<double>(1)[0];
double E = warpMat.ptr<double>(1)[1];
double F = warpMat.ptr<double>(1)[2];
/*****************************************************RMS误差***********************************************/
double sumX = 0, sumY = 0;
for (int i = 0; i < 9; i++)
{
Point2f pt;
pt.x = A * points_camera[i].x + B * points_camera[i].y + C;
pt.y = D * points_camera[i].x + E * points_camera[i].y + F;
sumX += pow(points_robot[i].x - pt.x, 2);
sumY += pow(points_robot[i].y - pt.y, 2);
}
rms = sqrt(sumX / 9) + sqrt(sumY / 9);
}
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 9; i++)
{
/**********************************************对X操作**********************************************************/
points_robot[i].x += 0.01;
double curr = RMS_Add(points_camera, points_robot);
if (curr < rms)
{
while (curr < rms)
{
points_robot[i].x += 0.01;
rms = curr;
curr = RMS_Add(points_camera, points_robot);
}
points_robot[i].x -= 0.01;
}
else
{
points_robot[i].x -= 0.02;
curr = RMS_Add(points_camera, points_robot);
while (curr < rms)
{
points_robot[i].x -= 0.01;
rms = curr;
curr = RMS_Add(points_camera, points_robot);
}
points_robot[i].x += 0.01;
}
/**********************************************对Y操作**********************************************************/
points_robot[i].y += 0.01;
curr = RMS_Add(points_camera, points_robot);
if (curr < rms)
{
while (curr < rms)
{
points_robot[i].y += 0.01;
rms = curr;
curr = RMS_Add(points_camera, points_robot);
}
points_robot[i].y -= 0.01;
}
else
{
points_robot[i].y -= 0.02;
curr = RMS_Add(points_camera, points_robot);
while (curr < rms)
{
points_robot[i].y -= 0.01;
rms = curr;
curr = RMS_Add(points_camera, points_robot);
}
points_robot[i].y += 0.01;
}
}
}
Mat warp = estimateAffine2D(points_camera, points_robot);//仿射变换
return warp;
最后,图像上得坐标转变为机械坐标:
ptR.x = _stCalibrateParams.A * ptI.x + _stCalibrateParams.B * ptI.y + _stCalibrateParams.C;
ptR.y = _stCalibrateParams.D * ptI.x + _stCalibrateParams.E * ptI.y + _stCalibrateParams.F;
代码连接:https://download.csdn.net/download/weixin_42789529/88441518
推荐一个好用课堂管理小程序,欢迎扫码使用: