开发者

How to calculate the perimeter of a binary image in java?

I am trying to perform feature extraction on an image for a program I am working on, currently I can calculate the area of this but am having trouble with the perimeter.

H开发者_运维百科ow could I implement this in order to return a numeric value for the perimeter using java?

Thanks,

Tom

        Raster raster = source.getRaster();
    int perimeter = 0;
    for(int y = 0; y<source.getHeight(); y++)
        for(int x = 0; x< source.getWidth(); x++)
        {
            if(raster.getSample(x, y, 0) == 1 && ((raster.getSample(x+1, y, 0)==0) || (raster.getSample(x-1, y, 0)==0) || (raster.getSample(x, y+1, 0)==0) || (raster.getSample(x, y-1, 0)==0)))
                perimeter ++;
        }
    return perimeter;


You can use an approach similar to Matlab (described here)

A pixel is part of the perimeter if it is nonzero and it is connected to at least one zero-valued pixel

You can define various neighbourhoods for the pixels (e.g. a surrounding grid of three, or just up/down/left/right). Once you've filtered the image, counting the pixels out to give you a value.


I don't have an example of this and making one could take a while, but you could take a look at the marching squares -algorithm.

Edit: here's an implementation of Marching Squares written in Java by Tom Gibara.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜