#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——拟合圆的更多相关文章

  1. (转)最小二乘法拟合圆公式推导及vc实现[r]

    (下文内容为转载,不过已经不清楚原创的是哪里了,特此说明) 转自: http://www.cnblogs.com/dotLive/archive/2006/10/09/524633.html 该网址下 ...

  2. .net core(c#)拟合圆测试

    说明 很多时候,我们需要运动物体的转弯半径去描述其机器性能.但在大多数的现实条件下,我们只能够获取到运动物体的 GPS 位置点集,并不能直接得到转弯半径或者圆心位置.为此,我们可以利用拟合圆的方式得到 ...

  3. [opencv]拟合vector<Mat>集合区域接近的元素

    vector<Rect> PublicCardFrameDetection::fitrect(vector<Rect> rects){ int size = rects.siz ...

  4. opencv:轮廓逼近与拟合

    轮廓逼近,本质上是减少编码点 拟合圆,生成最相似的圆或椭圆 #include <opencv2/opencv.hpp> #include <iostream> using na ...

  5. Python+OpenCV图像处理(十五)—— 圆检测

    简介: 1.霍夫圆变换的基本原理和霍夫线变换原理类似,只是点对应的二维极径.极角空间被三维的圆心和半径空间取代.在标准霍夫圆变换中,原图像的边缘图像的任意点对应的经过这个点的所有可能圆在三维空间用圆心 ...

  6. 【python+opencv】直线检测+圆检测

     Python+OpenCV图像处理—— 直线检测 直线检测理论知识: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进 ...

  7. C#使用最小二乘法对多个离散点进行圆拟合

    /// <summary> /// 最小二乘法拟合圆,计算拟合圆半径和拟合圆圆心 /// </summary> /// <param name="points& ...

  8. (转载)找圆算法((HoughCircles)总结与优化

      Opencv内部提供了一个基于Hough变换理论的找圆算法,HoughCircle与一般的拟合圆算法比起来,各有优势:优势:HoughCircle对噪声点不怎么敏感,并且可以在同一个图中找出多个圆 ...

  9. 转载-找圆算法((HoughCircles)总结与优化-霍夫变换

    原文链接: http://www.opencv.org.cn/forum.php?mod=viewthread&tid=34096   找圆算法((HoughCircles)总结与优化 Ope ...

随机推荐

  1. python json.dumps() 中文乱码问题

    python json.dumps() 中文乱码问题   python 输出一串中文字符,在控制台上(控制台使用UTF-8编码)通过print 可以正常显示,但是写入到文件中之后,中文字符都输出成as ...

  2. mysql数据库忘记密码时如何登录

    1.打开cmd命令提示符,进入上一步mysql.exe所在的文件夹即: 2.输入命令  mysqld --skip-grant-tables  回车,此时就跳过了mysql的用户验证 3.然后直接输入 ...

  3. day9-Memcached & Redis使用

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  4. VB导入Excel到数据库软件(持续更新中。)

    1.选择Excel文件版本 电脑上用的 Office2010 引用:Mircosoft Excel 14.0 Object Library 2.选择Excel文件 '选择文件公共变量 Public D ...

  5. Abstract(抽象)

    谈到抽象,就先谈谈面向对象语言的三大特性,也是人们口中常说的封装.继承.多态. 封装:什么是封装,按到我的理解,封装就是把某些类的相关属性和方法封装,对内实现数据影城,对外提供稳定接口. 继承:从字面 ...

  6. T-SQL 之 执行顺序

    1.sql查询语句的处理步骤,代码清单 --查询组合字段 (5)select (5-2) distinct(5-3) top(<top_specification>)(5-1)<se ...

  7. React文档总结

    元素渲染 更新元素渲染 计时器例子 function tick(){ const element = ( <div> <h1>Hello, World!</h1> ...

  8. 配置python的豆瓣source

    sunny@sunny-ThinkPad-T450:~$ mkdir ~/.pip sunny@sunny-ThinkPad-T450:~$ gedit ~/.pip/pip.conf [global ...

  9. 读书笔记 Week7 2018-4-24

    <程序员的自我修养> 首先来大致说一下读这本书的感觉,可以说对于我自己而言,是第一次对于编程有了个纯粹的认识,或者说的更明确一点,是对整个操作系统有了一个大致的感觉.虽然自己这一年也算是写 ...

  10. Linux实战教学笔记38:企业级Nginx Web服务优化实战(下)

    四,Nginx站点目录及文件URL访问控制 4.1 根据扩展名限制程序和文件访问 Web2.0时代,绝大多数网站都是以用户为中心多的,例如:bbs,blog,sns产品,这几个产品都有一个共同特点,就 ...