一、最小外接圆
#include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv;
void main() {
Mat srcImg = imread("E://10.png");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
GaussianBlur(srcImg, srcImg, Size(, ), , );
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
Canny(srcImg, srcImg, , );
imshow("Canny", srcImg);
//查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
cout << "num=" << contours.size() << endl;
Point2f center; //定义圆中心坐标
float radius; //定义圆半径
for (int i = ; i<contours.size(); i++) //依次遍历每个轮廓
{
minEnclosingCircle(Mat(contours[i]), center, radius);
drawContours(dstImg, contours, i, Scalar(, , ), , );
circle(dstImg, center, radius, Scalar(, , ), , ); //绘制第i个轮廓的最小外接圆
}
imshow("dst", dstImg);
waitKey();
}
二、椭圆拟合
#include "opencv2/opencv.hpp"
using namespace cv;
void main() {
Mat srcImg = imread("E://10.png");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
GaussianBlur(srcImg, srcImg, Size(, ), , );
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
Canny(srcImg, srcImg, , );
imshow("Canny", srcImg);
//查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
vector<RotatedRect> box(contours.size());
Point2f rect[];
for (int i = ; i<contours.size(); i++)
{
box[i] = fitEllipse(Mat(contours[i]));
//ellipse(dstImg, box[i].center, Size(box[i].size.width/2, box[i].size.height/2), box[i].angle, 0, 360, Scalar(0, 255, 0), 2, 8);
ellipse(dstImg, box[i], Scalar(, , ), , );
}
imshow("dst", dstImg);
waitKey();
}
三、逼近多边形曲线
#include "opencv2/opencv.hpp"
using namespace cv;
void main() {
Mat srcImg = imread("E://02.jpg");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
Mat dstImg2(srcImg.size(), CV_8UC3, Scalar::all());//全黑图像
GaussianBlur(srcImg, srcImg, Size(, ), , );
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
//Canny(srcImg, srcImg, 100, 200);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY_INV);
imshow("threshold", srcImg);
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
vector<vector<Point>> contours_poly(contours.size());
for (int i = ; i<contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i],, true);//true曲线封闭,反之不封闭
drawContours(dstImg, contours, i, Scalar(, , ), , );//绘制轮廓
drawContours(dstImg2, contours_poly, i, Scalar(, , ), , ); //绘制多边形逼近
}
imshow("dst", dstImg);
imshow("approx", dstImg2);
waitKey();
}
四、计算轮廓面积及长度(可用于轮廓筛选)
#include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv;
void main() {
Mat srcImg = imread("E://33.jpg");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
Mat dstImg2(srcImg.size(), CV_8UC3, Scalar::all());
GaussianBlur(srcImg, srcImg, Size(, ), , );
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
//Canny(srcImg, srcImg, 100, 200);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY);
imshow("threshold", srcImg);
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
cout << "num=" << contours.size() << endl;
for (int i = ; i<contours.size(); i++)
{
double area = contourArea(contours[i]);//计算第i个轮廓的面积
cout<<"area--"<<i<<"---"<<area<<endl;
double length = arcLength(contours[i], true);
cout << "length--" << i << "---" << length << endl;
//if(area>10000) //面积大约1W
//if(area> 100 && area<300)
if (length< && area>)
drawContours(dstImg, contours, i, Scalar(, , ), , );
}
imshow("dst", dstImg);
waitKey();
}
五、提取不规则轮廓
#include "opencv2/opencv.hpp"
using namespace cv;
void main() {
Mat srcImg = imread("E://220.jpg");
imshow("src", srcImg);
Mat dstImg = srcImg.clone(); //原图备份
Mat tempImg = srcImg.clone(); //原图备份
Mat tempImg2(srcImg.size(), CV_8UC3, Scalar::all()); //定义全黑的和原图一样大小的图像
Mat draw(srcImg.size(), CV_8UC3, Scalar::all());
Mat tempImg3(srcImg.size(), CV_8UC3, Scalar::all());
GaussianBlur(srcImg, srcImg, Size(, ), , );
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY); //二值化
imshow("threshold", srcImg);
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
while ()
{
for (int i = ; i<contours.size(); i++)
{
tempImg2.copyTo(draw); //每次进入将draw清空为全黑
tempImg2.copyTo(tempImg3);
//drawContours(dstImg, contours, i, Scalar(0, 255, 0), 5, 8);
drawContours(draw, contours, i, Scalar(, , ), -, );
Mat mask; //定义掩码
cvtColor(draw, mask, CV_BGR2GRAY);
tempImg.copyTo(tempImg3, mask); //将tempImg 复制到tempImg3(只有mask部分被复制)
imshow("draw", draw);
imshow("result", tempImg3);
char key = waitKey();
if (key == ) //按下Esc键跳出for循环
break;
}
break;
}
}