1 Star 0 Fork 0

bisheng/CheckPointRectReconnect

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ImageMeasurement.cpp 23.80 KB
一键复制 编辑 原始数据 按行查看 历史
ubuntu16 提交于 2024-03-04 13:41 . 20230304
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
//
// Created by bisheng on 4/18/22.
//
#include "ImageMeasurement.h"
double CalculateCircularity(const vector<Point> &contour)
{
cv::Moments moment = cv::moments(contour);
cv::Point mid_point = cv::Point(int(moment.m10 / moment.m00), int(moment.m01 / moment.m00));
double center_x = mid_point.x;
double center_y = mid_point.y;
double max_distance = 0;
for (int i = 0; i < contour.size(); i++)
{
double distance = sqrt(pow(center_x - contour[i].x, 2) + pow(center_y - contour[i].y, 2));
if (distance > max_distance)
max_distance = distance;
}
double area = contourArea(contour);
double circularity = area / (max_distance * max_distance * M_PI);
circularity = min(1.0, circularity);
return circularity;
}
void ImageMeasurement::GetImage(Mat &image)
{
if (false)
{
// image = imread("image.bmp", IMREAD_COLOR);
// image = imread("measure_image_" + to_string(-cap_id_) + ".bmp", IMREAD_COLOR);
}
else
{
cv::VideoCapture capture(cap_id_);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 3264);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 2448);
capture.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
capture.set(CV_CAP_PROP_FPS, 1);
capture.set(CV_CAP_PROP_AUTOFOCUS, 0);
capture.set(CV_CAP_PROP_BRIGHTNESS, 0.1);
capture.set(CV_CAP_PROP_CONTRAST, 0.8);
sleep(4);
capture.read(image);
}
}
bool ImageMeasurement::GetRectangleCenter(const Mat &src_image, double &pixel_x, double &pixel_y)
{
Size size = Size(800, 600);
Mat result_image;
bool show_process = true;
if (show_process)
{
resize(src_image, result_image, size, 0, 0, INTER_NEAREST);
imshow("result1", result_image);
waitKey(0);
}
pixel_x = 0;
pixel_y = 0;
// 二值化处理
Mat gray_image, threshold_image1;
cvtColor(src_image, gray_image, CV_RGB2GRAY);
threshold(gray_image, threshold_image1, 20, 255, CV_THRESH_BINARY);
if (show_process)
{
resize(threshold_image1, result_image, size, 0, 0, INTER_NEAREST);
imshow("result2", result_image);
waitKey(0);
}
// 闭运算进行闭合
Mat element_closing1 = getStructuringElement(MORPH_ELLIPSE, Size(40, 40));
morphologyEx(threshold_image1, threshold_image1, MORPH_CLOSE, element_closing1);
if (show_process)
{
resize(threshold_image1, result_image, size, 0, 0, INTER_NEAREST);
imshow("result3", result_image);
waitKey(0);
}
// 寻找最大轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(threshold_image1, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat max_contour_image1 = Mat::zeros(threshold_image1.size(), CV_8UC1);
double max_area = 0;
int max_contour = 0;
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > max_area)
{
max_area = contourArea(contours[i]);
max_contour = i;
}
}
if (contours.size() > 0)
{
drawContours(max_contour_image1, contours, max_contour, Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(max_contour_image1, result_image, size, 0, 0, INTER_NEAREST);
imshow("result4", result_image);
waitKey(0);
}
// 闭运算进行闭合
Mat element_closing = getStructuringElement(MORPH_ELLIPSE, Size(20, 20));
Mat closing_image;
morphologyEx(max_contour_image1, closing_image, MORPH_CLOSE, element_closing);
if (show_process)
{
resize(closing_image, result_image, size, 0, 0, INTER_NEAREST);
imshow("result5", result_image);
waitKey(0);
}
// 减小区域
Mat image_reduce1;
Mat reverse_image;
bitwise_not(gray_image, reverse_image);
reverse_image.copyTo(image_reduce1, closing_image);
bitwise_not(image_reduce1, image_reduce1);
if (show_process)
{
resize(image_reduce1, result_image, size, 0, 0, INTER_NEAREST);
imshow("result6", result_image);
waitKey(0);
}
// 动态阈值
Mat mean_image;
blur(gray_image, mean_image, Size(250, 250));
Mat dyn_image = Mat::zeros(mean_image.size(), CV_8UC1);
;
for (int i = 0; i < dyn_image.rows; i++)
{
for (int j = 0; j < dyn_image.cols; j++)
{
if ((gray_image.at<uchar>(i, j) - mean_image.at<uchar>(i, j)) <= -50)
{
if (closing_image.at<uchar>(i, j) == 255)
{
dyn_image.at<uchar>(i, j) = 255;
}
else
{
dyn_image.at<uchar>(i, j) = 0;
}
}
else
{
dyn_image.at<uchar>(i, j) = 0;
}
}
}
if (show_process)
{
resize(dyn_image, result_image, size, 0, 0, INTER_NEAREST);
imshow("result7", result_image);
waitKey(0);
}
// 寻找轮廓面积大于10000的轮廓
contours.clear();
hierarchy.clear();
findContours(dyn_image, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat max_contour_image2 = Mat::zeros(dyn_image.size(), CV_8UC1);
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > 10000)
{
drawContours(max_contour_image2, contours, i, Scalar(255), -1, 8, hierarchy);
}
}
if (show_process)
{
resize(max_contour_image2, result_image, size, 0, 0, INTER_NEAREST);
imshow("result8", result_image);
waitKey(0);
}
// 开运算除去连接部分
// 上边缘窄的话开运算会去掉真实长方形?????????????????????
/*Mat element_opening = getStructuringElement(MORPH_RECT, Size(120, 60));
Mat opening_image;
morphologyEx(max_contour_image2, opening_image, MORPH_OPEN, element_opening);
if (show_process)
{
resize(opening_image, result_image, size, 0, 0, INTER_NEAREST);
imshow("result9", result_image);
waitKey(0);
}
max_contour_image2 = opening_image.clone();*/
// 寻找凸包多边形
contours.clear();
hierarchy.clear();
findContours(max_contour_image2, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
vector<vector<Point>> hull(contours.size());
for (unsigned int i = 0; i < contours.size(); i++)
{
convexHull(Mat(contours[i]), hull[i], false);
}
Mat max_hull_image2 = Mat::zeros(max_contour_image2.size(), CV_8UC1);
for (unsigned int i = 0; i < contours.size(); i++)
{
drawContours(max_hull_image2, hull, i, Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(max_hull_image2, result_image, size, 0, 0, INTER_NEAREST);
imshow("result10", result_image);
waitKey(0);
}
// 寻找(面积/最小外接矩形面积)>0.85
contours.clear();
hierarchy.clear();
findContours(max_hull_image2, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat max_contour_image3 = Mat::zeros(dyn_image.size(), CV_8UC1);
Mat draw_image = src_image.clone();
for (int i = 0; i < contours.size(); i++)
{
RotatedRect rotate_rect = minAreaRect(contours[i]);
double min_rect_area = rotate_rect.boundingRect().area();
double contour_area = contourArea(contours[i]);
if (contour_area / min_rect_area > 0.85)
{
drawContours(max_contour_image3, contours, i, Scalar(255), -1, 8, hierarchy);
drawContours(draw_image, contours, i, Scalar(255), 1, 8, hierarchy);
}
}
if (show_process)
{
resize(max_contour_image3, result_image, size, 0, 0, INTER_NEAREST);
imshow("result11", result_image);
// imwrite("image.bmp", draw_image);
waitKey(0);
}
// 寻找圆度小于0.6的轮廓
contours.clear();
hierarchy.clear();
findContours(max_contour_image3, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat rect_contour_image4 = Mat::zeros(max_contour_image3.size(), CV_8UC1);
for (int i = 0; i < contours.size(); i++)
{
double circularity = CalculateCircularity(contours[i]);
if (circularity < 0.6)
{
drawContours(rect_contour_image4, contours, i, Scalar(255), -1, 8, hierarchy);
}
}
if (show_process)
{
resize(rect_contour_image4, result_image, size, 0, 0, INTER_NEAREST);
imshow("result12", result_image);
waitKey(0);
}
// 寻找长宽比小于3的矩形
contours.clear();
hierarchy.clear();
findContours(rect_contour_image4, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat rect_contour_image5 = Mat::zeros(rect_contour_image4.size(), CV_8UC1);
for (int i = 0; i < contours.size(); i++)
{
RotatedRect rotate_rect = minAreaRect(contours[i]);
double length1 = rotate_rect.boundingRect().height;
double length2 = rotate_rect.boundingRect().width;
if (length1 < length2)
{
double tem = length1;
length1 = length2;
length2 = tem;
}
cout << "length" << length1 << "," << length2 << endl;
if (length1 * 1.0 / length2 < 3)
{
drawContours(rect_contour_image5, contours, i, Scalar(255), -1, 8, hierarchy);
}
}
if (show_process)
{
resize(rect_contour_image5, result_image, size, 0, 0, INTER_NEAREST);
imshow("result13", result_image);
waitKey(0);
}
// 寻找面积灰度积最小的矩形
/*contours.clear();
hierarchy.clear();
findContours(rect_contour_image5, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat rect_contour_image6 = Mat::zeros(rect_contour_image4.size(), CV_8UC1);
double min_gray_area = rect_contour_image5.rows * rect_contour_image5.cols * 255;
double min_gray_area_index = 0;
for (int i = 0; i < contours.size(); i++)
{
Mat rect_contour_image = Mat::zeros(rect_contour_image4.size(), CV_8UC1);
drawContours(rect_contour_image, contours, i, Scalar(255), -1, 8, hierarchy);
double area = contourArea(contours[i]);
int gray_num = 0;
int gray_total = 0;
double mean_gray;
for (int i = 0; i < dyn_image.rows; i++)
{
for (int j = 0; j < dyn_image.cols; j++)
{
if (rect_contour_image.at<uchar>(i, j) == 255)
{
gray_num++;
gray_total += gray_image.at<uchar>(i, j);
}
}
}
mean_gray = gray_total * 1.0 / gray_num;
if (mean_gray * area < min_gray_area)
{
min_gray_area = mean_gray * area;
min_gray_area_index = i;
}
}
Mat rect_contour_image = Mat::zeros(gray_image.size(), CV_8UC1);
if (contours.size() > 0)
{
drawContours(rect_contour_image, contours, min_gray_area_index, Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(rect_contour_image, result_image, size, 0, 0, INTER_NEAREST);
imshow("result14", result_image);
waitKey(0);
}*/
//  寻找质心y值最小的
contours.clear();
hierarchy.clear();
Mat rect_contour_image = Mat::zeros(gray_image.size(), CV_8UC1);
findContours(rect_contour_image5, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
double min_y_index = 0;
double min_y = INT32_MAX;
for (int i = 0; i < contours.size(); i++)
{
cv::Moments moment = cv::moments(contours.at(i));
if (moment.m01 / moment.m00 < min_y)
{
min_y_index = i;
min_y = moment.m01 / moment.m00;
}
}
if (contours.size() > 0)
{
drawContours(rect_contour_image, contours, min_y_index, Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(rect_contour_image, result_image, size, 0, 0, INTER_NEAREST);
imshow("result14", result_image);
waitKey(0);
}
if (contours.size() >= 1)
{
cv::Moments moment = cv::moments(contours.at(min_y_index));
cv::Point mid_point = cv::Point(int(moment.m10 / moment.m00), int(moment.m01 / moment.m00));
pixel_x = mid_point.x;
pixel_y = mid_point.y;
std::cout << "ppp" << pixel_x << "," << pixel_y << std::endl;
return true;
}
else
{
return false;
}
}
bool ImageMeasurement::GetRadiusCenter(const Mat &src_image, double &pixel_x, double &pixel_y)
{
Size size = Size(800, 600);
Mat result_image;
bool show_process = true;
if (show_process)
{
resize(src_image, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
pixel_x = 0;
pixel_y = 0;
// 二值化处理
Mat gray_image, threshold_image1;
cvtColor(src_image, gray_image, CV_RGB2GRAY);
threshold(gray_image, threshold_image1, 80, 255, CV_THRESH_BINARY);
if (show_process)
{
resize(threshold_image1, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 寻找最大轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(threshold_image1, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat max_contour_image1 = Mat::zeros(threshold_image1.size(), CV_8UC1);
double max_area = 0;
int max_contour = 0;
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > max_area)
{
max_area = contourArea(contours[i]);
max_contour = i;
}
}
if (contours.size() > 0)
{
drawContours(max_contour_image1, contours, max_contour, Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(max_contour_image1, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 闭运算进行闭合
Mat element_closing = getStructuringElement(MORPH_RECT, Size(5, 200));
Mat closing_image;
morphologyEx(max_contour_image1, closing_image, MORPH_CLOSE, element_closing);
if (show_process)
{
resize(closing_image, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 减小区域
Mat image_reduce1;
Mat reverse_image;
bitwise_not(gray_image, reverse_image);
reverse_image.copyTo(image_reduce1, closing_image);
bitwise_not(image_reduce1, image_reduce1);
if (show_process)
{
resize(image_reduce1, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 增强图像
Mat scale_image = image_reduce1 * 2.55;
if (show_process)
{
resize(scale_image, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 二值化处理
Mat threshold_image2;
threshold(scale_image, threshold_image2, 60, 255, CV_THRESH_BINARY_INV);
if (show_process)
{
resize(threshold_image2, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 查找轮廓并且计算圆度
contours.clear();
hierarchy.clear();
vector<int> circle_contours_ids;
Mat circle_contour_image1 = Mat::zeros(threshold_image2.size(), CV_8UC1);
findContours(threshold_image2, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
for (int i = 0; i < contours.size(); i++)
{
double circularity = CalculateCircularity(contours[i]);
if (circularity > 0.6)
circle_contours_ids.push_back(i);
}
for (int i = 0; i < circle_contours_ids.size(); i++)
{
drawContours(circle_contour_image1, contours, circle_contours_ids[i], Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(circle_contour_image1, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 开运算剔除小区域
Mat element_opening = getStructuringElement(MORPH_RECT, Size(6, 6));
Mat opening_image;
morphologyEx(circle_contour_image1, opening_image, MORPH_OPEN, element_opening);
if (show_process)
{
resize(opening_image, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 膨胀进行二次筛选
Mat element_dilation = getStructuringElement(MORPH_RECT, Size(60, 60));
Mat dilation_image;
morphologyEx(opening_image, dilation_image, MORPH_DILATE, element_dilation);
if (show_process)
{
resize(dilation_image, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
Mat image_reduce2;
Mat reverse_image1;
bitwise_not(gray_image, reverse_image1);
reverse_image1.copyTo(image_reduce2, dilation_image);
if (show_process)
{
resize(image_reduce2, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
Mat image_reduce3;
bitwise_not(image_reduce2, image_reduce3);
Mat threshold_image3;
threshold(image_reduce3, threshold_image3, 20, 255, CV_THRESH_BINARY_INV);
if (show_process)
{
resize(threshold_image3, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 查找轮廓并且计算圆度
contours.clear();
hierarchy.clear();
circle_contours_ids.clear();
Mat circle_contour_image2 = Mat::zeros(threshold_image3.size(), CV_8UC1);
findContours(threshold_image3, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
for (int i = 0; i < contours.size(); i++)
{
double circularity = CalculateCircularity(contours[i]);
if (circularity > 0.6)
{
circle_contours_ids.push_back(i);
cout << "圆度:" << circularity << endl;
}
}
for (int i = 0; i < circle_contours_ids.size(); i++)
{
drawContours(circle_contour_image2, contours, circle_contours_ids[i], Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(circle_contour_image2, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 寻找最大轮廓
contours.clear();
hierarchy.clear();
findContours(circle_contour_image2, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat max_contour_image2 = Mat::zeros(threshold_image1.size(), CV_8UC1);
max_area = 0;
max_contour = 0;
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > max_area)
{
max_area = contourArea(contours[i]);
max_contour = i;
}
}
if (contours.size() > 0)
{
drawContours(max_contour_image2, contours, max_contour, Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(max_contour_image2, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 开运算剔除小区域
element_opening = getStructuringElement(MORPH_RECT, Size(6, 6));
Mat opening_image2;
morphologyEx(max_contour_image2, opening_image2, MORPH_OPEN, element_opening);
if (show_process)
{
resize(opening_image2, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
// 寻找最大轮廓
contours.clear();
hierarchy.clear();
findContours(opening_image2, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
Mat max_contour_image3 = Mat::zeros(opening_image2.size(), CV_8UC1);
max_area = 0;
max_contour = 0;
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > max_area)
{
max_area = contourArea(contours[i]);
max_contour = i;
}
}
if (contours.size() > 0)
{
drawContours(max_contour_image3, contours, max_contour, Scalar(255), -1, 8, hierarchy);
}
if (show_process)
{
resize(max_contour_image3, result_image, size, 0, 0, INTER_LINEAR);
imshow("result", result_image);
waitKey(0);
}
if (contours.size() > 0)
{
cv::Moments moment = cv::moments(contours.at(max_contour));
cv::Point mid_point = cv::Point(int(moment.m10 / moment.m00), int(moment.m01 / moment.m00));
pixel_x = mid_point.x;
pixel_y = mid_point.y;
return true;
}
else
{
return false;
}
}
bool ImageMeasurement::SetTemplateImage()
{
GetImage(template_image_);
imwrite("template_image_.bmp", template_image_);
bool result = GetRectangleCenter(template_image_, template_radius_x_, template_radius_y_);
for (int i = 0; i < 3; i++)
{
if (result == false)
{
cout << "Error: 找不到孔" << endl;
GetImage(template_image_);
result = GetRectangleCenter(template_image_, template_radius_x_, template_radius_y_);
}
else
{
cout << "成功找到孔^-^";
cout << "坐标:" << template_radius_x_ << "," << template_radius_y_ << endl;
break;
}
}
circle(template_image_, cv::Point(template_radius_x_, template_radius_y_), 20, cv::Scalar(0, 0, 255), -1);
imwrite("template_image_check.bmp", template_image_);
return result;
}
bool ImageMeasurement::SetCalibrationImage()
{
GetImage(calibration_image_);
imwrite("calibration_image_.bmp", calibration_image_);
bool result = GetRectangleCenter(calibration_image_, calibration_radius_x_, calibration_radius_y_);
for (int i = 0; i < 3; i++)
{
if (result == false)
{
cout << "Error: 找不到孔" << endl;
GetImage(calibration_image_);
result = GetRectangleCenter(calibration_image_, calibration_radius_x_, calibration_radius_y_);
}
else
{
cout << "成功找到孔^-^";
cout << "坐标:" << calibration_radius_x_ << "," << calibration_radius_y_ << endl;
break;
}
}
circle(calibration_image_, cv::Point(calibration_radius_x_, calibration_radius_y_), 20, cv::Scalar(0, 0, 255), -1);
imwrite("calibration_image_check.bmp", calibration_image_);
pulse_pixel_x_ = abs(calibration_pulse_ * 1.0 / (template_radius_x_ - calibration_radius_x_));
pulse_pixel_y_ = abs(calibration_pulse_ * 1.0 / (template_radius_y_ - calibration_radius_y_));
cout << "标定脉冲数:" << calibration_pulse_ << endl;
cout << "脉冲像素比x:" << pulse_pixel_x_ << endl;
cout << "脉冲像素比y:" << pulse_pixel_y_ << endl;
return result;
}
bool ImageMeasurement::SetMeasureImage()
{
GetImage(measure_image_);
// imwrite("measure_image_" + to_string(measure_times_) + ".bmp", measure_image_);
measure_times_++;
bool result = GetRectangleCenter(measure_image_, measure_radius_x_, measure_radius_y_);
for (int i = 0; i < 3; i++)
{
if (result == false)
{
cout << "Error: 找不到孔" << endl;
GetImage(measure_image_);
result = GetRectangleCenter(measure_image_, measure_radius_x_, measure_radius_y_);
}
else
{
cout << "成功找到孔^-^";
cout << "坐标:" << measure_radius_x_ << "," << measure_radius_y_ << endl;
break;
}
}
circle(measure_image_, cv::Point(measure_radius_x_, measure_radius_y_), 20, cv::Scalar(0, 0, 255), -1);
imwrite("measure_image_check" + to_string(measure_times_ - 1) + ".bmp", measure_image_);
return result;
}
void ImageMeasurement::GetPulse(int &pulse_x, int &pulse_y)
{
pulse_x = pulse_pixel_x_ * (template_radius_x_ - measure_radius_x_);
pulse_y = pulse_pixel_y_ * (template_radius_y_ - measure_radius_y_);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bishengsjtu/check-point-rect-reconnect.git
git@gitee.com:bishengsjtu/check-point-rect-reconnect.git
bishengsjtu
check-point-rect-reconnect
CheckPointRectReconnect
master

搜索帮助