您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++ OpenCV標記點檢測怎么實現(xiàn)”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“C++ OpenCV標記點檢測怎么實現(xiàn)”文章能幫助大家解決問題。
效果如下:
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區(qū)域左上角的點 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區(qū)域 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 << "點集區(qū)域面積:" << 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; }
關(guān)于“C++ OpenCV標記點檢測怎么實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。