-
大小: 4.16MB文件类型: .rar金币: 2下载: 0 次发布日期: 2023-08-10
- 语言: C/C++
- 标签: Wong-Trinder 圆心定位 C++ OPENCV
资源简介
采用基于opencv249的C++编写,Wong-Trinder圆点定位算子

代码片段和文件信息
// CenterLocation.cpp : 定义控制台应用程序的入口点。
#include “stdafx.h“
#include
#include
#include
using namespace std;
using namespace cv;
//1.构造函数m,用于求解p+q阶原点矩mpq
int m_(int iint jint p int q int b)
{
double res = 0;
res += pow(double(i) p) * pow(double(j) q) * b;
return res;
}
//2.构造函数M,用于求解p+q阶中心矩Mpq
int M_(int iint jdouble xdouble yint p int q int b)
{
double res = 0;
res += pow(i-x p) * pow(j-y q) * b;
return res;
}
//3.主函数,对圆心(xy)、圆度r进行求解
int main(void)
{
//3.1读取数据
IplImage * imgSrc = cvLoadImage(“circle1.jpg“);//读取三通道图像数据(RGB)
IplImage* imggray = cvCreateImage(cvGetSize(imgSrc) IPL_DEPTH_8U 1);//申请一段内存
cvCvtColor(imgSrc imggray CV_BGR2GRAY); //灰度化
cvSaveImage(“RGBtoGray1.jpg“imggray);//保存灰度化后的图像文件
int height = imgSrc->height;//获取图像的高和宽
int width = imgSrc->width;
//3.2计算阈值T
CvScalar pixel;//定义数组
int g[100][100]w[100][100];
int min = 256;double Average = 0;
for (int i = 0; i < height; i++)//读取灰度值并存放到数组中
{
for (int j = 0; j < width; j++)
{
pixel = cvGet2D(imgSrc i j);
if(pixel.val[0] < min)
min = int(pixel.val[0]);
Average += pixel.val[0];
g[i][j] = int(pixel.val[0]);
w[i][j] = int(pixel.val[0]);
}
}
Average = Average/(width*height);
double T = (min + Average) * 0.5;//计算出阈值
//3.3图像二值化并计算坐标(xy)
int m00 = 0m10 = 0m01 = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if(g[i][j]>T) g[i][j] = 1;//图像二值化
else g[i][j] = 0;
m00 += m_(ij00g[i][j]);
m10 += m_(ij10g[i][j]);
m01 += m_(ij01g[i][j]);
}
}
double x = m10/(m00 * 1.0)y = m01 / (m00 * 1.0);
//3.4计算Mx、My、圆度r
double M20 = 0M02 = 0M11 = 0M = 0x_=0y_=0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
M20 += M_(ijxy20g[i][j]);
M02 += M_(ijxy02g[i][j]);
M11 += M_(ijxy11g[i][j]);
x_ += i * g[i][j] * w[i][j];
y_ += j * g[i][j] * w[i][j];
M += g[i][j] * w[i][j];
}
}
double Mx = (M20 + M02) / 2.0 + sqrt(M11 * M11 + 0.5 * (M20 - M02) * 0.5 * (M20 - M02));
double My = (M20 + M02) / 2.0 - sqrt(M11 * M11 + 0.5 * (M20 - M02) * 0.5 * (M20 - M02));
double r = Mx / My;
//3.5使用Trinder算子计算改进后的xy
x_ *= 1 / M;
y_ *= 1 / M;
//3.6输出结果
cout<<“最小灰度值min=“< cout<<“中间量:“<<“\n“<<“m10=“<< m10 <<“ m01=“<< m01 <<“ m00=“<< m00 <<“\n““M20=“< cout<<“Mx=“<
//3.7保存到文件
ofstream SaveFile(“结果1.txt“);
SaveFile<<“最小灰度值min=“< SaveFile<<“中间量:“<<“\n“<<“m10=“<< m10 <<“ m01=“<< m01 <<“ m00=“<< m00 <<“\n““M20=“<
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3706 2017-10-12 19:58 CenterLocation\CenterLocation\CenterLocation.cpp
文件 4380 2017-10-09 19:07 CenterLocation\CenterLocation\CenterLocation.vcxproj
文件 1318 2017-10-09 19:07 CenterLocation\CenterLocation\CenterLocation.vcxproj.filters
文件 143 2017-10-09 19:07 CenterLocation\CenterLocation\CenterLocation.vcxproj.user
文件 22611 2013-09-26 14:39 CenterLocation\CenterLocation\circle1.jpg
文件 25171 2013-09-26 14:38 CenterLocation\CenterLocation\circle2.jpg
文件 2897 2017-10-12 19:38 CenterLocation\CenterLocation\Debug\CenterLocation.Build.CppClean.log
文件 406 2017-10-12 19:38 CenterLocation\CenterLocation\Debug\CenterLocation.exe.em
文件 472 2017-10-12 19:38 CenterLocation\CenterLocation\Debug\CenterLocation.exe.em
文件 381 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\CenterLocation.exe.intermediate.manifest
文件 75 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\CenterLocation.lastbuildstate
文件 4043 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\CenterLocation.log
文件 412611 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\CenterLocation.obj
文件 1179648 2017-10-12 19:38 CenterLocation\CenterLocation\Debug\CenterLocation.pch
文件 0 2017-10-12 19:38 CenterLocation\CenterLocation\Debug\CenterLocation.write.1.tlog
文件 218 2017-10-12 19:38 CenterLocation\CenterLocation\Debug\CenterLocation_manifest.rc
文件 2290 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\cl.command.1.tlog
文件 20826 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\CL.read.1.tlog
文件 1020 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\CL.write.1.tlog
文件 2 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 2 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 2 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 2 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 2 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 2 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 2854 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 6424 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 1082 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\li
文件 450 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\mt.command.1.tlog
文件 394 2017-10-12 19:58 CenterLocation\CenterLocation\Debug\mt.read.1.tlog
............此处省略28个文件信息
- 上一篇:VC++头文件库
- 下一篇:C语言读取文件数据,写入内存
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
评论
共有 条评论