📄 vehicletracedlg.cpp
字号:
HTuple AngleA, AngleB, AngleSub, Distance,AreaSub;
HTuple XA1=existVehicle.SeedX[SeedNum-2];
HTuple YA1=existVehicle.SeedY[SeedNum-2];
HTuple XA2=existVehicle.SeedX[SeedNum-1];
HTuple YA2=existVehicle.SeedY[SeedNum-1];
HTuple XB1=RowNS[r];
HTuple YB1=ColumnNS[r];
HTuple XB2=RowSC[long(SeedMatchInd[0])];
HTuple YB2=ColumnSC[long(SeedMatchInd[0])];
::angle_ll(XA1,YA1,XA2,YA2,XA2,YA2,XA2,YA2+50,&AngleA);
::angle_ll(XB1,YB1,XB2,YB2,XB2,YB2,XB2,YB2+50,&AngleB);
AngleSub = (AngleA-AngleB).Abs();
::distance_pp(XA2,YA2,XB1,YB1,&Distance);
FrameSub=k-existVehicle.FrameEnd;
AreaSub=abs(existVehicle.SeedArea[SeedNum-1]-AreaNS[r]);
::tuple_concat(FrameSubs,FrameSub,&FrameSubs);
::tuple_concat(AreaSubs,AreaSub,&AreaSubs);
::tuple_concat(Distances,Distance,&Distances);
::tuple_concat(AngleSubs,AngleSub,&AngleSubs);
::tuple_concat(VehicleIndex,f,&VehicleIndex);
}
}
if(FrameSubs.Num()!=0)
{
HTuple JoinValue,JoinValueInd;
::tuple_max(FrameSubs,&FrameSubsMAX);
::tuple_max(AreaSubs,&AreaSubsMAX);
::tuple_max(Distances,&DistancesMAX);
::tuple_max(AngleSubs,&AngleSubsMAX);
if(FrameSubsMAX!=0)
FrameSubs=FrameSubs/FrameSubsMAX;
if(AreaSubsMAX!=0)
AreaSubs=AreaSubs/AreaSubsMAX;
if(DistancesMAX!=0)
Distances=Distances/DistancesMAX;
if(AngleSubsMAX!=0)
AngleSubs=AngleSubs/AngleSubsMAX;
JoinValue=FrameSubs+AreaSubs+Distances+AngleSubs;
::tuple_sort_index(JoinValue,&JoinValueInd);
Vehicle Vehicle=Vehicles.GetAt(VehicleIndex[long(JoinValueInd[0])]);
::tuple_concat(Vehicle.SeedX,RowNS[r],&Vehicle.SeedX);
::tuple_concat(Vehicle.SeedY,ColumnNS[r],&Vehicle.SeedY);
::tuple_concat(Vehicle.SeedArea,AreaNS[r],&Vehicle.SeedArea);
::tuple_concat(Vehicle.SeedX,HTuple(RowSC[long(SeedMatchInd[0])]),&(Vehicle.SeedX));
::tuple_concat(Vehicle.SeedY,HTuple(ColumnSC[long(SeedMatchInd[0])]),&(Vehicle.SeedY));
::tuple_concat(Vehicle.SeedArea,HTuple(AreaSC[long(SeedMatchInd[0])]),&(Vehicle.SeedArea));
//Vehicle.FrameStart=k;
Vehicle.isEnd=false;
Vehicles.SetAt(long(JoinValueInd[0]),Vehicle);
}
else */
//***第四部:增加新目标,更新临时种子点************************************************************
// 对于那些新发现的而又未被匹配上的点则成为临时种子点
::select_shape(SelectedRegionsGray,&SelectedRegionsNS,HTuple("area"),HTuple("and"),HTuple(200),HTuple(99999));
::area_center(SelectedRegionsNS,&AreaNS,&RowNS,&ColumnNS);
::tuple_sqrt(AreaNS,&LenNS);
//***第五步:根据当前帧的灰度状况对背景进行更新***********************************
::union1(RegionGray,&RegionUnionFinal);
::dilation_rectangle1(RegionUnionFinal,&RegionDilation,HTuple(5),HTuple(5));
::complement(RegionDilation,&BackGround);
::clip_region(BackGround,&RegionClipped,HTuple(0),HTuple(0),m_lHeight-1,m_lWidth-1);
::zoom_region(RegionClipped,&BackGroundZoom,HTuple(0.5),HTuple(0.5));
::update_bg_esti(ImageZoomed,BackGroundZoom,BgEstiHandle);
::shape_trans(RegionGray,&RegionRec,HTuple("rectangle1"));//'rectangle1'
::disp_obj(ActualImage,m_lpWindowID);
::disp_obj(RegionRec,m_lpWindowID);
//***第六步:显示结果:运动速度和运动轨迹********************************************
for (int e=0; e<Vehicles.GetSize(); e++)
{
Vehicle existVehicle=Vehicles.GetAt(e);
if(existVehicle.isEnd==false)
{
int SeedNum=existVehicle.SeedX.Num();
// 显示轨迹
if(SeedNum>2)
{
Hobject Route;
gen_contour_polygon_xld(&Route,existVehicle.SeedX,existVehicle.SeedY);
//::disp_arrow(m_lWindowID,HTuple(x[SeedNum-3]),HTuple(y[SeedNum-3]),HTuple(x[SeedNum-2]),HTuple(y[SeedNum-2]),HTuple(1));
::disp_obj(Route,m_lpWindowID);
}
// 显示速度
::set_tposition(m_lpWindowID,existVehicle.SeedX[SeedNum-1],existVehicle.SeedY[SeedNum-1]);
::write_string(m_lpWindowID,HTuple(existVehicle.Velocity[SeedNum-2]*HTuple(0.036))); //*0.036将单位由cm/s化为m/s
}
}
}
}
void CVehicleTraceDlg::OnCaptureChanged(CWnd *pWnd)
{
// TODO: Add your message handler code here
CDialog::OnCaptureChanged(pWnd);
}
void CVehicleTraceDlg::OnStop()
{
isPlay=false;
m_btnstop.EnableWindow(false);
m_btncontinue.EnableWindow(true);
}
void CVehicleTraceDlg::OnContinue()
{
isPlay=true;
m_btnstop.EnableWindow(true);
m_btncontinue.EnableWindow(false);
}
void CVehicleTraceDlg::OnEnd()
{
// ::give_bg_esti(&BackgroundImage,BgEstiHandle);
// ::zoom_image_factor(BackgroundImage,&ImageZoomed,HTuple(2),HTuple(2),HTuple("constant"));
// ::disp_obj(ImageZoomed,m_lWindowID);
m_btnstart.EnableWindow(true);
m_btnend.EnableWindow(false);
m_btnstop.EnableWindow(false);
m_btncontinue.EnableWindow(false);
isPlay=false;
KillTimer(m_nIdEvent);
close_framegrabber(m_lFGHandle);
::close_bg_esti(BgEstiHandle);
::close_window(WindowHandleExpand);
}
void CVehicleTraceDlg::OnShowpic()
{
HWindow w;
HImage obj(Image);
obj.Display(w);
w.Click();
}
void CVehicleTraceDlg::OnBtnCalibration()
{
UpdateData();
// 为用于标定的控制点赋坐标值--二维平面坐标(X,Y)
HTuple WorldX,WorldY;
int n=0;
int index1=0;
int index2=m_edit_xw.Find(",");
while(index2!=-1)
{
// 读取标定控制点的世界坐标X
CString temp=m_edit_xw.Left(index2);
WorldX.Append(atof(temp));
int length=index2-index1+1;
m_edit_xw.Delete(index1,length);
index2=m_edit_xw.Find(",");
n++;
}
index2=m_edit_yw.Find(",");
while(index2!=-1)
{
// 读取标定控制点的世界坐标Y
CString temp=m_edit_yw.Left(index2);
WorldY.Append(atof(temp));
int length=index2-index1+1;
m_edit_yw.Delete(index1,length);
index2=m_edit_yw.Find(",");
}
HTuple Pointer, Type, Scale;
HTuple Row1, Column1, Row2, Column2, Pointer1, Type1, Width1,Height1;
HTuple HomMat2DIdentity, scale;
HTuple HomMat2DScale, rows, columns, Covariance;
HTuple Qx, Qy, Qw, XResult, YResult;
Hobject Rectangle, ImageReduced, ImageZoomed, Region;
Hobject RegionAffineTrans, GrayImage, BlackRegion, WhiteRegion;
Hobject Contours, Cross;
Scale = HTuple(4);
// ::set_part(m_lpWindowID,HTuple(1),HTuple(1),m_lHeight-1,m_lWidth-1);
// 画一个矩形框放大图像以便于点出用于标定的控制点
::draw_rectangle1(m_lpWindowID,&Row1,&Column1,&Row2,&Column2);
::tuple_int(Row1,&Row1);
::tuple_int(Column1,&Column1);
::tuple_int(Row2,&Row2);
::tuple_int(Column2,&Column2);
::gen_rectangle1(&Rectangle,Row1,Column1,Row2,Column2);
::reduce_domain(Image,Rectangle,&ImageReduced);
::zoom_image_factor(ImageReduced,&ImageZoomed,Scale,Scale,HTuple("constant"));
// ::get_image_pointer1(ImageZoomed,&Pointer1,&Type1,&Width1,&Height1);
::set_window_attr("background_color",HTuple("black"));
::open_window(HTuple(0),m_lWidth,(Column2-Column1)*Scale,(Row2-Row1)*Scale,0,"","",&WindowHandleExpand);
::set_part(WindowHandleExpand,Row1*Scale,Column1*Scale,Row2*Scale,Column2*Scale);
::disp_obj(ImageZoomed,WindowHandleExpand);
// 注意在上述函数中确定显示区域时一定要*Scale!!
::draw_region(&Region,WindowHandleExpand); //画若干个与世界坐标对应的点
::hom_mat2d_identity(&HomMat2DIdentity); //产生单位阵
scale = HTuple(1)/(Scale.Real());
::hom_mat2d_scale(HomMat2DIdentity,scale,scale,HTuple(0),HTuple(0),&HomMat2DScale);//缩放
::affine_trans_region(Region,&RegionAffineTrans,HomMat2DScale,HTuple("false"));//仿射变换
::reduce_domain(Image,RegionAffineTrans,&ImageReduced); //将用于标定的点还原至原始图像中
::rgb1_to_gray(ImageReduced,&GrayImage); //将彩色图像(rgb)转化为灰度图像
::bin_threshold(GrayImage,&BlackRegion);
::difference(GrayImage,BlackRegion,&WhiteRegion);
::gen_contour_region_xld(WhiteRegion,&Contours,HTuple("border"));
// 自动匹配出四个角点
findcornerNew(WhiteRegion,&rows,&columns);
// 将匹配出来的结果在图像中标示出来
::gen_cross_contour_xld(&Cross,rows*Scale,columns*Scale,HTuple(6),HTuple(0.785398));
::set_colored(WindowHandleExpand,HTuple(6));
::disp_obj(Cross,WindowHandleExpand);
// 求解转换矩阵
::vector_to_proj_hom_mat2d(rows,columns,WorldX,WorldY,HTuple("gold_standard"),HTuple(),HTuple(),HTuple(),HTuple(),HTuple(),HTuple(),&HomMat2D,&Covariance);
// 说明:上述函数中至少需要4个点才能计算,有可能采用的是八参数模型的仿射变换!
}
void CVehicleTraceDlg::EdgeSort (Hobject Edges, Hobject *EdgeGroup1, Hobject *EdgeGroup2, HTuple Row, HTuple Column)
{
// 本函数的作用是?????
HTuple RowEdge, ColumnEdge, RowBegin, ColBegin, RowEnd;
HTuple ColEnd, Nr, Nc, Dist, R, flag, i;
Hobject ObjectSelected;
::area_center_xld(Edges,_,&RowEdge,&ColumnEdge,_);
::select_obj(Edges,&ObjectSelected,HTuple(1));
::fit_line_contour_xld(ObjectSelected,HTuple("tukey"),HTuple(-1),HTuple(0),HTuple(5),HTuple(2),&RowBegin,&ColBegin,&RowEnd,&ColEnd,&Nr,&Nc,&Dist);
R = (((ColumnEdge-Column)*(HTuple(RowBegin[long(0)])-HTuple(RowEnd[long(0)])))/(HTuple(ColBegin[long(0)])-HTuple(ColEnd[long(0)])))+Row;
flag = R-RowEdge;
::gen_empty_obj(&(*EdgeGroup1));
::gen_empty_obj(&(*EdgeGroup2));
for (i=0; i<=(R.Num())-1; i+=1)
{
::select_obj(Edges,&ObjectSelected,i+1);
if (HTuple(flag[long(i[0])])>0)
{
::concat_obj((*EdgeGroup1),ObjectSelected,&(*EdgeGroup1));
}
else
{
::concat_obj((*EdgeGroup2),ObjectSelected,&(*EdgeGroup2));
}
}
::union_straight_contours_xld((*EdgeGroup1),&(*EdgeGroup1),HTuple(5),HTuple(0.5),HTuple(50),HTuple("every"),HTuple("maximum"));
::union_straight_contours_xld((*EdgeGroup2),&(*EdgeGroup2),HTuple(5),HTuple(0.5),HTuple(50),HTuple("every"),HTuple("maximum"));
return;
}
void CVehicleTraceDlg::findcornerNew (Hobject Region, HTuple *rows, HTuple *columns)
{
HTuple Area, Row, Column, RowBegin, ColBegin, RowEnd, ColEnd;
HTuple Nr, Nc, Dist, i, IsParallel;
Hobject Contours, ContoursSplit, RegressContours, SelectedContoursH;
Hobject EdgeGroupH1, EdgeGroupH2, SelectedContoursV, EdgeGroupV1;
Hobject EdgeGroupV2, Edges;
::area_center(Region,&Area,&Row,&Column);
::gen_contour_region_xld(Region,&Contours,HTuple("border"));
// 用直线拟合区域边界:"lines"
::segment_contours_xld(Contours,&ContoursSplit,HTuple("lines"),HTuple(5),HTuple(1.5),HTuple(1));
// 计算用直线拟合后的边界的回归曲线
::regress_contours_xld(ContoursSplit,&RegressContours,HTuple("no"),HTuple(1));
// 对这些回归曲线加以选择
::select_contours_xld(RegressContours,&SelectedContoursH,HTuple("direction"),HTuple(-45).Rad(),HTuple(45).Rad(),HTuple(-0.5),HTuple(0.5));
EdgeSort(SelectedContoursH,&EdgeGroupH1,&EdgeGroupH2,Row,Column);
::select_contours_xld(RegressContours,&SelectedContoursV,HTuple("direction"),HTuple(45).Rad(),HTuple(135).Rad(),HTuple(-0.5),HTuple(0.5));
EdgeSort(SelectedContoursV,&EdgeGroupV1,&EdgeGroupV2,Row,Column);
::concat_obj(EdgeGroupH1,EdgeGroupV2,&Edges);
::concat_obj(Edges,EdgeGroupH2,&Edges);
::concat_obj(Edges,EdgeGroupV1,&Edges);
::fit_line_contour_xld(Edges,HTuple("tukey"),HTuple(-1),HTuple(0),HTuple(5),HTuple(2),&RowBegin,&ColBegin,&RowEnd,&ColEnd,&Nr,&Nc,&Dist);
(*rows) = HTuple();
(*columns) = HTuple();
for (i=0; i<=2; i+=1)
{
// 计算两直线的交点
::intersection_ll(HTuple(RowBegin[long(i[0])]),HTuple(ColBegin[long(i[0])]),HTuple(RowEnd[long(i[0])]),HTuple(ColEnd[long(i[0])]),HTuple(RowBegin[long(HTuple(i+1)[0])]),HTuple(ColBegin[long(HTuple(i+1)[0])]),HTuple(RowEnd[long(HTuple(i+1)[0])]),HTuple(ColEnd[long(HTuple(i+1)[0])]),&Row,&Column,&IsParallel);
::tuple_concat((*rows),Row,&(*rows));
::tuple_concat((*columns),Column,&(*columns));
}
// 计算两直线的交点
::intersection_ll(HTuple(RowBegin[long(0)]),HTuple(ColBegin[long(0)]),HTuple(RowEnd[long(0)]),HTuple(ColEnd[long(0)]),HTuple(RowBegin[long(3)]),HTuple(ColBegin[long(3)]),HTuple(RowEnd[long(3)]),HTuple(ColEnd[long(3)]),&Row,&Column,&IsParallel);
::tuple_concat((*rows),Row,&(*rows));
::tuple_concat((*columns),Column,&(*columns));
return;
}
void CVehicleTraceDlg::OnBtnBrownse()
{
// TODO: 在此添加控件通知处理程序代码
BROWSEINFO bi;
char szDispName[MAX_PATH+1];
memset(&bi,0,sizeof(BROWSEINFO));
bi.hwndOwner = m_hWnd;
bi.pszDisplayName = szDispName;
bi.ulFlags = BIF_BROWSEINCLUDEFILES+BIF_EDITBOX;
//bi.lpszTitle = "Please select the folder u want to clean:";
if(ITEMIDLIST *iil = SHBrowseForFolder(&bi))
{
SHGetPathFromIDList(iil, szDispName);
m_strPath = szDispName;
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -