查找本地Maxima灰度图像opencv

时间:2013-02-28 19:57:05

标签: c++ visual-c++ opencv

我正在尝试创建我的个人Blob检测算法 据我所知,我首先必须使用不同的sigma(我正在使用Mat kernel= getGaussianKernel(x,y);创建不同的高斯内核)然后获取该内核的拉普拉斯算子,然后用它过滤Image,这样我就创建了我的scalepace。现在我需要在每个结果的尺度空间图像中找到局部最大值。但我似乎找不到合适的方法......到目前为止我的代码是

vector <Point> GetLocalMaxima(const cv::Mat Src,int MatchingSize, int Threshold)
{  
    vector <Point> vMaxLoc(0); 

    if ((MatchingSize % 2 == 0) ) // MatchingSize has to be "odd" and > 0
    {
        return vMaxLoc;
    }

    vMaxLoc.reserve(100); // Reserve place for fast access 
    Mat ProcessImg = Src.clone();
    int W = Src.cols;
    int H = Src.rows;
    int SearchWidth  = W - MatchingSize;
    int SearchHeight = H - MatchingSize;
    int MatchingSquareCenter = MatchingSize/2;


    uchar* pProcess = (uchar *) ProcessImg.data; // The pointer to image Data 

    int Shift = MatchingSquareCenter * ( W + 1);
    int k = 0;

    for(int y=0; y < SearchHeight; ++y)
    { 
        int m = k + Shift;
        for(int x=0;x < SearchWidth ; ++x)
        {
            if (pProcess[m++] >= Threshold)
            {
                Point LocMax;
                Mat mROI(ProcessImg, Rect(x,y,MatchingSize,MatchingSize));
                minMaxLoc(mROI,NULL,NULL,NULL,&LocMax);
                if (LocMax.x == MatchingSquareCenter && LocMax.y == MatchingSquareCenter)
                { 
                    vMaxLoc.push_back(Point( x+LocMax.x,y + LocMax.y )); 
                    // imshow("W1",mROI);cvWaitKey(0); //For gebug              
                }
            }
        }
        k += W;
    }
    return vMaxLoc; 
}
我在this线程中找到了

,它应该返回最大值所在的点向量。它确实返回一个点矢量,但每个点的所有x和y坐标总是-17891602 ...该怎么办??? 如果你要引导我除了纠正我的代码以外的其他东西,请提供信息,因为我对opencv一无所知。我刚刚学习

2 个答案:

答案 0 :(得分:0)

这里的问题是你的LocMax点在内部循环中被声明并且从未被初始化,所以它每次都返回垃圾数据。如果您回顾StackOverflow question you linked,您会看到它们的相似变量Point maxLoc(0,0)在顶部被声明并构造为指向搜索窗口的中间。它只需要初始化一次。后续循环迭代将使用minMaxLoc函数结果替换该值。

总之,在内循环中删除此行:

Point LocMax; // delete this

在顶部附近添加一个稍微改动的版本:

vector <Point> vMaxLoc(0); // This was your original first line 
Point LocMax(0,0);  // your new second line

无论如何,这应该让你开始。

答案 1 :(得分:0)

我发现了这些人。问题是我的门槛太高了。我不明白为什么它给了我负分而不是零分,但降低了工作阈值

相关问题