void CHalconImage::CreateImageDispRect(HWND hwnd,CRect Screenrect)
{
m_Screenrect = Screenrect;
HWindowID = (Hlong)hwnd;
set_system("do_low_error","false");
open_window(m_Screenrect.left,m_Screenrect.top,m_Screenrect.Width(),m_Screenrect.Height(),
HWindowID,"visible","",&m_HWindowID);
}
void CHalconImage::LoadImage(CString strPath,BOOL AdjustImageFlag )
{
read_image(&m_LiveImage, strPath);
if(AdjustImageFlag)
{
get_image_size(m_LiveImage, &m_ImageWidth, &m_ImageHeight);
set_part(m_HWindowID, 0, 0, m_ImageWidth - 1, m_ImageHeight -1);
m_ImageZoom=m_ImageWidth/m_Screenrect.Width();
}
set_draw(m_HWindowID,"margin");
disp_obj(m_LiveImage, m_HWindowID);
}
//输入文字到图象上
void CHalconImage::DrawTxt(int x,int y,CString strtxt,CString color)
{
set_color( m_HWindowID,color.GetBuffer(color.GetLength()));
set_tposition( m_HWindowID,(Hlong)(x),(Hlong)(y) );
write_string( m_HWindowID,HTuple(strtxt));
color.ReleaseBuffer();
}
//在图象上画出直线
void CHalconImage::DrawLine(int startx,int starty,int endx,int endy,CString color)
{
set_color( m_HWindowID,color.GetBuffer(color.GetLength()));
set_draw(m_HWindowID, "margin");
disp_line( m_HWindowID, startx,starty,endx,endx);
color.ReleaseBuffer();
}
//学习模板
BOOL CHalconImage::LeanModel()
{
DrawTxt(20,20,"用鼠标左键按下画出模板区域","blue");
HTuple Row, Column, Phi, Length1, Length2;
Next1:
draw_rectangle2(m_HWindowID, &Row, &Column, &Phi, &Length1, &Length2);
gen_rectangle2(&Rectangle, Row, Column, Phi, Length1, Length2);
int res=AfxMessageBox("是否确认此区域的图象作为模板的图象?YES[确认] NO[重画] ",MB_YESNOCANCEL);
if(res==IDCANCEL)
return FALSE;
else if(res == IDNO)
goto Next1;
reduce_domain(m_LiveImage, Rectangle, &ImageReduced);
create_shape_model(ImageReduced, "auto", HTuple(0).Rad(), HTuple(360).Rad(),
"auto", "auto", "use_polarity", "auto", "auto", &ModelID);
return TRUE;
}
//查找模板
BOOL CHalconImage::FindModel()
{
find_shape_model(m_LiveImage, ModelID, HTuple(0).Rad(), HTuple(360).Rad(),
0.5, 1, 0.5, "least_squares", 0, 0.9, &Row1, &Column1, &Angle, &Score);
dev_display_shape_matching_results(ModelID, "red", Row1, Column1, Angle, 1, 1, 0);
set_color(m_HWindowID,"blue");
gen_cross_contour_xld(&Cross, Row1, Column1, 30, HTuple(45).Rad());
disp_obj(Cross, m_HWindowID);
return TRUE;
}
void CHalconImage::dev_display_shape_matching_results (Halcon::HTuple ModelID,
Halcon::HTuple Color, Halcon::HTuple Row, Halcon::HTuple Column,
Halcon::HTuple Angle, Halcon::HTuple ScaleR, Halcon::HTuple ScaleC,
Halcon::HTuple Model)
{
Hobject ModelContours, ContoursAffinTrans;
HTuple NumMatches, Index, Match, HomMat2DIdentity;
HTuple HomMat2DScale, HomMat2DRotate, HomMat2DTranslate;
NumMatches = Row.Num();
if (NumMatches>0)
{
if (0 != ((ScaleR.Num())==1))
{
tuple_gen_const(NumMatches, ScaleR, &ScaleR);
}
if (0 != ((ScaleC.Num())==1))
{
tuple_gen_const(NumMatches, ScaleC, &ScaleC);
}
if (0 != ((Model.Num())==0))
{
tuple_gen_const(NumMatches, 0, &Model);
}
else if (0 != ((Model.Num())==1))
{
tuple_gen_const(NumMatches, Model, &Model);
}
for (Index=0; Index<=(ModelID.Num())-1; Index+=1)
{
double ang =Angle[Index].D()*180/PI;
CString str;
str.Format("%f,%f,%f",Row[Index].D(),Column[Index].D(),ang);
DrawTxt(50*Index[0].I(),20,str,"blue");
get_shape_model_contours(&ModelContours, HTuple(ModelID[Index]), 1);
set_color(m_HWindowID,HTuple(Color[Index%(Color.Num())]));
for (Match=0; Match<=NumMatches-1; Match+=1)
{
if (0 != (Index==HTuple(Model[Match])))
{
/
hom_mat2d_identity(&HomMat2DIdentity);
hom_mat2d_scale(HomMat2DIdentity, HTuple(ScaleR[Match]), HTuple(ScaleC[Match]),
0, 0, &HomMat2DScale);
hom_mat2d_rotate(HomMat2DScale, HTuple(Angle[Match]), 0, 0, &HomMat2DRotate);
hom_mat2d_translate(HomMat2DRotate, HTuple(Row[Match]), HTuple(Column[Match]),
&HomMat2DTranslate);
affine_trans_contour_xld(ModelContours, &ContoursAffinTrans, HomMat2DTranslate);
disp_obj(ContoursAffinTrans, m_HWindowID);
}
}
}
}
return;
}