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

轉灰階疑問

尚未結案
williams8807
一般會員


發表:40
回覆:37
積分:15
註冊:2003-11-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-17 16:37:40 IP:163.28.xxx.xxx 未訂閱
我在Image1載入一個24bit全彩的圖檔,並將它轉為8bit灰階影像 再利用Image1的Image1MouseDown事件將影像部分區域擷取到Image2 但為什麼Image2的影像又變成24bit全彩影像呢? 我也試著在Image1MouseDown將影像再做一次轉換,但程式執行速度卻很慢 請問各位高手提供意見讓我解決這個問題,謝謝!
TheMoon
中階會員


發表:17
回覆:95
積分:67
註冊:2002-06-05

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-08-17 17:47:32 IP:202.39.xxx.xxx 未訂閱
因為不知道您將彩色影像轉灰階影像的程式是如何作的,所以我猜想: 可能是你要用來儲存灰階影像的TBitmap物件的PixelFormat屬性沒設成pf8bit, 設成pf8bit後要記得調色盤資訊也要設定,請參考: http://delphi.ktop.com.tw/topic.php?topic_id=29259 另外,執行速度很慢的話, 可以試試用ScanLine的方式作處理,請參考: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=53810 PS.把程式碼PO上來比較容易讓人知道問題在哪裡。
williams8807
一般會員


發表:40
回覆:37
積分:15
註冊:2003-11-22

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-08-18 16:06:04 IP:163.28.xxx.xxx 未訂閱
void __fastcall TForm1::Button1Click(TObject *Sender)    if(OpenPictureDialog1->Execute())
   {
    Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); 
   }    //-----建立灰階調色盤
  typedef struct {
    TLogPalette lPal;
    TPaletteEntry dummy[256];
  } LogPal;      LogPal SysPal;
  SysPal.lPal.palVersion = 0x300;
  SysPal.lPal.palNumEntries = 256;      for(int i=0;i<256;i  )
  {
     SysPal.lPal.palPalEntry[i].peRed   = (unsigned char)i;
     SysPal.lPal.palPalEntry[i].peGreen = (unsigned char)i;
     SysPal.lPal.palPalEntry[i].peBlue  = (unsigned char)i;
     SysPal.lPal.palPalEntry[i].peFlags = 0;  // peFlags must set to 0
  }
//////////////////////////////////////////////////////////////////      Graphics:: TBitmap *graybmp24 = new Graphics:: TBitmap();
  Graphics:: TBitmap *graybmp8 = new Graphics:: TBitmap();
  graybmp24->Assign(Image1->Picture->Bitmap);
  Byte *ptr,*ptr2;
  graybmp8->PixelFormat = pf8bit;
  graybmp8->Height = 480;
  graybmp8->Width = 640;
  graybmp8->Palette = CreatePalette(&SysPal.lPal);       for(int j=0 ; j<480 ; j  )
  {
     ptr  = (Byte *)graybmp24->ScanLine[j];
     ptr2 = (Byte *)graybmp8->ScanLine[j];         for(int i=0 ; i<640 ; i  )
        ptr2[i]  =  ptr[i*3];
  }
  Image1->Picture->Assign(graybmp8);
  delete graybmp8;
  delete graybmp24;    }    //--------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
  if (Image1->Picture->Bitmap->Empty==true)
   ShowMessage("");       Graphics:: TBitmap *bmp = new Graphics:: TBitmap();
   bmp->PixelFormat=pf8bit;   
   //加了這一行會變成256色8bit,並非灰階8bit的影像       bmp->Width=320;
   bmp->Height=240;
   SetStretchBltMode(bmp->Canvas->Handle, STRETCH_HALFTONE);
   Image1->Canvas->CopyMode=cmSrcCopy;
   bmp->Canvas->CopyMode=cmSrcCopy;
   bmp->Canvas->CopyRect(Rect(0, 0, 320, 240), Image1->Canvas,Rect(((X*bmp->Width)/160)-160, ((Y*bmp->Height)/120)-120, ((X*bmp->Width)/160) 160, ((Y*bmp->Height)/120) 120));
   Image2->Picture->Assign(bmp);
   delete bmp;
}    
ps: 灰階調色盤的建立是參考版上的資料 發表人 - williams8807 於 2004/08/18 17:09:09 發表人 - williams8807 於 2004/08/18 17:13:00
TheMoon
中階會員


發表:17
回覆:95
積分:67
註冊:2002-06-05

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-08-18 18:35:15 IP:202.39.xxx.xxx 未訂閱
給點建議:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if(OpenPictureDialog1->Execute())
  {
    Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); 
  }    
  //這段可以宣告在外面
  //-----建立灰階調色盤
  typedef struct {
    TLogPalette lPal;
    TPaletteEntry dummy[256];
  } LogPal;
    
  //這段可以放在FormCreate裡
  LogPal SysPal;
  SysPal.lPal.palVersion = 0x300;
  SysPal.lPal.palNumEntries = 256;
  for(int i=0;i<256;i  )
  {
     SysPal.lPal.palPalEntry[i].peRed   = (unsigned char)i;
     SysPal.lPal.palPalEntry[i].peGreen = (unsigned char)i;
     SysPal.lPal.palPalEntry[i].peBlue  = (unsigned char)i;
     SysPal.lPal.palPalEntry[i].peFlags = 0;  // peFlags must set to 0
  }
      Graphics:: TBitmap *graybmp24 = new Graphics:: TBitmap();
  Graphics:: TBitmap *graybmp8 = new Graphics:: TBitmap();
  graybmp24->Assign(Image1->Picture->Bitmap);
  Byte *ptr,*ptr2;
  graybmp8->PixelFormat = pf8bit;
  graybmp8->Height = 480;
  graybmp8->Width = 640;
  graybmp8->Palette = CreatePalette(&SysPal.lPal);      for(int j=0 ; j<480 ; j  )
  {
     ptr  = (Byte *)graybmp24->ScanLine[j];
     ptr2 = (Byte *)graybmp8->ScanLine[j];         //這種作法是取R,G,B三個plane中某個plane作為灰階影像資料的來源
     //與一般彩色轉灰階的作法不一樣
     for(int i=0 ; i<640 ; i  )
        ptr2[i]  =  ptr[i*3];
     
  }
  Image1->Picture->Assign(graybmp8);
  delete graybmp8;
  delete graybmp24;
}    //--------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
  if(Image1->Picture->Bitmap->Empty==true)
    ShowMessage("");      Graphics:: TBitmap *bmp = new Graphics:: TBitmap();
  bmp->PixelFormat=pf8bit;   
  //加了這一行會變成256色8bit,並非灰階8bit的影像      //試試設定其調色盤,看看有沒有效
  bmp->Palette = CreatePalette(&SysPal.lPal);
        bmp->Width=320;
  bmp->Height=240;
  SetStretchBltMode(bmp->Canvas->Handle, STRETCH_HALFTONE);
  Image1->Canvas->CopyMode=cmSrcCopy;
  bmp->Canvas->CopyMode=cmSrcCopy;
  bmp->Canvas->CopyRect(Rect(0, 0, 320, 240), Image1->Canvas,Rect(((X*bmp->Width)/160)-160, ((Y*bmp->Height)/120)-120, ((X*bmp->Width)/160) 160, ((Y*bmp->Height)/120) 120));
  Image2->Picture->Assign(bmp);
  delete bmp;
}
williams8807
一般會員


發表:40
回覆:37
積分:15
註冊:2003-11-22

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-08-21 19:53:31 IP:163.28.xxx.xxx 未訂閱
我試過您的方法,但還是不行
TheMoon
中階會員


發表:17
回覆:95
積分:67
註冊:2002-06-05

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-08-23 17:08:25 IP:202.39.xxx.xxx 未訂閱
小弟按照上述作法寫了一個Sample Code, 可以正常Work, 試試看吧 < class="code"> struct LogPal { TLogPalette lPal; TPaletteEntry dummy[256]; }; LogPal GrayPalette; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { //設定灰階調色盤 GrayPalette.lPal.palVersion=0x300; GrayPalette.lPal.palNumEntries=256; for(int i=0;i<256;i ) { GrayPalette.lPal.palPalEntry[i].peRed = (unsigned char)i; GrayPalette.lPal.palPalEntry[i].peGreen = (unsigned char)i; GrayPalette.lPal.palPalEntry[i].peBlue = (unsigned char)i; GrayPalette.lPal.palPalEntry[i].peFlags = 0; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { Img1->Picture->LoadFromFile("Lena256c.bmp"); Graphics::TBitmap *bmp24 = new Graphics::TBitmap(); bmp24->Assign(Img1->Picture->Bitmap); Graphics::TBitmap *bmp8 = new Graphics::TBitmap(); bmp8->PixelFormat = pf8bit; bmp8->Height = Img1->Height; bmp8->Width = Img1->Width; bmp8->Palette = CreatePalette(&GrayPalette.lPal); Byte *ptr1,*ptr2; for(int j=0; jHeight; j ) { ptr1 = (Byte *)bmp24->ScanLine[j]; ptr2 = (Byte *)bmp8->ScanLine[j]; for(int i=0 ; iWidth ; i ) ptr2[i] = (int)((ptr1[i*3] ptr1[i*3 1] ptr1[i*3 2])/3); } Img3->Picture->Assign(bmp8); delete bmp24; delete bmp8; } //--------------------------------------------------------------------------- void __fastcall TForm1::Img1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { Graphics::TBitmap *bmp = new Graphics::TBitmap(); bmp->PixelFormat = pf8bit; bmp->Width = 128; bmp->Height = 128; bmp->Palette = CreatePalette(&GrayPalette.lPal); bmp->Canvas->CopyRect(Rect(0, 0, 128, 128), Img1->Canvas, Rect(X-64, Y-64, X 63, Y 63)); Img2->Picture->Assign(bmp); delete bmp; } //---------------------------------------------------------------------------
系統時間:2024-05-20 21:15:15
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!