C語言版二值圖像統(tǒng)計連通區(qū)域
連通區(qū)標(biāo)記是最基本的圖像處理算法之一。該算法中,按從左至右、從上至下的順序,對整幅圖像進(jìn)行掃描,通過比較每個前景像素的鄰域進(jìn)行連通區(qū)標(biāo)記,并創(chuàng)建等效標(biāo)記列表。最后,合并等效標(biāo)記列表,并再次掃描圖像以更新標(biāo)記。算法的優(yōu)點的是通俗易懂,缺點是需要兩次掃描圖像,效率不高。
區(qū)域生長法利用區(qū)域生長的思想,一次生長過程可以標(biāo)記一整個連通區(qū),只需對圖像進(jìn)行一次掃描就能標(biāo)記出所有連通區(qū)。算法描述如下:
輸入待標(biāo)記圖像bitmap,初始化一個與輸入圖像同樣尺寸的標(biāo)記矩陣labelmap,一個隊列queue以及標(biāo)記計數(shù)labelIndex;按從左至右、從上至下的順序掃描bitmap,當(dāng)掃描到一個未被標(biāo)記的前景像素p時,labelIndex加1,并在labelmap中標(biāo)記p(相應(yīng)點的值賦為labelIndex),同時,掃描p的八鄰域點,若存在未被標(biāo)記的前景像素,則在labelmap中進(jìn)行標(biāo)記,并放入queue中,作為區(qū)域生長的種子;當(dāng)queue不為空時,從queue中取出一個生長種子點p1,掃描p1的八鄰域點,若存在未被標(biāo)記過的前景像素,則在labelmap中進(jìn)行標(biāo)記,并放入queue中;重復(fù)3直至queue為空,一個連通區(qū)標(biāo)記完成;轉(zhuǎn)到2,直至整幅圖像被掃描完畢,得到標(biāo)記矩陣labelmap和連通區(qū)的個數(shù)labelIndex。
該算法最壞情況下,將對每個像素點都進(jìn)行一次八鄰域搜索,算法復(fù)雜度為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; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實現(xiàn)字符轉(zhuǎn)unix時間戳的簡單實例
下面小編就為大家?guī)硪黄狢語言實現(xiàn)字符轉(zhuǎn)unix時間戳的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06深入分析C++中執(zhí)行多個exe文件方法的批處理代碼介紹
本篇文章是對C++中執(zhí)行多個exe文件方法的批處理代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++超詳細(xì)講解隱藏私有屬性和方法的兩種實現(xiàn)方式
為了避免因為將類庫中的私有成員開放給類的使用方而導(dǎo)致的軟件邏輯外泄,因此需要將對外代碼中的私有成員隱藏起來,下面我們來了解一下隱藏私有屬性和方法的兩種實現(xiàn)方式2022-05-05C++17實現(xiàn)flyweight_factory模板類及使用示例詳解
這篇文章主要為大家介紹了C++17實現(xiàn)flyweight_factory模板類及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08