0

OpenCV で入力画像に対して方向推定を実行しようとしています。画像の勾配を取得するために sobel 関数を使用calculateOrientationsし、向きを計算するためにインターネットで見つけた という別の関数を使用しました。

コードは次のとおりです。

void computeGradient(cv::Mat inputImg)
{
    // Gradient X
    cv::Sobel(inputImg, grad_x, CV_16S, 1, 0, 5, 1, 0, cv::BORDER_DEFAULT);
    cv::convertScaleAbs(grad_x, abs_grad_x);

    // Gradient Y
    cv::Sobel(inputImg, grad_y, CV_16S, 0, 1, 5, 1, 0, cv::BORDER_DEFAULT);
    cv::convertScaleAbs(grad_y, abs_grad_y);

    // convert from CV_8U to CV_32F
    abs_grad_x.convertTo(abs_grad_x2, CV_32F, 1. / 255);
    abs_grad_y.convertTo(abs_grad_y2, CV_32F, 1. / 255);

    // calculate orientations
    calculateOrientations(abs_grad_x2, abs_grad_y2);    
} 

void calculateOrientations(cv::Mat gradientX, cv::Mat gradientY)
{
    // Create container element
    orientation = cv::Mat(gradientX.rows, gradientX.cols, CV_32F);

    // Calculate orientations of gradients --> in degrees
    // Loop over all matrix values and calculate the accompagnied orientation
    for (int i = 0; i < gradientX.rows; i++){
        for (int j = 0; j < gradientX.cols; j++){
            // Retrieve a single value
            float valueX = gradientX.at<float>(i, j);
            float valueY = gradientY.at<float>(i, j);
            // Calculate the corresponding single direction, done by applying the arctangens function
            float result = cv::fastAtan2(valueX, valueY);
            // Store in orientation matrix element
            orientation.at<float>(i, j) = result;
        }
    }
}

ここで、取得した向きが正しいかどうかを確認する必要があります。そのために、方向マトリックスでサイズ 5x5 の各ブロックに矢印を描きたいと思います。誰かがこれに矢印を描く方法についてアドバイスしてもらえますか? ありがとうございました。

4

2 に答える 2

3

OpenCV で方向を区別する最も簡単な方法は、線の始点または終点に小さな円または正方形を描くことです。矢印の機能はありません。矢印が必要な場合は、これを書く必要があります (簡単ですが時間がかかります)。このようにしたら(openCVではありませんが、変換していただければ幸いです):

  double arrow_pos = 0.5; // 0.5 = at the center of line
  double len = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
  double co = (x2-x1)/len, si = (y2-y1)/len; // line coordinates are (x1,y1)-(x2,y2)
  double const l = 15, sz = linewidth*2; // l - arrow length 
  double x0 = x2 - len*arrow_pos*co;
  double y0 = y2 - len*arrow_pos*si;
  double x = x2 - (l+len*arrow_pos)*co;
  double y = y2 - (l+len*arrow_pos)*si;
  TPoint tp[4] = {TPoint(x+sz*si, y-sz*co), TPoint(x0, y0), TPoint(x-sz*si, y+sz*co), TPoint(x+l*0.3*co, y+0.3*l*si)};
  Polygon(tp, 3);
  Canvas->Polyline(tp, 2);

更新: OpenCV 2.4.10 および 3.0 以降に追加された arrowedLine(...) 関数

于 2015-01-15T16:51:57.787 に答える