全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:979
推到 Plurk!
推到 Facebook!

我寫了一個鍊結串列來存放每個pixel的各灰階植次數以建立背景~但是當時間一長電腦就變很慢

尚未結案
dragonhippopdc
一般會員


發表:45
回覆:51
積分:19
註冊:2005-01-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-14 22:09:36 IP:220.133.xxx.xxx 未訂閱
我寫了一個鍊結串列來存我每一秒讀進來的frame~串列放每個pixel的各個不同灰階植次數~然後再抓出每個pixel的次數最大的灰階值來當此pixel的灰階值以建立背景~但是當時間一長電腦就變很慢~好像是因為我一秒觸動一次timer而回圈太多以至於跑不完的樣子 以下是我的code  
 
int flag=0;
unsigned char *nowBuffer=new unsigned char[ImageWidth*ImageHeight];
unsigned char *beforeBuffer=new unsigned char[ImageWidth*ImageHeight];
unsigned char tableBuffer[2][10*ImageWidth]={0};
//------------------------
class Histogram
{
 public:
 unsigned char data;
 int pix;
 int c;
 Histogram *next;
};
Histogram *head=NULL;
//----------------------------------
以下為timer內的code
for (int i=105;i<115;i  )
      for (int j=0;jnext=NULL;
         p->data=tempBuffer[i*ImageWidth j];
         p->pix=i*ImageWidth j;
         p->c=1;
         head=p;
         tableBuffer[0][(p->pix)-33600]=p->data;
         tableBuffer[1][(p->pix)-33600]=p->c;
        }
        else
        {
         Histogram *h;
         h=head;
         while(h!=NULL)
         {
          if(h->data==tempBuffer[i*ImageWidth j]&&h->pix==(i*ImageWidth j))
          {
           h->c  ;
            if(tableBuffer[1][(h->pix)-33600]c)
            {
             tableBuffer[0][(h->pix)-33600]=h->data;
             tableBuffer[1][(h->pix)-33600]=h->c;
            }
            else if(tableBuffer[0][(h->pix)-33600]==h->data)
            {
             tableBuffer[1][(h->pix)-33600]=h->c;
            }
            break;
          }
          h=h->next;
         }
         if(tempBuffer[i*ImageWidth j]!=0)
         {
          Histogram *p;
          p=(Histogram *)malloc(sizeof(Histogram));
          p->data=tempBuffer[i*ImageWidth j];
          p->pix=i*ImageWidth j;
          p->c=1;
          p->next=head;
          head=p;
          if(tableBuffer[1][(p->pix)-33600]c)
          {
           tableBuffer[0][(p->pix)-33600]=p->data;
           tableBuffer[1][(p->pix)-33600]=p->c;
          }
         }
        }
      }
 delete tempBuffer;
感覺問題好像出在這邊while(h!=NULL)~因為我拿掉這個就不會有跑不動的問題~想請問各位大大是不是有其它的寫法嗎~因為這個寫法跑個幾秒電腦就會開始lag了~不過他應該是正確的~因為真的能抓出背景來~且是正確的~只不過太吃資源的樣子
stellos
中階會員


發表:24
回覆:84
積分:51
註冊:2004-06-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-15 01:02:17 IP:220.138.xxx.xxx 未訂閱
你在迴圈內malloc的記憶體空間沒有free吧 你可以Debug一下 當每次malloc時是否一直配新的記憶體空間
dragonhippopdc
一般會員


發表:45
回覆:51
積分:19
註冊:2005-01-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-15 08:27:49 IP:203.68.xxx.xxx 未訂閱
可是我是要一直建立新的結點來存一個新的pixel的灰階值及次數 而如果pixel相同且灰階值相同我才累加次數 不然都是新建一個節點來存三個資料 1.pixel 2.灰階值 3.次數 而一開始次數是放1~timer每一秒觸動一次如果以後進來的frame有pixel且灰階值跟之前所新增的節點有相同的就累加次數而不建節點 所以才要跑while(p!NULL)這個回圈 此回圈就是要重新跑一遍確定是否有無一樣的 想請問大大你指的是我建節點這個部份沒歸還嗎 這個部份應該是不能歸還的樣子~ 想請你再幫我看看是不是那裡還有問題~ 謝謝ㄛ~
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-03-17 03:47:09 IP:211.76.xxx.xxx 未訂閱
dragonhippopdc你好: linked list的尋找時間很長,加上移到下一個list還要多花一個dereference的時間。如果記憶體管理沒問題,大概就是資料結構不佳了.. 建議你用STL的map來做,速度會快很多。但以你的case來看,indexing的部份可能會難做一些...(要以pix和data當index)。先試試看吧~
dragonhippopdc
一般會員


發表:45
回覆:51
積分:19
註冊:2005-01-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-03-17 16:33:45 IP:218.175.xxx.xxx 未訂閱
pwipwi你好: 請問你所說的STL的map這個是什麼呢~ 我不懂ㄝ~我沒用過這個東西~還請你說明一下
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-03-17 20:33:09 IP:211.76.xxx.xxx 未訂閱
請參考: http://www.sgi.com/tech/stl/Map.html
dragonhippopdc
一般會員


發表:45
回覆:51
積分:19
註冊:2005-01-15

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-03-20 13:37:35 IP:220.133.xxx.xxx 未訂閱
各位大大~現在我已經解決了會變慢的問題~我是將判段重寫~並且都分成用成class但是我現在卡在不是怎麼一次歸還所有建出的結點~以下是我的code  
 
//-------------------
class node
{
 friend class list;   
 int pix;
 int data;
 int c;
 node *link;
};
// --------------------------------------
class list
{
   private:
      node *front;      
      node *mid;
   public:
      list();
      ~list();
      bool empty();
      void insert_node(int key,int value);
      void delete_node();
};
// ---------------------------------------------------
list::list(void)      // Constructor
{
   front = new node;
   mid = new node;
   front->link = NULL;
   mid->link = NULL;
}
//-------------------
list::~list(void)     // Destructor
{
   node *this_node, *temp_node;       if(front->link != NULL)
   {
      this_node = front->link;
      while(this_node->link != NULL)
      {
         temp_node = this_node;
         this_node = this_node->link;
         delete temp_node;
      }
      delete this_node;
   }
   delete front;
   delete mid;
}
// ----------------------
//    判斷是否為空串列
// ----------------------
bool list::empty(void)
{
   if(front->link == NULL)   // 空串列
      return true;
   else
      return false;
}
// --------------------------------------------------------
void list::insert_node(int key,int value)
{
   int flag=0;
   node *new_node, *prev_node, *this_node;
   new_node = new node;
   new_node->pix = key;
   new_node->data = value;
   new_node->c = 1;
   new_node->link = NULL;
   if(empty())
   {                            
      front->link = new_node;
      mid->link = new_node;
   }
   else
   {
    if(flag==0)
    {
     this_node = mid->link;
    }
    else
    {
     this_node=front;
     flag=0;
    }
    if(this_node->link==NULL)
    {
     this_node->link=new_node;
     mid->link=new_node;
    }
    else
    {
      if(value < this_node->link->data)
      {
       prev_node = this_node->link;
       this_node->link = this_node->link->link;
       return;
      }
      else if(value == this_node->link->data)
      {
       this_node->link->c  ;
       return;
      }
      else
      {
       while(this_node->link!= NULL)
       {
              if(value > this_node->link->data)
              {
               prev_node = this_node->link;
               this_node->link = this_node->link->link;
              }
              else
              {
               prev_node->link = new_node;
               new_node->link = this_node;
               return;
              }
       }
      }
    }
    if(key==33600)
    {
     flag=1;
    }
   }
}
// -----------------------------------
//    自單向鏈結串列中刪除資料(key)
// -----------------------------------
void list::delete_node(void)
{
   node *this_node, *prev_node, *temp_node;
   prev_node = front;
   this_node = front->link;
   while(this_node->link != NULL)
   {   // 當不是最後一個節點時          temp_node = this_node;
      prev_node->link = this_node->link;
      free(temp_node);
   }
   if(this_node->link == NULL)
   {   
      temp_node = this_node;
      prev_node->link = NULL;    
      front->link = prev_node;
      delete temp_node;
      return;
   }
}
我是想要一段時間就將全部list中的所以練節刪除~可是我上面的這個寫法一刪就當了~不知是那錯了~想請各位大大幫我解答一下
系統時間:2024-05-17 10:57:27
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!