您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“C++ OpenCV标记点检测怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++ OpenCV标记点检测怎么实现”文章能帮助大家解决问题。
效果如下:
1.导入原图:
2.截取ROI
3.进行自适应阈值化与Canny边缘提取
4.进行闭运算,然后轮廓检测,然后计算点集面积,通过面积阈值去除杂点
5.再次进行轮廓检测,拟合椭圆
代码如下:
#include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include <opencv2\core\core.hpp> #include <iostream> #define scale 2//图像缩放因子 #define cannythreshold 80 typedef struct _ROIStruct { cv::Point2d ROIPoint; cv::Mat ROIImage; }ROIStruct; ROIStruct getROI(cv::Mat src,bool flag = false) { ROIStruct ROI_Struct; cv::Rect2d ROIrect = selectROI(src); ROI_Struct.ROIPoint = ROIrect.tl();//获取ROI区域左上角的点 ROI_Struct.ROIImage = src(ROIrect); if (flag == true) { cv::imshow("ROI", ROI_Struct.ROIImage); } return ROI_Struct; } int main() { cv::Mat srcImage = cv::imread("7.jpg");//读取待处理的图片 cv::resize(srcImage, srcImage, cv::Size(srcImage.cols / scale, srcImage.rows / scale));//图像缩放,否则原来图像会在ROI时显示不下 ROIStruct ROI = getROI(srcImage);//选择ROI区域 cv::Mat DetectImage, thresholdImage; ROI.ROIImage.copyTo(DetectImage); cv::imshow("ROI", DetectImage); cv::cvtColor(DetectImage, thresholdImage, CV_RGB2GRAY); cv::adaptiveThreshold(thresholdImage, thresholdImage, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,11,35);//自适应阈值 cv::Canny(thresholdImage, thresholdImage, cannythreshold, cannythreshold * 3, 3); cv::imshow("thresholdImage", thresholdImage); std::vector<std::vector<cv::Point>> contours1; std::vector<cv::Vec4i> hierarchy1; cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3, 3)); cv::morphologyEx(thresholdImage, thresholdImage, cv::MORPH_CLOSE, element,cv::Point(-1,-1),2); cv::Mat findImage = cv::Mat::zeros(thresholdImage.size(), CV_8UC3); cv::findContours(thresholdImage, contours1, hierarchy1,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); for (int i = 0; i <contours1.size();i++) { double area = cv::contourArea(contours1[i]); //std::cout << i << "点集区域面积:" << area << std::endl; if (area < 120) { continue; } else { drawContours(findImage, contours1, i, cv::Scalar(255, 255, 255), -1, 8, cv::Mat(), 0, cv::Point()); } } cv::imshow("drawing", findImage); cv::Mat CircleImage(findImage.size(),CV_8UC1); findImage.copyTo(CircleImage); cv::cvtColor(CircleImage, CircleImage, CV_RGB2GRAY); std::vector<std::vector<cv::Point>> contours2; std::vector<cv::Vec4i> hierarchy2; cv::Mat resultImage(CircleImage.size(), CV_8UC3); cv::findContours(CircleImage, contours2, hierarchy2, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE); std::vector<cv::Point> points1, points2; cv::Mat compareImage; DetectImage.copyTo(compareImage); for (int j = 0; j <contours2.size();j++) { cv::RotatedRect box = cv::fitEllipse(contours2[j]); points1.push_back(box.center); ellipse(resultImage, box, cv::Scalar(0, 0, 255), 1, CV_AA); ellipse(compareImage, box, cv::Scalar(0, 0, 255), 1, CV_AA); } for (int i = 0; i < points1.size(); i++) { cv::Point ans; ans.x = ROI.ROIPoint.x + points1[i].x; ans.x = ans.x*scale; ans.y = ROI.ROIPoint.y + points1[i].y; ans.y = ans.y*scale; points2.push_back(ans); } std::cout << points1 << std::endl; std::cout << ROI.ROIPoint << std::endl; std::cout << points2 << std::endl; cv::imshow("resultImage", resultImage); cv::imshow("compareImage", compareImage); cv::waitKey(0); return 0; }
关于“C++ OpenCV标记点检测怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。