使用OpenCV丢弃单元格图像中的异常SIFT关键点

Mat*_*teo 6 c++ opencv computer-vision sift

我正在接近生物信息学的任务,需要从一些细胞图像中提取一些特征.

我使用SIFT算法提取图像内部的关键点,如图所示.

在此输入图像描述

你也可以在图片中看到(用红色圈出),一些关键点是异常值,我不想计算它们的任何特征.

cv::KeyPoint用以下代码获得了向量:

const cv::Mat input = cv::imread("/tmp/image.jpg", 0); //Load as grayscale

cv::SiftFeatureDetector detector;
std::vector<cv::KeyPoint> keypoints;
detector.detect(input, keypoints);
Run Code Online (Sandbox Code Playgroud)

但是我想从vector所有这些关键点中丢弃,例如,在图像中以它们为中心的某个感兴趣区域(ROI)内有少于3个关键点.

因此,我需要实现一个函数,返回作为输入给定的某个ROI内的关键点数:

int function_returning_number_of_key_points_in_ROI( cv::KeyPoint, ROI );
   //I have not specified ROI on purpose...check question 3
Run Code Online (Sandbox Code Playgroud)

我有三个问题:

  1. 是否有任何现有功能做类似的事情?
  2. 如果没有,你可以帮我理解如何自己实施吗?
  3. 您是否会使用循环或矩形ROI执行此任务?您将如何在输入中指定它?

注意:

我忘了指定我想要一个有效的函数实现,即检查每个关键点所有其他关键点相对于它的相对位置将不是一个好的解决方案(如果存在另一种方式).

mev*_*ron 8

我决定采用统计路线,但如果您有多个单元格,这可能不起作用.

我的解决方案相当简单:

  1. 计算关键点位置
  2. 找到关键点空间位置的质心
  3. 计算所有点到质心的欧几里德距离
  4. 过滤原始关键点 distance < mu + 2*sigma

这是我使用此算法得到的图像(关键点==绿色,质心==红色):

在此输入图像描述

最后,这是我如何做到的代码示例:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>

#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

void distanceFromCentroid(const vector<Point2f>& points, Point2f centroid, vector<double>& distances)
{
    vector<Point2f>::const_iterator point;
    for(point = points.begin(); point != points.end(); ++point)
    {
        double distance = std::sqrt((point->x - centroid.x)*(point->x - centroid.x) + (point->y - centroid.y)*(point->y - centroid.y));
        distances.push_back(distance);
    }
}

int main(int argc, char* argv[])
{
    Mat input = imread("cell.jpg", 0); //Load as grayscale

    SiftFeatureDetector detector;
    vector<cv::KeyPoint> keypoints;
    detector.detect(input, keypoints);

    vector<Point2f> points;
    vector<KeyPoint>::iterator keypoint;
    for(keypoint = keypoints.begin(); keypoint != keypoints.end(); ++keypoint)
    {
        points.push_back(keypoint->pt);
    }

    Moments m = moments(points, true);
    Point2f centroid(m.m10 / m.m00, m.m01 / m.m00);

    vector<double> distances;
    distanceFromCentroid(points, centroid, distances);

    Scalar mu, sigma;
    meanStdDev(distances, mu, sigma);

    cout << mu.val[0] << ", " << sigma.val[0] << endl;

    vector<KeyPoint> filtered;
    vector<double>::iterator distance;
    for(size_t i = 0; i < distances.size(); ++i)
    {
        if(distances[i] < (mu.val[0] + 2.0*sigma.val[0]))
        {
            filtered.push_back(keypoints[i]);
        }
    }

    Mat out = input.clone();
    drawKeypoints(input, filtered, out, Scalar(0, 255, 0));

    circle(out, centroid, 7, Scalar(0, 0, 255), 1);

    imshow("kpts", out);
    waitKey();

    imwrite("statFilter.png", out);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!

  • 真棒!很高兴你发现它很有用; 顺便说一下很酷的问题:)我在想如果你有多个单元格,你可以做一些类型的聚类操作(K-Nearest Neighbors或类似的东西)作为预处理步骤,并以这种方式单独处理它们. (2认同)