Find local maxima in grayscale image using OpenCV

前端 未结 10 2318
暗喜
暗喜 2020-11-30 04:44

Does anybody know how to find the local maxima in a grayscale IPL_DEPTH_8U image using OpenCV? HarrisCorner mentions something like that but I\'m actually not i

10条回答
  •  长情又很酷
    2020-11-30 05:46

    Actually after I posted the code above I wrote a better and very very faster one .. The code above suffers even for a 640x480 picture.. I optimized it and now it is very very fast even for 1600x1200 pic. Here is the code :

    void localMaxima(cv::Mat src,cv::Mat &dst,int squareSize)
    {
    if (squareSize==0)
    {
        dst = src.clone();
        return;
    }
    
    Mat m0;
    dst = src.clone();
    Point maxLoc(0,0);
    
    //1.Be sure to have at least 3x3 for at least looking at 1 pixel close neighbours
    //  Also the window must be x
    SANITYCHECK(squareSize,3,1);
    int sqrCenter = (squareSize-1)/2;
    
    //2.Create the localWindow mask to get things done faster
    //  When we find a local maxima we will multiply the subwindow with this MASK
    //  So that we will not search for those 0 values again and again
    Mat localWindowMask = Mat::zeros(Size(squareSize,squareSize),CV_8U);//boolean
    localWindowMask.at(sqrCenter,sqrCenter)=1;
    
    //3.Find the threshold value to threshold the image
        //this function here returns the peak of histogram of picture
        //the picture is a thresholded picture it will have a lot of zero values in it
        //so that the second boolean variable says :
        //  (boolean) ? "return peak even if it is at 0" : "return peak discarding 0"
    int thrshld =  maxUsedValInHistogramData(dst,false);
    threshold(dst,m0,thrshld,1,THRESH_BINARY);
    
    //4.Now delete all thresholded values from picture
    dst = dst.mul(m0);
    
    //put the src in the middle of the big array
    for (int row=sqrCenter;row(row,col)==0)
                continue;
            //2.the value at (row,col) is not 0 so it can be a local maxima point
            m0 =  dst.colRange(col-sqrCenter,col+sqrCenter+1).rowRange(row-sqrCenter,row+sqrCenter+1);
            minMaxLoc(m0,NULL,NULL,NULL,&maxLoc);
            //if the maximum location of this subWindow is at center
            //it means we found the local maxima
            //so we should delete the surrounding values which lies in the subWindow area
            //hence we will not try to find if a point is at localMaxima when already found a neighbour was
            if ((maxLoc.x==sqrCenter)&&(maxLoc.y==sqrCenter))
            {
                m0 = m0.mul(localWindowMask);
                                //we can skip the values that we already made 0 by the above function
                col+=sqrCenter;
            }
        }
    }
    

提交回复
热议问题