#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 240;
int max_thresh = 255;
int core = 6;
int max_core = 10;
/// Function header
void thresh_callback(int, void*);
/** @function main */
int main(int argc, char** argv)
{
/// 加载源图像
src = imread("778924580128721738.jpg", 1);
//src = imread("4-0-36-29-90_10.jpg", 1);
//src = imread("4-0-36-29-90_10.jpg", 1);
//src = imread("4-0-36-29-90_10.jpg", 1);
//resize(src, src, Size(512, 512));
/// 转为灰度图并模糊化
cvtColor(src, src_gray, CV_BGR2GRAY);
bitwise_not(src_gray, src_gray);
//blur(src_gray, src_gray, Size(3, 3));
/// 创建窗体
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src);
createTrackbar(" Threshold:", "Source", &thresh, max_thresh, thresh_callback);
createTrackbar(" erode:", "Source", &core, max_core, thresh_callback);
thresh_callback(0, 0);
waitKey(0);
return(0);
}
/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat threshold_output;
vector<vector<Point> > contours;
vector<vector<Point> > maxcontours;
vector<Vec4i> hierarchy;
// 阈值化检测边界
threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);
Mat element = getStructuringElement(2, Size(7, 7)); //膨胀腐蚀核
for (int i = 0;i<2;i++) {
dilate(threshold_output, threshold_output, element);
i++;
}
for (int i = 0;i<core;i++) {
erode(threshold_output, threshold_output, element);
i++;
}
imshow("thresh", threshold_output);
/// 寻找轮廓
findContours(threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
//筛选轮廓
/*for (vector<vector<Point>>::iterator it = contours.begin();it != contours.end();it++) {
if (contourArea(*it)< contourArea(*(it++))) {
}
}*/
/// 对每个找到的轮廓创建可倾斜的边界框和椭圆
vector<RotatedRect> minRect(contours.size());
for (int i = 0; i < contours.size(); i++)
{
minRect[i] = minAreaRect(Mat(contours[i]));
}
/// 绘出轮廓及其可倾斜的边界框和边界椭圆
Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
//rectPoint变量中得到了矩形的四个顶点坐标
//RotatedRect rectPoint = minAreaRect(points);//定义一个存储以上四个点的坐标的变量
// Point2f fourPoint2f[4];
//将rectPoint变量中存储的坐标值放到 fourPoint的数组中
// rectPoint.points(fourPoint2f);
Scalar color = Scalar(255, 0, 0);
//drawContours(drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point());
// rotated rectangle
Point2f center; float radius;
minEnclosingCircle(contours[i], center, radius);
if ((0.25*src.cols < center.x && center.x<0.38*src.cols) && (0.5*src.rows < center.y &¢er.y< 0.8*src.rows)) {
//面积筛选
if (3.1415*(radius + 20)*(radius + 20) < 6500) {
/* cout << src.cols << endl;
cout<< src.rows<<endl;
cout << center.x << endl;
cout << center.x / src.cols << endl;
cout << center.y << endl;
cout << center.y / src.rows << endl;*/
circle(src, center, radius + 20, Scalar(0, 0, 255), 2);
//cout << 3.1415*(radius + 20)*(radius + 20) << endl;
}
}
}
/// 结果在窗体中显示
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("标注", src);
}
相关链接:https://blog.csdn.net/abcvincent/article/details/79275429