利用opencv进行图像处理,提取椭圆圆心处理

利用opencv进行图像处理,提取椭圆圆心处理

写这个是因为项目正好在做这个,所以简单写写提取椭圆圆心坐标的代码,用的软件是VS。
首先介绍一下步骤,直接从图像处理开始
1,二值化处理(threhold())
2,高斯滤波(GaussianBlur())
3,轮廓提取(canny算子)
4,寻找闭合轮廓(findContours())

上面介绍的是提取椭圆轮廓过程,下面介绍的是过滤干扰图像的过程,分三步,不过,这三部也不都是必须的,看个人需求:
1,像素点数量过滤(size)
2,面积过滤
3,长宽比过滤
代码如下,比较简单,就不过多介绍了,欢迎留言:

#include<iostream>
#include"opencv.hpp"

using namespace std;
using namespace cv;


void drawCross(Mat &img, Point2f point, Scalar color, int size, int thickness /*= 1*/)
{
	//绘制横线  
	line(img, cvPoint(point.x - size / 2, point.y), cvPoint(point.x + size / 2, point.y), color, thickness, 8, 0);
	//绘制竖线  	
	line(img, cvPoint(point.x, point.y - size / 2), cvPoint(point.x, point.y + size / 2), color, thickness, 8, 0);
	return;
}
 

//提取单幅图像的特征点
void FeaturePoint(Mat &img)
{
	Point2f center; //定义变量
	vector<Point2f> ellipsecenterleft;
	Mat edges;
	threshold(img, img, 80, 255, CV_THRESH_BINARY_INV);
	//imshow("threshold", img);
	此函数等待按键,按键盘任意键就返回
	//waitKey(0);
	GaussianBlur(img, edges, Size(5, 5), 0, 0);
	//imshow("GaussianBlur", edges);
	此函数等待按键,按键盘任意键就返回
	//waitKey(0);
	Canny(edges, edges, 40, 120, 3);
	//imshow("Canny", edges);
	此函数等待按键,按键盘任意键就返回
	//waitKey(0);
	vector<vector<Point> > contours;// 创建容器,存储轮廓
	vector<Vec4i> hierarchy;// 寻找轮廓所需参数


	findContours(edges, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
	//cout << "conours size in "<<serialNumber <<" :" << contours.size() << endl;

	//if (contours.size() == 1)
	for (int i = 0; i < contours.size();i++) 
	{
		RotatedRect m_ellipsetemp;  // fitEllipse返回值的数据类型
    	if (contours[i].size() <= 200){
			continue;
		}
		if (contourArea(contours[i]) < 100 && contourArea(contours[i]) > 1000)
		{
			continue;
		}
		m_ellipsetemp = fitEllipse(contours[i]);  //找到的第一个轮廓,放置到m_ellipsetemp


		ellipse(img, m_ellipsetemp, cv::Scalar(255,0,0));   //在图像中绘制椭圆

		if (m_ellipsetemp.size.width / m_ellipsetemp.size.height <0.3)
		{
			continue;
		}
		center = m_ellipsetemp.center;//读取椭圆中心

		drawCross(img, center, Scalar(255,0,0), 30, 2);
		cout << center.x << ends << center.y << endl;
	}
	
	/*imshow("image", img);
	waitKey(0);*/
	//return center;//返回椭圆中心坐标
	
}



int main(int argc, char* argv[])
{
	const char* imagename = "pixmap1.png";

	//从文件中读入图像
	Mat img = imread(imagename, 1);

	//如果读入图像失败
	if (img.empty())
	{
		fprintf(stderr, "Can not load image %s\n", imagename);
		return -1;
	}
	FeaturePoint(img);
	//显示图像
	imshow("image", img);

	//此函数等待按键,按键盘任意键就返回
	waitKey();
	return 0;
}
  • 8
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值