日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二值图像连通 C语言,二值图像统计连通区域C语言版

發布時間:2024/8/5 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二值图像连通 C语言,二值图像统计连通区域C语言版 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.bkjia.com/Javabc/771817.html

連通區標記是最基本的圖像處理算法之一。該算法中,按從左至右、從上至下的順序,對整幅圖像進行掃描,通過比較每個前景像素的鄰域進行連通區標記,并創建等效標記列表。最后,合并等效標記列表,并再次掃描圖像以更新標記。算法的優點的是通俗易懂,缺點是需要兩次掃描圖像,效率不高。

區域生長法利用區域生長的思想,一次生長過程可以標記一整個連通區,只需對圖像進行一次掃描就能標記出所有連通區。算法描述如下:

輸入待標記圖像bitmap,初始化一個與輸入圖像同樣尺寸的標記矩陣labelmap,一個隊列queue以及標記計數labelIndex;按從左至右、從上至下的順序掃描bitmap,當掃描到一個未被標記的前景像素p時,labelIndex加1,并在labelmap中標記p(相應點的值賦為labelIndex),同時,掃描p的八鄰域點,若存在未被標記的前景像素,則在labelmap中進行標記,并放入queue中,作為區域生長的種子;當queue不為空時,從queue中取出一個生長種子點p1,掃描p1的八鄰域點,若存在未被標記過的前景像素,則在labelmap中進行標記,并放入queue中;重復3直至queue為空,一個連通區標記完成;轉到2,直至整幅圖像被掃描完畢,得到標記矩陣labelmap和連通區的個數labelIndex。

該算法最壞情況下,將對每個像素點都進行一次八鄰域搜索,算法復雜度為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;

}

總結

以上是生活随笔為你收集整理的二值图像连通 C语言,二值图像统计连通区域C语言版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。