How to implement C++ OpenCV Marker Point Detection
This article mainly introduces the relevant knowledge of "how to achieve C++ OpenCV marker detection". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve C++ OpenCV marker detection" can help you solve the problem.
The effect is as follows:
1. Import the original image:
two。 Intercept ROI
3. Adaptive thresholding and Canny Edge extraction
4. The closed operation is carried out, then the contour is detected, the area of the point set is calculated, and the miscellaneous points are removed by the area threshold.
5. Detect the contour again and fit the ellipse
The code is as follows:
# include # define scale 2 define cannythreshold 80typedef struct / image scaling factor # define cannythreshold 80typedef 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 () / / get the point ROI_Struct.ROIImage = src (ROIrect) in the upper left corner of the ROI region; if (flag = = true) {cv::imshow ("ROI", ROI_Struct.ROIImage);} return ROI_Struct;} int main () {cv::Mat srcImage = cv::imread ("7.jpg") / / read cv::resize (srcImage, srcImage, cv::Size (srcImage.cols / scale, srcImage.rows / scale)); / / zoom the image, otherwise the original image will display ROIStruct ROI = getROI (srcImage) in ROI; / / Select ROI region cv::Mat DetectImage, thresholdImage; ROI.ROIImage.copyTo (DetectImage); cv::imshow ("ROI", DetectImage) Cv::cvtColor (DetectImage, thresholdImage, CV_RGB2GRAY); cv::adaptiveThreshold (thresholdImage, thresholdImage, 255,255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,11,35); / / Adaptive threshold cv::Canny (thresholdImage, thresholdImage, cannythreshold, cannythreshold * 3,3); cv::imshow ("thresholdImage", thresholdImage); std::vector contours1; std::vector hierarchy1 Cv::Mat element = cv::getStructuringElement (cv::MORPH_ELLIPSE, cv::Size (3,3)); cv::morphologyEx (thresholdImage, thresholdImage, cv::MORPH_CLOSE, element,cv::Point (- 1jjimi 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