區域生長演算法 C 語言版 使用stack
適用圖片大小 256 X 256
by JK
//------------------------------

// Area_grow.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"

#include
#include
#include
#include

#define width 256
#define height 256
using namespace std;



int AreaArray[width*height] ;

//---------------------------
//bool searchArray(int *Array ,int searchValue )
// 陣列搜索
//return 0 ; 沒有找到
//-----------------------
bool searchArray(int *Array ,int searchValue )
{

for (int i=0;i< 256*256;i++ )
{
int temp= Array[i];
if (temp!=NULL)
{
if (temp==searchValue)
{
return 1 ;
}

//char str[10];
//itoa(temp,str,10);
//printf(str);

}
}

return 0 ; //沒有找到
}
//-------------------------------------
typedef unsigned char byte;
int A_i =0;
void search4direction(int seed , byte *photo , stack *stack_temp , int* stack_Area )
{
//-----------局域生長-------------



int N = 256 ;


int v1_seed = photo[seed] ;
int seed_i = seed % N ;
int seed_j = seed / N ;
int err = 3;

if( (seed_i < 255) && (seed_i > 0) && (seed_j < 255) && (seed_j > 0) )
{
// '------檢查上下左右--------------------

int new_index = (seed_j + 1) * N + seed_i;
int v_up = photo[new_index ];

if( ! searchArray(AreaArray,new_index) ) // ' 只處理在紀錄本裡沒有的點
{
if ((v_up - v1_seed) < err)
{
AreaArray[A_i] =new_index ;// '紀錄生長位置 ' 紀錄本存下新的點
A_i++;
stack_temp->push (new_index);// '紀錄當次點
}
}

//---------------------

new_index = (seed_j ) * N + seed_i+1;
int v_right = photo[new_index ];

if( ! searchArray(AreaArray,new_index) ) // ' 只處理在紀錄本裡沒有的點
{
if ((v_right - v1_seed) < err)
{
AreaArray[A_i] =new_index ;// '紀錄生長位置 ' 紀錄本存下新的點
A_i++;
stack_temp->push (new_index);// '紀錄當次點
}
}

//------------------------------

//---------------------

new_index = (seed_j-1 ) * N + seed_i;
int v_down = photo[new_index ];

if( ! searchArray(AreaArray,new_index) ) // ' 只處理在紀錄本裡沒有的點
{
if ((v_down - v1_seed) < err)
{
AreaArray[A_i] =new_index ;// '紀錄生長位置 ' 紀錄本存下新的點
A_i++;
stack_temp->push (new_index);// '紀錄當次點
}
}

//---------------------

new_index = (seed_j ) * N + seed_i-1;
int v_left = photo[new_index ];

if( ! searchArray(AreaArray,new_index) ) // ' 只處理在紀錄本裡沒有的點
{
if ((v_left - v1_seed) < err)
{
AreaArray[A_i] =new_index ;// '紀錄生長位置 ' 紀錄本存下新的點
A_i++;
stack_temp->push (new_index);// '紀錄當次點
}
}


// '------/檢查上下左右/--------------------
}

// '-----------/局域生長/-------------




} //end void search4directio



//===============================================

//主程式區

//===============================================


int _tmain(int argc, _TCHAR* argv[])
{
unsigned char *image;
FILE *fp,*fc;
image=new unsigned char[width*height];
// 讀檔
fp=fopen("lena.raw","rb");
fread(image,sizeof(unsigned char),width*height,fp);

//-------------------
int row_i = 200;
int column_i = 128;
// '-----------局域生長-------------
stack stack_temp;
int seed_ini = 256 * column_i + row_i;

int seed = seed_ini ;
search4direction(seed, image, &stack_temp, AreaArray);
while ( stack_temp.size () != 0)
{
int new_point = stack_temp.top();//複製頂端那個值
stack_temp.pop();//吐掉頂端那個

seed = new_point;
search4direction(seed, image, &stack_temp, AreaArray);
}
//'-----------/局域生長/-------------
//---------------




//------成長區域填色-------------

for(int i=0;i




















創作者介紹
創作者 prague12 的頭像
prague12

prague12

prague12 發表在 痞客邦 留言(0) 人氣( 59 )