#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);
}

最新文章

  1. [Java入门笔记] Java语言基础(二):常量、变量与数据类型
  2. css 文本气泡样式
  3. jQyery实现轮播器
  4. 线程池ThreadPoolExecutor
  5. C# 中将多个空格替换成一个空格
  6. Java IO (3) - Reader
  7. S2SH商用后台权限系统第二讲
  8. [ES6] Array.findIndex()
  9. C语言函数入门
  10. HDU 2149-Public Sale(巴什博奕)
  11. 多少遍ner让他加56看6
  12. 基于visual Studio2013解决C语言竞赛题之0608水仙花函数
  13. ios 清除列表选中状态
  14. C# 逆变与协变
  15. 解决win环境下访问本机虚拟机中centos7 ftp服务器的问题
  16. 我为什么推荐Prettier来统一代码风格
  17. 分布式全局ID生成器设计
  18. cocos creator踩坑日记
  19. Mysql经常使用基本命令汇总及默认账户权限与改动
  20. 记开发个人图书收藏清单小程序开发(九)Web开发——新增图书信息

热门文章

  1. Django timezone问题
  2. Spring 学习记录7 初识XmlWebApplicationContext
  3. objective-C 的内存管理之-实例分析
  4. hibernate nhibernate sqlserver数据库的默认值冲突解决
  5. git本地代码库回滚(webstorm下)
  6. rocketmq消息存储概述
  7. Spring注解配置Aop
  8. 开发 WebAPP 的几个前端框架(不断更新中)
  9. Ros学习——movebase源码解读之amcl
  10. iOS设备尺寸