線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1501
推到 Plurk!
推到 Facebook!

取多個灰階值的圖片運算 如何避免重複的程式碼

尚未結案
silverfoxkkk
一般會員


發表:6
回覆:12
積分:8
註冊:2004-11-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-12-04 18:11:10 IP:140.117.xxx.xxx 未訂閱
您好:     目前小弟在作有關影像處理的程式,在作取出灰階值運算時, 要作出深度為256、128、64、32、16、8、4、2…等各種變化, 程式方面如果是作各別計算的話是沒問題,而它的pixel的值, 在各個image上也是只要作些微的更改,其它的話都是相同的程式碼, 想請教前輩不知是否有方法可以使原本要用8組程式碼的程式, 可以使用一組就解決,以下列出取深度為256的程式碼, 程式碼如果寫得不好,也請多多包涵、指教。
   for(int line=0;lineHeight;line  ){
      TRGBTriple *ptr=(TRGBTriple*)Bmp->ScanLine[line];
      for(int pixel=0;pixelWidth;pixel  ){
         grayvalue=0.3*(float)R[line][pixel] 0.3*(float)G[line][pixel] 0.4*(float)B[line][pixel];
         ptr[pixel].rgbtRed=grayvalue;
         ptr[pixel].rgbtGreen=grayvalue;
         ptr[pixel].rgbtBlue=grayvalue;
         rout[line][pixel]=ptr[pixel].rgbtRed;
         gout[line][pixel]=ptr[pixel].rgbtGreen;
         bout[line][pixel]=ptr[pixel].rgbtBlue;
      }
   }
發表人 - silverfoxkkk 於 2005/12/04 18:12:24 發表人 - silverfoxkkk 於 2005/12/04 18:14:53
fusung
中階會員


發表:26
回覆:169
積分:99
註冊:2003-11-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-12-04 22:30:15 IP:61.222.xxx.xxx 未訂閱
哈囉, silverfoxkkk:    除了256之外,麻煩你再列一組,我想答案呼之欲出囉!! <> <> >
------


The first step toward proving things for yourself is to understand how others have done it before!

silverfoxkkk
一般會員


發表:6
回覆:12
積分:8
註冊:2004-11-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-12-05 05:42:45 IP:140.117.xxx.xxx 未訂閱
fusung 您好:     謝謝您的熱心,以下再列出深度為128的程式碼, 大多是一樣的,除了grayvalue的值的計算不同, 而grayvalue的值,是宣告成byte grayvalue。 期待前輩的指教囉~
   for(int line=0;lineHeight;line  ){
      TRGBTriple *ptr=(TRGBTriple*)Bmp->ScanLine[line];
      for(int pixel=0;pixelWidth;pixel  ){
         grayvalue=0.3*(float)R[line][pixel] 0.3*(float)G[line][pixel] 0.4*(float)B[line][pixel];
         grayvalue=(grayvalue/2)*2;      //唯一的改變
         ptr[pixel].rgbtRed=grayvalue;
         ptr[pixel].rgbtGreen=grayvalue;
         ptr[pixel].rgbtBlue=grayvalue;
         rout[line][pixel]=ptr[pixel].rgbtRed;
         gout[line][pixel]=ptr[pixel].rgbtGreen;
         bout[line][pixel]=ptr[pixel].rgbtBlue;
      }
   }
fusung
中階會員


發表:26
回覆:169
積分:99
註冊:2003-11-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-12-05 10:34:47 IP:61.222.xxx.xxx 未訂閱
哈囉, silverfoxkkk: 我想關鍵就是你列出來的這一行:
 
grayvalue=(grayvalue/2)*2;      //唯一的改變
其中裡面的2是256/128得來的嗎? 我推測你的公式是像這樣嗎? (真糟糕,我應該再多要一組64,哈哈!) 你再 class="code"> void __fastcall transResolution(byte grayvalue, int level) { int gain; gain = 256/level; grayvalue = grayvalue/gain*gain; } The first step toward proving things for yourself is to understand how others have done it before!
------


The first step toward proving things for yourself is to understand how others have done it before!

silverfoxkkk
一般會員


發表:6
回覆:12
積分:8
註冊:2004-11-08

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-12-05 13:26:13 IP:140.117.xxx.xxx 未訂閱
fusung 您好:    您推測的公式沒錯,小弟的確是這麼想的,在這邊推測一下前輩的意思, 是指按下button後,給予byte grayvalue和int level的值, 再呼叫void __fastcall transResolution(int level)嗎? grayvalue的值每個pixel的值都不同,似乎無法先給值。 如果是的話,以下為其程式碼:
void __fastcall transResolution(int level)
{
   int gain;     //新增;
   gain = 256/level;     //新增;
   for(int line=0;lineHeight;line  ){
      TRGBTriple *ptr=(TRGBTriple*)Bmp->ScanLine[line];
      for(int pixel=0;pixelWidth;pixel  ){
         grayvalue=0.3*(float)R[line][pixel] 0.3*(float)G[line][pixel] 0.4*(float)B[line][pixel];
         grayvalue = grayvalue/gain*gain;     //新增;
         ptr[pixel].rgbtRed=grayvalue;
         ptr[pixel].rgbtGreen=grayvalue;
         ptr[pixel].rgbtBlue=grayvalue;
         rout[line][pixel]=ptr[pixel].rgbtRed;
         gout[line][pixel]=ptr[pixel].rgbtGreen;
         bout[line][pixel]=ptr[pixel].rgbtBlue;
      }
   }
}
再請教一下前輩,副函式中,有fastcall跟沒fastcall的差別為何? 也就是如果寫成 void transResolution(byte grayvalue, int level), 這樣子的話,不知道兩者間有何差異? 不瞞前輩,用呼叫副函式的方法,小弟也有想過,不過想試試別的方法, 其實是看上了botton的Events中的OnClick,想說8個botton大部分都一樣, 如果可以修改grayvalue的值,再導入第一個的bottonclick, 這樣子…,不是也挺好玩的,好像也跟呼叫副函式差不多,還是一樣? 不曉得前輩是否有看懂小天馬行空的想法,不管如何,還是謝謝前輩的指教。
fusung
中階會員


發表:26
回覆:169
積分:99
註冊:2003-11-25

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-12-05 14:56:23 IP:61.222.xxx.xxx 未訂閱
void __fastcall transResolution(int level) { -- 省略-- } 我想你寫的code應該是對的,算是實現你簡化code的想法吧! 不過,我不知道你這樣轉出來的圖片結果會不會正確, 說實在有點懷疑,建議還可以在站上找找關鍵字" >。 < class="code"> 可以的話,附上原始碼更好,我想以後還是會有人有同樣問題,謝謝。 另外,關於▁fastcall跟fastcall的差別為何? 你可參考這一篇【關於fastcall 的問題?】 http://delphi.ktop.com.tw/topic.php?topic_id=77045, 裡面有暗黑破壞神pwipwi精闢的討論,參考看看囉! 最後,你那個想法我覺得應該也是可行,建議動手試試看驗證想法! 發表人 -
------


The first step toward proving things for yourself is to understand how others have done it before!

silverfoxkkk
一般會員


發表:6
回覆:12
積分:8
註冊:2004-11-08

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-12-05 18:10:26 IP:140.117.xxx.xxx 未訂閱
感謝fusung前輩的提醒,之前的那個公式,的確是有點暇疵, 但對於大部分的灰階圖,還是能騙過人類的眼睛  ^^, 所以小弟又想了一個新的公式,雖然還是不很完美,但也盡力了, 至於…之前的想法的話,一開始就遇到難題,呼叫bottonclick副程式, 好像…不是我想得那麼簡單,或是根本沒辦法呼叫,會再試試囉, 最後再次感謝fusung前輩,提供寶貴的意見, 以下為目前的原始碼,請多多指教:
byte grayvalue;
void graylevel(int level)
{
   Graphics::TBitmap *Bmp=new Graphics::TBitmap();
   Bmp->Width=simgwidth;
   Bmp->Height=simgheight;
   Bmp->PixelFormat=pf24bit;
   TRGBTriple *ptr;
   for(int line=0;lineHeight;line  ){
      TRGBTriple *ptr=(TRGBTriple*)Bmp->ScanLine[line];
      for(int pixel=0;pixelWidth;pixel  ){
         grayvalue=0.3*(float)R[line][pixel] 0.3*(float)G[line][pixel] 0.4*(float)B[line][pixel];
         grayvalue=(grayvalue/(256/level))*(255/(level-1));
         ptr[pixel].rgbtRed=grayvalue;
         ptr[pixel].rgbtGreen=grayvalue;
         ptr[pixel].rgbtBlue=grayvalue;
      }
   }
   formmain->imgout->Picture = NULL;
   formmain->imgout->Picture->Bitmap->Assign(Bmp);
   delete Bmp;
   Bmp=NULL;
}
呼叫時,所用的原始碼:
{
   int level=256;    //有256、128、64、32、…、2等值;
   graylevel(level);
}
發表人 - silverfoxkkk 於 2005/12/05 19:14:35
系統時間:2024-05-07 14:09:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!