^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Date:2018.09.09

需求:在一个矩形ROI区域内抓到产品的边缘(为规则的直线);矩形框需人为设定。

  • 相关halcon算子:

1. gen_measure_rectangle2

void GenMeasureRectangle2(const HTuple &row, const HTuple &column, \
  const HTuple &radius, const HTuple &len1, const HTuple &len2, \
  const HTuple &width, const HTuple &height, const HTuple &interpolation, \
  HTuple *measureHandle);

2. measure_pos

void MeasurePos(const HObject &image, const HTuple &measureHandle, \
  const HTuple &sigma, cosnt HTuple &threshold, const HTuple &transition, const HTuple &select, \
  HTuple *rowEdge, HTuple *colEdge, HTuple *amplitude, HTuple *distance);

3.  fit_line_contour_xld

void FitLineContourXld(const HObject &contours, const HTuple &algorithm, \
  const HTuple &maxNumPoints, const HTuple &clippingEndPoints, const HTuple &iterations, const HTuple clippingFactor, \
  HTuple *rowBegin, HTuple *colBegin, HTuple *rowEnd, HTuple *colEnd, HTuple *nr, HTuple *nc, HTuple *dist);

 

代码实现:

step1.划定矩形ROI区域,并归一化方向。(以水平边缘线为例

DrawRectangle2(3600, &hv_Row, &hv_Column, &hv_Phi, &hv_Length1, &hv_Length2);
GenRectangle2(&ho_m_region, hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2);
SmallestRectangle2(ho_m_region, &hv_row0, &hv_col0, &hv_radius0, &hv_len1, &hv_len2);
hv_phi0 = hv_radius0.TupleDeg();
if (0 != (hv_phi0<0))
{
  hv_phi0 += 180;
}
hv_radius0 = hv_phi0.TupleRad();
hv_phi0 = hv_radius0.TupleDeg();
if (0 != (HTuple("horizontal")==hv_m_direction))
{
  if (0 != (hv_phi0<90))
  {
    hv_phi1 = hv_phi0+90;
  }
  else
  {
    hv_phi1 = hv_phi0-90;
  }
}
hv_radius1 = hv_phi1.TupleRad();

 step2. 生成measureHandle并找到一系列边缘点

for (hv_i=0; hv_i.Continue(end_val28, step_val28); hv_i += step_val28)
{
  hv_row = hv_row0+(((hv_m_step-(2*hv_i))*hv_stepLen)*(hv_radius0.TupleSin()));
  hv_col = hv_col0-(((hv_m_step-(2*hv_i))*hv_stepLen)*(hv_radius0.TupleCos()));
  GenMeasureRectangle2(hv_row, hv_col, hv_radius1, hv_len2, hv_stepLen*0.6, hv_widthImg, 
      hv_heightImg, "nearest_neighbor", &hv_MeasureHandle);
  GenRectangle2(&ho_Rectangle, hv_row, hv_col, hv_radius1, hv_len2, hv_stepLen);
  MeasurePos(ho_m_image, hv_MeasureHandle, hv_m_sigma, hv_m_threshold, hv_m_transition, 
      hv_m_selection, &hv_rowEdge, &hv_colEdge, &hv_Amplitude, &hv_Distance);
  //待添加...

  if (0 != ((hv_Amplitude.TupleLength())>0))
  {
    hv_Indices = 0;
    TupleAbs(hv_Amplitude, &hv_Amplitude);
    TupleMax(hv_Amplitude, &hv_Max);
    TupleFind(hv_Amplitude, hv_Max, &hv_Indices);
    hv_rowsEdge = hv_rowsEdge.TupleConcat(HTuple(hv_rowEdge[HTuple(hv_Indices[0])]));
    hv_colsEdge = hv_colsEdge.TupleConcat(HTuple(hv_colEdge[HTuple(hv_Indices[0])]));
  }
...    
}

 step3. 拟合直线边缘

GenContourPolygonXld(&ho_lineXLD, hv_rowsEdge, hv_colsEdge);
FitLineContourXld(ho_lineXLD, "tukey", -1, 0, 5, 2, &hv_RowBegin, &hv_ColBegin, 
    &hv_RowEnd, &hv_ColEnd, &hv_Nr, &hv_Nc, &hv_Dist);

 OK了。

 

posted on 2018-09-09 21:48  xiawuhao2013  阅读(1652)  评论(0编辑  收藏  举报