#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. 弗雷塞斯 从生物学到生物信息学到机器学习 转录组入门(3):了解fastq测序数据

    sra文件转换为fastq格式 1 fastq-dump -h --split-3 也就是说如果SRA文件中只有一个文件,那么这个参数就会被忽略.如果原文件中有两个文件,那么它就会把成对的文件按*_1 ...

  2. Win8电源选项中没有休眠这一项如何让Win8也能够休眠

    我们都知道,Win8默认的电源选项中是没有休眠这一选项的,即使用Alt+F4打开关闭Windows选项窗口也看不到”休眠“.难道Win8就不能够休眠了吗?答案当然不是,我们只要进行一些设置就能让Win ...

  3. WEB服务重要基础

    1.1用户访问房展基本流程 我们每天都会使用Web客户端上网浏览网页.最常见Web客户端就是Web浏览器,如通过的微软InternetExplorer(IE)以及技术人员偏爱的火狐浏览器.谷歌浏览器等 ...

  4. [z]兼容IE6的相对于浏览器窗口定位

    http://blog.uedsc.com/css-position.html http://www.w3cmm.com/notepad/css-position.html http://sofish ...

  5. Python实践练习:生成随机的测验试卷文件

    题目 假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验.不妙的是,班里有几个坏蛋,你无法确信学生不会作弊.你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人 ...

  6. RocketMQ初探(四)之RocketMQ4.x版本可视化管理控制台rocketmq-console-ng搭建(Apache)

    之前有部署过3.2.6为AliBaba版本的Web监控平台(可参考之前博客 https://www.cnblogs.com/buyige/p/9395453.html),现用RocketMQ4.2.0 ...

  7. IE11 - Object doesn't support property or method 'includes'

    IE不支持字符串的includes()方法:可以用indexOf()替换: includes()方法返回true和false; var str = "asdklmn": if(st ...

  8. 解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes

    今天想用用MySQL 数据库  谁知道老提示 1045 access denied for user 'root'@'localhost' using password yes 最后在csdn 上找到 ...

  9. 前端生成二维码 - Javascript生成二维码(QR)

    前段时间项目中需要动态的生成二维码,经过评估,前后端生成都可以.但后端生成会有两个问题: 没有找到正规发布出来的后端开源库. 二维码图片,会随着商品的增加而不断变多. 基于以上两个问题,决定在前端生成 ...

  10. fdsf

    https://blog.csdn.net/chen_2890/article/details/83757022Elasticsearch环境搭建和介绍(Windows) https://blog.c ...