Ask Your Question
0

How to create my own filter to detect a pixel is a local maximum?

asked 2014-02-23 23:43:16 -0600

fbr gravatar image

Hello,

I am interested into doing this from the Python cv2 wrapper of OpenCV.

I'd like to test each pixel of a gray image to see if it has a value >= to the value of its 9 neighbors.

The output should be a binary mask, preferably of the same size than the input image.

Thanks, F.

edit retag flag offensive close merge delete

Comments

You may need to see minMaxLoc()

Haris gravatar imageHaris ( 2014-02-24 00:31:38 -0600 )edit

I am looking for something local, not global.

fbr gravatar imagefbr ( 2014-02-24 01:46:57 -0600 )edit
1

You can set ROI of your kernel size and perform minMaxLoc()

Haris gravatar imageHaris ( 2014-02-24 02:02:14 -0600 )edit

That sound highly unefficient. A dumb way would be to test explicitely all the neighbor pixels. I'll try that since I don't have a better idea.

fbr gravatar imagefbr ( 2014-02-24 02:31:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-02-24 03:29:11 -0600

kbarni gravatar image

You might simply try to implement it yourself, without opencv functions:

Mat image, result;
    for(y=1;y<image.rows-1;y++)
        for(x=1;x<image.cols-1;x++)
            if((image.at<uchar>(x,y)>image.at<uchar>(x-1,y-1))&&(image.at<uchar>(x,y)>image.at<uchar>(x,y-1))&&(image.at<uchar>(x,y)>image.at<uchar>(x+1,y-1))&&(image.at<uchar>(x,y)>image.at<uchar>(x+1,y))&&(image.at<uchar>(x,y)>image.at<uchar>(x+1,y+1))&&(image.at<uchar>(x,y)>image.at<uchar>(x,y+1))&&(image.at<uchar>(x,y)>image.at<uchar>(x-1,y+1))&&(image.at<uchar>(x,y)>image.at<uchar>(x-1,y)))mask.at<uchar>(x,y)=1;
            else mask.at<uchar>(x,y)=0;

Sorry for the C code, it's untested and not optimized, but it's easy to understand: you take every pixel (x,y) (except at the border), and you compare it to the 8 neighbors (x-1,y-1 ; x,y-1 ; x+1,y-1 ; ...). If it's bigger, than it's a local maximum.

You can adapt and optimize the code according to your needs. With a little work you can process the borders, too.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-02-23 23:43:16 -0600

Seen: 1,369 times

Last updated: Feb 24 '14