opencv——拟合圆
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"
#include <iostream>
#include"Timer.h"
using namespace std;
int otsu2 (IplImage *image);
CvBox2D findRectContours(const IplImage *gray);
void main()
{ IplImage* img =cvLoadImage("mark1.jpg",);
// cvCopyImage(srcImgGrey,img0tsu);
MyTimer mt;
mt.Reset();
mt.Start();
//Sleep(1000); int thre2;
thre2 = otsu2(img);
//cout<<"The Threshold of this Image in Otsu is:"<<thre2<<endl;//输出显示阀值
cvThreshold(img,img,thre2,,CV_THRESH_BINARY); // 二值化 // CvMemStorage * storage = cvCreateMemStorage(0);
// CvSeq * contour = 0; //cvFindContours(img,storage,&contour,sizeof(CvContour),1,2);
CvBox2D box=findRectContours(img);
mt.End(); cout<<box.center.x<<endl<<box.center.y<<endl<<box.size.height<<endl<<box.size.width<<endl<<mt.costTime<<endl;
cvDrawCircle(img,cvPoint(box.center.x,box.center.y),,cvScalar(,,),,,);
cvNamedWindow("img", CV_WINDOW_AUTOSIZE );
cvShowImage( "img", img);//显示图像
cvReleaseImage(&img);
cvWaitKey(); } CvBox2D findRectContours(const IplImage *gray)
{ CvBox2D box; CvSeq* firstContour = NULL;
CvMemStorage* storage = cvCreateMemStorage();
IplImage* contourImg = cvCreateImage(cvGetSize(gray), gray->depth, );
cvCopy(gray, contourImg);
cvFindContours(contourImg, storage, &firstContour, sizeof(CvContour), ,);
CvSeq* maxContour = firstContour;
CvSeq* Contour = firstContour;
while(Contour)
{
if(maxContour->total < Contour->total)
{
maxContour = Contour;
}
Contour = Contour->h_next;
} if(maxContour)
{
box = cvFitEllipse2(maxContour);
// CvPoint2D32f cross;
// float radius;
//cvMinEnclosingCircle(maxContour,&cross,&radius);
// cout<<cross.x<<endl<<cross.y<<endl;
//box.center.x=cross.x;box.center.y=cross.y;box.size.width=radius;
}
//cvDrawContours(contourImg,maxContour,cvScalar(0,0,255),cvScalar(0,0,255),1,1,0,cvPoint(0,0));
cvReleaseMemStorage(&storage);
return box;
}
/*======================================================================*/
/* OTSU global thresholding routine */
/*======================================================================*/
int otsu2 (IplImage *image)
{
int w = image->width;
int h = image->height; unsigned char*np; // 图像指针
unsigned char pixel;
int thresholdValue=; // 阈值
int ihist[]; // 图像直方图,256个点 int i, j, k; // various counters
int n, n1, n2, gmin, gmax;
double m1, m2, sum, csum, fmax, sb; // 对直方图置零...
memset(ihist, , sizeof(ihist)); gmin=; gmax=;
// 生成直方图
for (i =; i < h; i++)
{
np = (unsigned char*)(image->imageData + image->widthStep*i);
for (j =; j < w; j++)
{
pixel = np[j];
ihist[ pixel]++;
if(pixel > gmax) gmax= pixel;
if(pixel < gmin) gmin= pixel;
}
} // set up everything
sum = csum =0.0;
n =; for (k =; k <=; k++)
{
sum += k * ihist[k]; /* x*f(x) 质量矩*/
n += ihist[k]; /* f(x) 质量 */
} if (!n)
{
// if n has no value, there is problems...
//fprintf (stderr, "NOT NORMAL thresholdValue = 160\n");
thresholdValue =;
goto L;
} // do the otsu global thresholding method
fmax =-1.0;
n1 =;
for (k =; k <; k++)
{
n1 += ihist[k];
if (!n1) { continue; }
n2 = n - n1;
if (n2 ==) { break; }
csum += k *ihist[k];
m1 = csum / n1;
m2 = (sum - csum) / n2;
sb = n1 * n2 *(m1 - m2) * (m1 - m2);
/* bbg: note: can be optimized. */
if (sb > fmax)
{
fmax = sb;
thresholdValue = k;
}
} L:
for (i =; i < h; i++)
{
np = (unsigned char*)(image->imageData + image->widthStep*i);
for (j =; j < w; j++)
{
if(np[j] >= thresholdValue)
np[j] =;
else np[j] =;
}
} //cout<<"The Threshold of this Image in Otsu is:"<<thresholdValue<<endl;
return(thresholdValue);
}
opencv——拟合圆的更多相关文章
- (转)最小二乘法拟合圆公式推导及vc实现[r]
(下文内容为转载,不过已经不清楚原创的是哪里了,特此说明) 转自: http://www.cnblogs.com/dotLive/archive/2006/10/09/524633.html 该网址下 ...
- .net core(c#)拟合圆测试
说明 很多时候,我们需要运动物体的转弯半径去描述其机器性能.但在大多数的现实条件下,我们只能够获取到运动物体的 GPS 位置点集,并不能直接得到转弯半径或者圆心位置.为此,我们可以利用拟合圆的方式得到 ...
- [opencv]拟合vector<Mat>集合区域接近的元素
vector<Rect> PublicCardFrameDetection::fitrect(vector<Rect> rects){ int size = rects.siz ...
- opencv:轮廓逼近与拟合
轮廓逼近,本质上是减少编码点 拟合圆,生成最相似的圆或椭圆 #include <opencv2/opencv.hpp> #include <iostream> using na ...
- Python+OpenCV图像处理(十五)—— 圆检测
简介: 1.霍夫圆变换的基本原理和霍夫线变换原理类似,只是点对应的二维极径.极角空间被三维的圆心和半径空间取代.在标准霍夫圆变换中,原图像的边缘图像的任意点对应的经过这个点的所有可能圆在三维空间用圆心 ...
- 【python+opencv】直线检测+圆检测
Python+OpenCV图像处理—— 直线检测 直线检测理论知识: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进 ...
- C#使用最小二乘法对多个离散点进行圆拟合
/// <summary> /// 最小二乘法拟合圆,计算拟合圆半径和拟合圆圆心 /// </summary> /// <param name="points& ...
- (转载)找圆算法((HoughCircles)总结与优化
Opencv内部提供了一个基于Hough变换理论的找圆算法,HoughCircle与一般的拟合圆算法比起来,各有优势:优势:HoughCircle对噪声点不怎么敏感,并且可以在同一个图中找出多个圆 ...
- 转载-找圆算法((HoughCircles)总结与优化-霍夫变换
原文链接: http://www.opencv.org.cn/forum.php?mod=viewthread&tid=34096 找圆算法((HoughCircles)总结与优化 Ope ...
随机推荐
- java.lang.NoClassDefFoundError: org/jaxen/JaxenException解决方法
在使用dom4j的xpath时出现java.lang.NoClassDefFoundError: org/jaxen/JaxenException的异常,原因是dom4j引用了jaxen jar包,而 ...
- linux 监控系统剩余内存大小
cur_free = `free -m | awk '/buffers\// {print $NF}'` chars="current memory is $cur_free." ...
- oracle用户具有的权限和角色
如何查看一个oracle用户具有的权限和角色 1.查看所有用户: select * from dba_users; select * from all_users; select * from use ...
- 项目通过nginx强转为https访问后,代码中重定向的连接又变成了http协议,导致点击页面按钮,后台逻辑处理完后重定向报错了
修改如下,需要在nginx对应的server下的location中增加配置,使重定向的地址协议取当前链接的协议,而不是nginx访问tomcat的协议,因为nginx访问tomcat是http的,并没 ...
- c++builder 画图 填充
c++builder 画图 填充 void __fastcall TForm2::Button1Click(TObject *Sender) { Canvas->Brush->Color ...
- django html模板继承 {%block 标记名} {%endblock%}
对于url文件 url(r'^tp1/', views.tp1) 对于views文件,跳转到tp1.html 同时将list列表传到前端 def tp1(request): list = [1, 2, ...
- 无法访问win8默认共享(如C$)解决办法
可以使用此过程允许作为本地 Administrators 组的成员并使用密码身份验证登录的用户在会话过程中使用其管理权限.启动注册表编辑器.单击“开始”,在“开始搜索”框中键入 regedit,然后按 ...
- Echarts在java中使用
index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- Spark中RDD的常用操作(Python)
弹性分布式数据集(RDD) Spark是以RDD概念为中心运行的.RDD是一个容错的.可以被并行操作的元素集合.创建一个RDD有两个方法:在你的驱动程序中并行化一个已经存在的集合:从外部存储系统中引用 ...
- Linux实战教学笔记19:Linux相关网络知识梳理
第十九节 Linux相关网络知识梳理 标签(空格分隔): Linux实战教学笔记-陈思齐 一,前言 一个运维有时也要和网络打交道,所以具备最基本的网络知识,对一个运维人员来说是必要的.但,对于我们的工 ...