……… //在findContours之前一般先需要对图像做预处理,例如形态学操作、阈值分割等等以保证findContours找到的边界准确
std::vector
cv::findContours(index_img, mContoursPanel, cv::RETR_EXTERNAL,
cv::CHAIN_APPROX_NONE, cv::Point(0, 0));//把物体的最外边界提取出来
Moments moment;//矩
cv::Mat temp(mContoursPanel.at(0));//第一个轮廓
moment = moments(temp, false);
cv::Point pt1;
if (moment.m00 != 0)//除数不能为0
{
pt1.x = cvRound(moment.m10 / moment.m00);//计算重心横坐标
pt1.y = cvRound(moment.m01 / moment.m00);//计算重心纵坐标
}
vector< vector
vector
findContours(thresh_img, contours_img, hierarchy1, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
double min_value = 0.5;//最小分值
for (int i = 0; i < contours_img.size(); i++)
{
double value = matchShapes(contours_img[i], contours_template[0], CONTOURS_MATCH_I2, 0.0);
if (value < min_value)
{
//获取中心点
Moments M; //计算多边形或光栅形状的所有三阶矩。
M = moments(contours_img[i]);
double cX = double(M.m10 / M.m00);
double cY = double(M.m01 / M.m00);
drawContours(img, contours_img, i , Scalar(0, 0, 255), 1, 8, hierarchy1, 0); // 表示要绘制的轮廓线的参数。如果它是负的,所有的等高线都画出来。
circle(img, Point2d(cX, cY), 1, Scalar(0, 255, 0), 2, 8);
putText(img, "center", Point2d(cX - 20, cY - 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0), 1, 8);
Point2d c = cal_Rob_Coordinate(Point2d(cX, cY));//这里是我们自己写的一个图像坐标转成机器人坐标的方法
cout << "中心坐标:" << cX << " " << cY << "机器坐标:" << c.x << " " << c.y<< endl << endl; //结果图片输出文字稍有改动
imshow("【匹配图像】", img);
}
}
imshow("【模板图像】", img_template);
waitKey(0);
首先说说OpenCvSharp4的安装:项目——管理NetGet程序包——搜索OpenCvSharp——安装OpenCvSharp4 和 OpenCvSharp4.runtime.win
Point[][] contours_img, contours_template;
HierarchyIndex[] hierarchy;
Cv2.FindContours(erode_img_template, out contours_template, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone, new Point());
for (int i = 0; i < contours_template.Count(); i++)
{
Cv2.DrawContours(img_template, contours_template, i, new Scalar(0, 0, 255), 1, LineTypes.Link8, hierarchy);
}
HierarchyIndex[] hierarchy1;
Cv2.FindContours(thresh_img, out contours_img, out hierarchy1, RetrievalModes.External, ContourApproximationModes.ApproxNone, new Point());
double min_value = 0.5;
for (int i = 0; i < contours_img.Count(); i++)
{
double value = Cv2.MatchShapes(contours_img[i], contours_template[0], ShapeMatchModes.I2, 0.0);
if (value < min_value)
{
//min_pos = i;
//获取中心点
Moments M;
M = Cv2.Moments(contours_img[i]);
double cX = (M.M10 / M.M00);
double cY = (M.M01 / M.M00);
Cv2.DrawContours(img, contours_img, i, new Scalar(0, 0, 255), 1, LineTypes.Link8, hierarchy1, 0);
Cv2.Circle(img, (int)cX, (int)cY, 1, new Scalar(0, 255, 0), 2, LineTypes.Link8);
Cv2.PutText(img, "center", new Point(cX - 20, cY - 20), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 255, 0), 1, LineTypes.Link8);
Point2d c = cal_Rob_Coordinate(new Point2d(cX, cY));
Cv2.ImShow("【匹配图像】", img);
}
}
由于OpenCvSharp没有Vector类或者说有Vector也不兼容
那么用 Point[][] 来代替 vector< vector