开发者

Detecting Bifurcations and Endpoints of Binary Image using OpenCV

I want to detect bifurcations and endpoints in a binary image that I have. The image has already be开发者_运维百科en thinned. Been reading on ways of how one can possible do this, but I don't know how I could go about implementing it in OpenCV. All I know is that I have to use a 3x3 window to scan the whole image.Then this is a bifurcation if the sum of 0 pixels on the 3x3 window are less or equal 5, is a terminate branch if the sum is 7 and if is 6 is a continuous line.

First of how, I don't know how to declare a 3x3 window in OpenCV and how to access each of the 9 blocks that make up the window.

Secondly how can I then move the window over the image. Will be grateful for your help, a sample code will also do.


In order to get a 3x3 window of a matrix you can do the following

Mat image = imread("image.png", 0);
// binarize it with cv::threshold or cv::adaptiveThreshold or something else.
Rect r(0, 0, 3, 3);
// this gets a 3x3 window starting with upper left of (0, 0) pixel
Mat windowImage(image, r);

Note: That the region of interest you acquire is only a reference inside the original matrix if you want a copy you'll need to call the clone() member of the window. This has bit me a few times :D

To access a pixel within the block you can use the at() member function of the Mat class. Like this (assuming your image is of type CV_8UC1):

// block processing loops
for(int i = 0; i < windowImage.rows; i++)
{
    for(int j = 0; j < windowImage.cols; j++)
    {
        unsigned char val = windowImage.at<unsigned char>(i, j);
        // do pixel processing logic here...
    }
}

Also, note the at() member has a bit of overhead, so if you want fast look at using the data pointer to speed things up.

To move the window around, you need to shift where the rectangle region of interest is. The first two parameters of the Rect constructor take the upper-left x and y locations to start at. So you'll need to divide your image into blocks.

const int BLOCK_SIZE = 3;
int rowBlocks = image.rows / BLOCK_SIZE;
int colBlocks = image.cols / BLOCK_SIZE;    

for(int i = 0; i < rowBlocks; i++)
{
    for(int j = 0; j < colBlocks; j++)
    {
        Mat block_ij(image, Rect(i, j, BLOCK_SIZE, BLOCK_SIZE));
        // block process here...
    }
}

Note: you may need to look that the function copyMakeBorder to add a border around your image for the cases when your image dimensions are not divisible by 3.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜