void EIEdgeImage::Read(LFLineFitter &lf)
{
	SafeRelease();
	width_ = lf.rWidth();
	height_ = lf.rHeight();
	nLines_ = lf.rNLineSegments();
	LFLineSegment* lineSegmentMap = lf.rOutputEdgeMap();

	lines_ = new LFLineSegment[nLines_];
	for (int i=0 ; i<nLines_ ; i++)
		lines_[i] = lineSegmentMap[i];

	SetLines2Grid();
	SetDirections();

}
Exemplo n.º 2
0
void computeNormals(const cv::Mat &edges, cv::Mat &normals, cv::Mat &orientationIndices)
{
  //TODO: move up
  const int directionsCount = 60;
  LFLineFitter lineFitter;
  fitLines(edges, lineFitter);

  Mat linearMap(edges.size(), CV_8UC1, Scalar(0));
  Mat linearMapNormals(edges.size(), CV_32FC2, Scalar::all(std::numeric_limits<float>::quiet_NaN()));
  Mat linearMapOrientationIndices(edges.size(), CV_32SC1, Scalar(-1));
  for (int i = 0; i < lineFitter.rNLineSegments(); ++i)
  {
    cv::Point start(lineFitter.outEdgeMap_[i].sx_, lineFitter.outEdgeMap_[i].sy_);
    cv::Point end(lineFitter.outEdgeMap_[i].ex_, lineFitter.outEdgeMap_[i].ey_);

    LineIterator edgelsIterator(linearMap, start, end);
    for(int j = 0; j < edgelsIterator.count; ++j, ++edgelsIterator)
    {
      **edgelsIterator = 255;
      Vec2f normal(lineFitter.outEdgeMap_[i].normal_.x, lineFitter.outEdgeMap_[i].normal_.y);
      linearMapNormals.at<Vec2f>(edgelsIterator.pos()) = normal;
      linearMapOrientationIndices.at<int>(edgelsIterator.pos()) = theta2Index(lineFitter.outEdgeMap_[i].Theta(), directionsCount);
    }
  }
//  imshow("edges", edges);
//  imshow("linearMap", linearMap);
//  waitKey();

  Mat dt, labels;
  distanceTransform(~linearMap, dt, labels, CV_DIST_L2, CV_DIST_MASK_PRECISE, DIST_LABEL_PIXEL);

  CV_Assert(linearMap.type() == CV_8UC1);
  CV_Assert(labels.type() == CV_32SC1);
  std::map<int, cv::Point> label2position;
  for (int i = 0; i < linearMap.rows; ++i)
  {
    for (int j = 0; j < linearMap.cols; ++j)
    {
      if (linearMap.at<uchar>(i, j) != 0)
      {
        //TODO: singal error if the label already exists
        label2position[labels.at<int>(i, j)] = cv::Point(j, i);
      }
    }
  }

  orientationIndices.create(edges.size(), CV_32SC1);
  orientationIndices = -1;

  normals.create(edges.size(), CV_32FC2);
  normals = Scalar::all(std::numeric_limits<float>::quiet_NaN());
  for (int i = 0; i < edges.rows; ++i)
  {
    for (int j = 0; j < edges.cols; ++j)
    {
//      if (edges.at<uchar>(i, j) != 0)
      cv::Point nearestEdgelPosition = label2position[labels.at<int>(i, j)];
      normals.at<Vec2f>(i, j) = linearMapNormals.at<Vec2f>(nearestEdgelPosition);
      orientationIndices.at<int>(i, j) = linearMapOrientationIndices.at<int>(nearestEdgelPosition);
      CV_Assert(orientationIndices.at<int>(i, j) >= 0 && orientationIndices.at<int>(i, j) < directionsCount);
    }
  }

/*
  Mat vis(orientations.size(), CV_8UC1, Scalar(0));
  for (int i = 0; i < orientations.rows; ++i)
  {
    for (int j = 0; j < orientations.cols; ++j)
    {
      Vec2f elem = orientations.at<Vec2f>(i, j);
      vis.at<uchar>(i, j) = (cvIsNaN(elem[0]) || cvIsNaN(elem[1])) ? 0 : 255;
    }
  }
  imshow("final or", vis);
  waitKey();
*/
}