Repository: opencv/opencv
File: modules/features2d/src/fast.cpp (C++)

Code before:
            unsigned score = pscore[y * stride + x];
            if (score > uthreshold)
            {
                kpt.pt.x = (float)(x);
                kpt.response = (nonmax_suppression != 0) ? (float)((int)score - 1) : 0.f;
                keypoints.push_back(kpt);
            }
        }
    }

    return CV_HAL_ERROR_OK;
}

void FAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression, FastFeatureDetector::DetectorType type)
{
    CV_INSTRUMENT_REGION();

    CV_OCL_RUN(_img.isUMat() && type == FastFeatureDetector::TYPE_9_16,
               ocl_FAST(_img, keypoints, threshold, nonmax_suppression, 10000));

    cv::Mat img = _img.getMat();
    CALL_HAL(fast_dense, hal_FAST, img, keypoints, threshold, nonmax_suppression, type);

    size_t keypoints_count = 1;
    keypoints.clear();
    uchar* kps = (uchar*)malloc(sizeof(KeyPoint) * keypoints_count);
    uchar** _kps = &kps;
    int hal_ret = cv_hal_FAST(img.data, img.step, img.cols, img.rows, _kps,
                              &keypoints_count, threshold, nonmax_suppression, type, realloc);
    if (hal_ret == CV_HAL_ERROR_OK) {
        keypoints.assign(reinterpret_cast<KeyPoint*>(*_kps), reinterpret_cast<KeyPoint*>(*_kps) + keypoints_count);
    }
    free(*_kps);
    if (hal_ret == CV_HAL_ERROR_OK) {
        return;
    }

    switch(type) {
    case FastFeatureDetector::TYPE_5_8:
        FAST_t<8>(_img, keypoints, threshold, nonmax_suppression);
        break;
    case FastFeatureDetector::TYPE_7_12:
        FAST_t<12>(_img, keypoints, threshold, nonmax_suppression);
        break;
    case FastFeatureDetector::TYPE_9_16:
        FAST_t<16>(_img, keypoints, threshold, nonmax_suppression);
        break;
    }
}



Review comment (suggestion):
you don't need extra variable. Use the following code instead:

```
    KeyPoint* kps = (KeyPoint*)malloc(sizeof(kps[0]) * keypoints_count);
    int hal_ret = cv_hal_FAST(img.data, img.step, img.cols, img.rows, (void**)&kps,
                              &keypoints_count, threshold, nonmax_suppression, type, realloc);
    if (hal_ret == CV_HAL_ERROR_OK) {
        keypoints.assign(kps, kps + keypoints_count);
    }
    free(kps);
    if (hal_ret == CV_HAL_ERROR_OK) {
        return;
    }
```

Code after:
            unsigned score = pscore[y * stride + x];
            if (score > uthreshold)
            {
                kpt.pt.x = (float)(x);
                kpt.response = (nonmax_suppression != 0) ? (float)((int)score - 1) : 0.f;
                keypoints.push_back(kpt);
            }
        }
    }

    return CV_HAL_ERROR_OK;
}

void FAST(InputArray _img, std::vector<KeyPoint>& keypoints, int threshold, bool nonmax_suppression, FastFeatureDetector::DetectorType type)
{
    CV_INSTRUMENT_REGION();

    CV_OCL_RUN(_img.isUMat() && type == FastFeatureDetector::TYPE_9_16,
               ocl_FAST(_img, keypoints, threshold, nonmax_suppression, 10000));

    cv::Mat img = _img.getMat();
    CALL_HAL(fast_dense, hal_FAST, img, keypoints, threshold, nonmax_suppression, type);

    size_t keypoints_count = 1;
    keypoints.clear();
    KeyPoint* kps = (KeyPoint*)malloc(sizeof(KeyPoint) * keypoints_count);
    int hal_ret = cv_hal_FASTv2(img.data, img.step, img.cols, img.rows, (void**)&kps,
                                &keypoints_count, threshold, nonmax_suppression, type, realloc);
    if (hal_ret == CV_HAL_ERROR_OK) {
        keypoints.assign(kps, kps + keypoints_count);
        free(kps);
        return;
    } else {
        free(kps);
        keypoints_count = 10000;
        keypoints.clear();
        keypoints.resize(keypoints_count);
        CALL_HAL(fast, cv_hal_FAST, img.data, img.step, img.cols, img.rows,
                (uchar*)(keypoints.data()), &keypoints_count, threshold, nonmax_suppression, type);
    }

    switch(type) {
    case FastFeatureDetector::TYPE_5_8:
        FAST_t<8>(_img, keypoints, threshold, nonmax_suppression);
        break;
    case FastFeatureDetector::TYPE_7_12:
        FAST_t<12>(_img, keypoints, threshold, nonmax_suppression);
        break;
    case FastFeatureDetector::TYPE_9_16:
        FAST_t<16>(_img, keypoints, threshold, nonmax_suppression);
        break;