Region Growth Method for marking the connected areas of Binary Images

Source: Internet
Author: User

Connectivity labeling is one of the most basic image processing algorithms. In this algorithm, the entire image is scanned from left to right, from top to bottom, and the connectivity zone is marked by comparing the neighborhoods of each foreground pixel, and an equivalent tag list is created. Finally, combine the equivalent tag list and scan the image again to update the tag. The advantage of the algorithm is that it is easy to understand, and the disadvantage is that it requires two scans of images, which is inefficient.

By using the concept of region growth, a growth process can mark an entire connected zone. You only need to scan the image once to mark all connected zones. The algorithm is described as follows:

Enter the bitmap of the image to be marked, initialize a tag matrix labelmap of the same size as the input image, a queue, and a tag count labelIndex. Scan bitmap from left to right and from top to bottom, when an unlabeled foreground pixel p is scanned, labelIndex is added with 1 and p is marked in labelmap (the value of the corresponding vertex is assigned to labelIndex, scan the eight neighboring points of p. If untagged foreground pixels exist, mark them in labelmap and put them in the queue as the seed for region growth. When the queue is not empty, extract A growth seed point p1 from queue and scan the eight neighboring points of p1. If there are unmarked foreground pixels, mark them in labelmap and put them in queue; repeat 3 until the queue is empty and a connectivity zone is marked. Go to 2 until the entire image is scanned. Then, the labelmap matrix and the number of connectivity zones labelIndex are obtained.

In the worst case, an eight-Neighbor Search is performed for each pixel. The algorithm complexity is O (n ).


typedef struct QNode{int data;struct QNode *next;}QNode;typedef struct Queue{struct QNode* first;struct QNode* last;}Queue;void PushQueue(Queue *queue, int data){QNode *p = NULL;p = (QNode*)malloc(sizeof(QNode));p->data = data;if(queue->first == NULL){queue->first = p;queue->last = p;p->next = NULL;}else{p->next = NULL;queue->last->next = p;queue->last = p;}}int PopQueue(Queue *queue){QNode *p = NULL;int data;if(queue->first == NULL){return -1;}p = queue->first;data = p->data;if(queue->first->next == NULL){queue->first = NULL;queue->last = NULL;}else{queue->first = p->next;}free(p);return data;}static int NeighborDirection[8][2] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};void SearchNeighbor(unsigned char *bitmap, int width, int height, int *labelmap,                     int labelIndex, int pixelIndex, Queue *queue){int searchIndex, i, length;labelmap[pixelIndex] = labelIndex;length = width * height;for(i = 0;i < 8;i++){searchIndex = pixelIndex + NeighborDirection[i][0] * width + NeighborDirection[i][1];if(searchIndex > 0 && searchIndex < length && bitmap[searchIndex] == 255 && labelmap[searchIndex] == 0){labelmap[searchIndex] = labelIndex;PushQueue(queue, searchIndex);}}}int ConnectedComponentLabeling(unsigned char *bitmap, int width, int height, int *labelmap){int cx, cy, index, popIndex, labelIndex = 0;Queue *queue = NULL;queue = (Queue*)malloc(sizeof(Queue));queue->first = NULL;    queue->last = NULL;memset(labelmap, 0, width * height);for(cy = 1; cy < height - 1; cy++){for(cx = 1; cx < width - 1; cx++){index = cy * width + cx;if(bitmap[index] == 255 && labelmap[index] == 0){labelIndex++;SearchNeighbor(bitmap, width, height, labelmap, labelIndex, index, queue);popIndex = PopQueue(queue);while(popIndex > -1){SearchNeighbor(bitmap, width, height, labelmap, labelIndex, popIndex, queue);popIndex = PopQueue(queue);}}}}free(queue);return labelIndex;}


For more information about Image Engineering & Computer Vision, please stay tuned to this blog and Sina Weibo songzi_tea.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.