C語言版二值圖像統(tǒng)計連通區(qū)域
連通區(qū)標記是最基本的圖像處理算法之一。該算法中,按從左至右、從上至下的順序,對整幅圖像進行掃描,通過比較每個前景像素的鄰域進行連通區(qū)標記,并創(chuàng)建等效標記列表。最后,合并等效標記列表,并再次掃描圖像以更新標記。算法的優(yōu)點的是通俗易懂,缺點是需要兩次掃描圖像,效率不高。
區(qū)域生長法利用區(qū)域生長的思想,一次生長過程可以標記一整個連通區(qū),只需對圖像進行一次掃描就能標記出所有連通區(qū)。算法描述如下:
輸入待標記圖像bitmap,初始化一個與輸入圖像同樣尺寸的標記矩陣labelmap,一個隊列queue以及標記計數(shù)labelIndex;按從左至右、從上至下的順序掃描bitmap,當(dāng)掃描到一個未被標記的前景像素p時,labelIndex加1,并在labelmap中標記p(相應(yīng)點的值賦為labelIndex),同時,掃描p的八鄰域點,若存在未被標記的前景像素,則在labelmap中進行標記,并放入queue中,作為區(qū)域生長的種子;當(dāng)queue不為空時,從queue中取出一個生長種子點p1,掃描p1的八鄰域點,若存在未被標記過的前景像素,則在labelmap中進行標記,并放入queue中;重復(fù)3直至queue為空,一個連通區(qū)標記完成;轉(zhuǎn)到2,直至整幅圖像被掃描完畢,得到標記矩陣labelmap和連通區(qū)的個數(shù)labelIndex。
該算法最壞情況下,將對每個像素點都進行一次八鄰域搜索,算法復(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í)有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法