#include "measurepos.h"
using namespace MeasurePos;
Messure::Messure(float Row, float Column, double Phi, int Length1, int Length2, int Width, int Height, string Interpolation)
{
this->Row = Row;
this->Column = Column;
this->Phi = Phi;
this->Length1 = Length1;
this->Length2 = Length2;
this->Width = Width;
this->Height = Height;
this->Interpolation = Interpolation;
}
Messure::~Messure()
{
}
int Messure::measure_pos(Mat &Image, int &MeasureHandle, double Sigma, double Threshold, string Transition, string Select, vector<double> &RowEdge, vector<double> &ColumnEdge, vector<double> &Amplitude, vector<double> &Distance)
{
//图像旋转到水平
Mat Roiimage;
Roiimage.create(Height, Width, CV_8UC1);
for (int i = Row - Length2; i < Row + Length2;i++)
{
for (int j = Column - Length1; j < Column + Length1;j++)
{
Point2ff srcPoint,retPoint;
srcPoint.x = j - Column;
srcPoint.y = i - Row;
retPoint = RoratePoint(srcPoint, Phi);
retPoint.x += Column;
retPoint.y += Row;
if (Interpolation == Nearest_neighbor)
{
Roiimage.at<uchar>(i, j) = (uchar)Nearest_neighborInter(Image, retPoint.x, retPoint.y);
}
else if (Interpolation == Bilinear)
{
Roiimage.at<uchar>(i, j) = (uchar)BilinearInter(Image, retPoint.x, retPoint.y);
}
else if (Interpolation == Bicubic)
{
}
else
return -1;
}
}
//统计均值
vector<float> meangray;
meangray.clear();
for (int j = Column - Length1; j < Column + Length1; j++)
{
int gray = 0;
float meanvalue = 0;
for (int i = Row - Length2; i < Row + Length2; i++)
{
gray += Roiimage.at<uchar>(i, j);
}
meanvalue = gray / (2 * Length2);
meangray.push_back(meanvalue);
}
//显示点集
Mat meanimage;
meanimage.create(Height, Width, CV_8UC3);
int sizemean = meangray.size();
//高斯滤波
float f1, f0, f2;
f0 = 1.0 / (sqrtf(2 * CV_PI)*Sigma);
f1 = f0*exp(-1.0 / (Sigma*Sigma));
f2 = f1;
float sumoff;
sumoff = f0 + f1 + f2;
vector<float> gaussvalue;
gaussvalue.push_back(meangray[0]);
for (int i = 1; i < sizemean-1;i++)
{
float tmp;
tmp = meangray[i - 1] * f1 + meangray[i] * f0 + meangray[i + 1] * f2;
tmp = tmp / sumoff;
gaussvalue.push_back(tmp);
}
gaussvalue.push_back(meangray[sizemean-1]);
//求导
for (int i = 1; i < sizemean - 1;i++)
{
meangray[i] = (gaussvalue[i + 1] - gaussvalue[i-1])/2*(sqrtf(2 * CV_PI)*Sigma);
}
meangray[sizemean - 1] = 0;
//阈值分割
vector<float> threholdvalue;
threholdvalue.clear();
for (int i = 0; i < sizemean; i++)
{
if (fabs(meangray[i]) >= Threshold)
{
threholdvalue.push_back(meangray[i]);
}
else
threholdvalue.push_back(0);
}
//求倒数最大值
for (int i= 1; i < sizemean-1; i++)
{
Point2ff tmppoint,outpoint;
if ((fabs(threholdvalue[i])>fabs(threholdvalue[i - 1]) )&& (fabs(threholdvalue[i])>fabs(threholdvalue[i+1])))
{
if ((Transition == TransitionNeg&&threholdvalue[i]<0 )|| (Transition == TransitionPos&&threholdvalue[i]>0) || Transition == TransitionAll)
{
//抛物线拟合
float A[3][3] = { { 70 * 70, 70, 1 }, { 160 * 160, 160, 1 }, { 390 * 390, 390, 1 } };
A[0][0] = (i - 1)*(i - 1);
A[0][1] = (i - 1);
A[1][0] = i*i;
A[1][1] = i;
A[2][0] = (i + 1)*(i + 1);
A[2][1] = (i + 1);
float B[3] = { 420, 90, 410 };
B[0] = threholdvalue[i - 1];
B[1] = threholdvalue[i];
B[2] = threholdvalue[i + 1];
float C[3] = { 0, 0, 0 };
CvMat* matA = cvCreateMat(3, 3, CV_32FC1);
CvMat* matB = cvCreateMat(3, 1, CV_32FC1);
CvMat* matC = cvCreateMat(3, 1, CV_32FC1);
cvSetData(matA, A, CV_AUTOSTEP);
cvSetData(matB, B, CV_AUTOSTEP);
cvSolve(matA, matB, matC, CV_LU);
/*for (int j = 0; j < 3; j++){
printf("%f\t", matC->data.fl[j]);
}*/
//
tmppoint.x = -matC->data.fl[1] / (2 * matC->data.fl[0]);
Amplitude.push_back(matC->data.fl[0] * tmppoint.x*tmppoint.x + matC->data.fl[1] * tmppoint.x + matC->data.fl[2]);
tmppoint.x = tmppoint.x - Length1;
tmppoint.y = 0;
outpoint = RoratePoint(tmppoint, Phi);
outpoint.x += Column;
outpoint.y += Row;
RowEdge.push_back(outpoint.y);
ColumnEdge.push_back(outpoint.x);
//cout << " tmppoint.x= " << outpoint.x << " tmppoint.y= " << outpoint.y << endl;
}
}
}
int edgesize = RowEdge.size();
for (int i = 1; i < edgesize; i++)
{
Distance.push_back(sqrtf((RowEdge[i] - RowEdge[i - 1])*(RowEdge[i] - RowEdge[i - 1]) + (ColumnEdge[i] - ColumnEdge[i - 1])*(ColumnEdge[i] - ColumnEdge[i - 1])));
}
return 1;
}
Point2ff Messure::RoratePoint(Point2ff SrcPoint, double Phi)
{
Point2ff TmpPoint;
TmpPoint.x = float(cos(Phi)*SrcPoint.x - sin(-Phi)*SrcPoint.y);
TmpPoint.y = float(sin(-Phi)*SrcPoint.x + cos(Phi)*SrcPoint.y);
return TmpPoint;
}
//最近邻域插值
int Messure::Nearest_neighborInter(Mat &Image, float x, float y)
{
int curx = int(x);
int cury = int(y);
if (curx<0 || curx>Width || cury<0 || cury>Height)
return -1;
int gray = 0;
gray = Image.at<uchar>(cury, curx);
return gray;
}
//双线性插值
int Messure::BilinearInter(Mat &Image, float x, float y)
{
// 四个最临近象素的坐标(i1, j1), (i2, j1), (i1, j2), (i2, j2)
int x1, x2;
int y1, y2;
// 四个最临近象素值
unsigned char f1, f2, f3, f4;
// 二个插值中间值
unsigned char f12, f34;
double epsilon = 0.0001;
// 计算四个最临近象素的坐标
x1 = (int)x;
x2 = x1 + 1;
y1 = (int)y;
y2 = y1 + 1;
int nHeight = Height;
int nWidth = Width;
if ((x < 0) || (x > nWidth - 1) || (y < 0) || (y > nHeight - 1))
{
// 要计算的点不在源图范围内,返回-1
return -1;
}
else
{
if (fabs(x - nWidth + 1) <= epsilon)
{
// 要计算的点在图像右边缘上
if (fabs(y - nHeight + 1) <= epsilon)
{
// 要计算的点正好是图像最右下角那一个象素,直接返回该点象素值
f1 = Image.at<uchar>(y1, x1);
return f1;
}
else
{
// 在图像右边缘上且不是最后一点,直接一次插值即可
f1 = Image.at<uchar>(y1, x1);
f3 = Image.at<uchar>(y1, x2);
// 返回插值结果
return ((int)(f1 + (y - y1) * (f3 - f1)));
}
}
else if (fabs(y - nHeight + 1) <= epsilon)
{
// 要计算的点在图像下边缘上且不是最后一点,直接一次插值即可
f1 = Image.at<uchar>(y1, x1);
f2 = Image.at<uchar>(y2, x1);
// 返回插值结果
return ((int)(f1 + (x - x1) * (f2 - f1)));
}
else
{
// 计算四个最临近象素值
f1 = Image.at<uchar>(y1, x1);
f2 = Image.at<uchar>(y2, x1);
f3 = Image.at<uchar>(y1, x2);
f4 = Image.at<uchar>(y2, x2);
// 插值1
f12 = (unsigned char)(f1 + (x - x1) * (f2 - f1));
// 插值2
f34 = (unsigned char)(f3 + (x - x1) * (f4 - f3));
// 插值3
return ((int)(f12 + (y - y1) * (f34 - f12)));
}
}
}
bool Messure::Matrix_PolyFit(double *x, double *y, int X_Y_Number, int Fit_N, double *ks)
{
/*
对X_Y_Number组数据x,y进行Fit_N次多项式拟合,拟合返回多项式的系数ks
x[X_Y_Number],y[X_Y_Number],ks[Fit_N+1]
*/
if (Fit_N>X_Y_Number || X_Y_Number<1)
{
return false;
}
int i, j, index, n;
double temp, *x2, *y2;
Fit_N++;
y2 = new double[Fit_N];
x2 = new double[Fit_N*Fit_N];
for (i = 0, index = 0; i<Fit_N; i++)
{
y2[i] = 0;
for (j = 0; j<Fit_N; j++)
{
x2[index + j] = 0;
}
index += Fit_N;
}
x2[0] = X_Y_Number;
for (i = 0; i<Fit_N; i++)
{
for (j = i + 1; j<Fit_N; j++)
{
temp = 0;
n = i + j;
for (index = 0; index<X_Y_Number; index++)
{
temp += pow(x[index], n);
}
index = j;
for (n = i; n<Fit_N; n++)
{
if (index >= 0)
{
x2[n*Fit_N + index] = temp;
}
index--;
}
}
}
n = Fit_N + Fit_N - 2;
temp = 0;
for (i = 0; i<X_Y_Number; i++)
{
temp += pow(x[i], n);
}
x2[Fit_N*Fit_N - 1] = temp;
for (i = 0; i<Fit_N; i++)
{
temp = 0;
for (j = 0; j<X_Y_Number; j++)
{
temp += y
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
measurepos.rar (33个子文件)
measurepos
measurepos.sln 976B
measurepos
LineFit.cpp 3KB
measurepos.cpp 13KB
measurepos.vcxproj.user 165B
LineFit.h 371B
measurepos.vcxproj 4KB
measurepos.h 2KB
opencvdebug.props 2KB
Image00000.BMP 301KB
main.cpp 4KB
opencvrelease.props 2KB
measurepos.vcxproj.filters 1KB
Debug
measurepos.log 2KB
vc120.pdb 1.25MB
measurepos.obj 382KB
measurepos.Build.CppClean.log 814B
LineFit.obj 439KB
main.obj 293KB
measurepos.tlog
CL.write.1.tlog 2KB
CL.read.1.tlog 60KB
cl.command.1.tlog 2KB
link.write.1.tlog 564B
link.command.1.tlog 2KB
link.read.1.tlog 8KB
measurepos.lastbuildstate 154B
vc120.idb 835KB
test.png 34KB
ic_pin.png 435KB
measurepos.v12.suo 28KB
Debug
measurepos.ilk 648KB
measurepos.pdb 2.21MB
measurepos.exe 148KB
measurepos.sdf 15.94MB
共 33 条
- 1
邓凌佳
- 粉丝: 65
- 资源: 1万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论19