轉灰階疑問 |
尚未結案
|
williams8807
一般會員 發表:40 回覆:37 積分:15 註冊:2003-11-22 發送簡訊給我 |
|
TheMoon
中階會員 發表:17 回覆:95 積分:67 註冊:2002-06-05 發送簡訊給我 |
因為不知道您將彩色影像轉灰階影像的程式是如何作的,所以我猜想:
可能是你要用來儲存灰階影像的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 發送簡訊給我 |
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 發送簡訊給我 |
給點建議:
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 發送簡訊給我 |
|
TheMoon
中階會員 發表:17 回覆:95 積分:67 註冊:2002-06-05 發送簡訊給我 |
小弟按照上述作法寫了一個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; j
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |