3

こんにちは、私はコンピューター ビジョン プロジェクトに取り組んでおり、カメラから openCV/C++ を使用して正方形を検出しようとしています。ソース コードを openCV ライブラリからダウンロードしましたが、fps がかなり落ちているようです。この問題を解決する方法を知っている人はいますか? 以下に私のテストに関するビデオ リンクがあります

これがコードで、別の投稿で見つけたばかりです:

void find_squares(Mat& image, vector<vector<Point> >& squares)
{
// blur will enhance edge detection
Mat blurred(image);
medianBlur(image, blurred, 9);

Mat gray0(blurred.size(), CV_8U), gray;
vector<vector<Point> > contours;

// find squares in every color plane of the image
for (int c = 0; c < 3; c++)
{
    int ch[] = {c, 0};
    mixChannels(&blurred, 1, &gray0, 1, ch, 1);

    // try several threshold levels
    const int threshold_level = 2;
    for (int l = 0; l < threshold_level; l++)
    {
        // Use Canny instead of zero threshold level!
        // Canny helps to catch squares with gradient shading
        if (l == 0)
        {
            Canny(gray0, gray, 10, 20, 3); // 

            // Dilate helps to remove potential holes between edge segments
            dilate(gray, gray, Mat(), Point(-1,-1));
        }
        else
        {
                gray = gray0 >= (l+1) * 255 / threshold_level;
        }

        // Find contours and store them in a list
        findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        // Test contours
        vector<Point> approx;
        for (size_t i = 0; i < contours.size(); i++)
        {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation
                if (approx.size() == 4 &&
                        fabs(contourArea(Mat(approx))) > 1000 &&
                        isContourConvex(Mat(approx)))
                {
                        double maxCosine = 0;

                        for (int j = 2; j < 5; j++)
                        {
                                double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                                maxCosine = MAX(maxCosine, cosine);
                        }

                        if (maxCosine < 0.3)
                                squares.push_back(approx);
                }
        }
    }
}

}

4

2 に答える 2

3

精度が落ちても構わない場合は、速度を上げることができます。例えば

// find squares in every color plane of the image
for (int c = 0; c < 3; c++)

3 つのカラー プレーンをループしています。1 つの色 (イメージがグレースケールであるかのように) を調べるだけで、速度が 3 倍になります。

また、非常に遅い Canny なしで試してください。use_canny パラメータを設定し、

 if (l == 0 && use_canny)
     {
        Canny(gray0, gray, 10, 20, 3); // 

ある場合とない場合を比較します。かなり速く、許容できる結果が得られています。

于 2013-01-08T21:48:36.387 に答える
1

コンピュータ ビジョンの優れた経験則は、集中的な処理を行う前に画像をグレースケールに変換することです。どうしても必要な場合にのみ、カラー チャンネルをループしてください。オブジェクト認識には次のパターンをお勧めします。

  1. 画像をグレースケールに変換
  2. グレースケール イメージをより単純な形式 (キャニー、しきい値、エッジ検出) にフィルター処理します。
  3. 重い処理をする(四角形を検出する)
  4. 処理された値で元の画像を再構築します(正方形を描画/保存します)

すべてのフレームに対してこれらすべての手順を実行していることを忘れないでください。そのため、不要なものはすべて削除してください。このコードは非常に頻繁に実行されるため、わずかな最適化でもパフォーマンスが大幅に向上することがわかります。そのため、最適化に時間を費やす価値があります。

于 2013-01-08T22:19:51.753 に答える