魔棒工具--RegionGrow算法简介
生活随笔
收集整理的這篇文章主要介紹了
魔棒工具--RegionGrow算法简介
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
from:?魔棒工具--RegionGrow算法簡介
ps里面的魔棒工具非常好用,是圖像處理中非常常用的一個(gè)工具,它現(xiàn)在已經(jīng)是我的c++工具箱中很重要的一員了,我會(huì)在以后的時(shí)間里把我的工具箱逐漸介紹給大家。 魔棒工具的核心算法是RegionGrow區(qū)域成長法,它的概念很簡單,首先在要處理的圖片上選取一個(gè)種子點(diǎn),然后以此點(diǎn)為起點(diǎn),向四周輻射形成一個(gè)區(qū)域。最初成長區(qū)域只有種子點(diǎn)這一個(gè)點(diǎn),然后不斷把周圍的點(diǎn)歸并入該成長區(qū)域,條件是該點(diǎn)的值與成長區(qū)域邊界點(diǎn)的值之差小于閾值。當(dāng)成長區(qū)域不能再繼續(xù)擴(kuò)大時(shí),算法停止。 ? ? 算法說明: 區(qū)域成長法的思想很好理解,代碼實(shí)現(xiàn)對于初學(xué)者有一定難度。對于滿足條件的像素點(diǎn),函數(shù)會(huì)把它們一個(gè)個(gè)的壓入隊(duì)列的尾部,然后從隊(duì)列的頭部一個(gè)個(gè)的取出來,形成成長區(qū)域。M是一個(gè)點(diǎn)名冊,用來記錄每一個(gè)像素是否被處理過。start和end用來記錄隊(duì)列的頭和尾,當(dāng)start==end時(shí),說明所有所有像素已經(jīng)處理完,函數(shù)結(jié)束。 ? 參數(shù)說明: src: 輸入的單通道圖像。 dst: 輸出的單通道圖像,與輸入同大小,必須提前開空間。 seedx, seedy:??種子點(diǎn)坐標(biāo) threshold:??容差 flag: 0/1 表示搜索方式是 8/4 鄰域 struct Node {int x;int y;Node* next; };void MyTreasureBox::RegionGrow(const IplImage* src, IplImage* dst, int seedx, int seedy, int threshold, bool flag) {if(!src || src->nChannels != 1)return ;int width = src->width;int height = src->height;int srcwidthstep = src->widthStep;uchar* img = (uchar*)src->imageData;//成長區(qū)域 cvZero(dst);//標(biāo)記每個(gè)像素點(diǎn)是否被計(jì)算過IplImage* M = cvCreateImage(cvSize(width, height), 8, 1);int Mwidthstep = M->widthStep;cvZero(M);M->imageData[seedy * Mwidthstep + seedx] = 1; //種子點(diǎn)位置為1,其它位置為0 CvScalar cur = CV_RGB(255,255,255);cvSet2D(dst, seedy, seedx, cur);//隊(duì)列的兩端int start = 0;int end = 1;Node *queue = new Node;queue->x = seedx;queue->y = seedy;queue->next = NULL;Node *first = queue;Node *last = queue;while (end - start > 0){int x = first->x;int y = first->y;uchar pixel = (uchar)img[y * srcwidthstep + x];for (int yy = -1; yy<=1; yy++){for (int xx = -1; xx<=1; xx++){if(flag)if ( abs(yy) && abs(xx))continue;int cx = x + xx;int cy = y + yy;if (cx >= 0 && cx <width && cy >=0 && cy < height){if (abs(img[cy * srcwidthstep + cx] - pixel) <= threshold && M->imageData[cy * Mwidthstep + cx] != 1){Node *node = new Node;node->x = cx;node->y = cy;node->next = NULL;end++;last->next = node;last = node;M->imageData[cy * Mwidthstep + cx] = 1;cvSet2D(dst, cy, cx, cur);}}}}Node* temp = first;first = first->next;delete temp;start++;}cvReleaseImage(&M); }
總結(jié)
以上是生活随笔為你收集整理的魔棒工具--RegionGrow算法简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下的静态库、动态库和动态加载库
- 下一篇: 让你的照片更鲜艳------hsv拉伸