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

請問如何修改程式碼使的灰階有好幾個層次呢?...

尚未結案
deh3215
一般會員


發表:3
回覆:4
積分:1
註冊:2005-10-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-10-26 12:58:47 IP:140.117.xxx.xxx 未訂閱
//---------------------------------------------------------------------------    #include  #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { OrgBitmap= new Graphics::TBitmap();//這裡已經宣告,但COMPILE會出現 Undefined OrgBitmap的句子... } //------------------------------------------------------------------- void __fastcall TForm1::N2K1Click(TObject *Sender) { Graphics::TBitmap *TheBitmap,*TempBitmap; Byte *ptr, *tptr; double sum; unsigned int red,green,blue; LOGPALETTE MyPalette; PALETTEENTRY pal[1]; HPALETTE MyPaletteHandle; //---------------------- TheBitmap=Image1->Picture->Bitmap; //---------------------- TempBitmap=new Graphics::TBitmap(); TempBitmap->Width=TheBitmap->Width; TempBitmap->Height=TheBitmap->Height; TempBitmap->PixelFormat=pf8bit; //---------------------- MyPalette.palVersion=0x300; MyPalette.palNumEntries=256; MyPaletteHandle=CreatePalette(&MyPalette); for(int i=0;i<256;i ) { pal[0].peRed=i; pal[0].peGreen=i; pal[0].peBlue=i; SetPaletteEntries(MyPaletteHandle,i,1,pal); } TempBitmap->Palette=MyPaletteHandle; //-------------------------------- for(int y=0;yHeight;y ) { ptr=(Byte*)TheBitmap->ScanLine[y]; tptr=(Byte*)TempBitmap->ScanLine[y]; for(int x=0;xWidth;x ) { GetPaletteEntries(TheBitmap->Palette,ptr[x],1,pal); red=pal[0].peRed;green=pal[0].peGreen;blue=pal[0].peBlue; sum=red*0.299 green*0.587 blue*0.114; if(sum>255) sum=255; if(sum<0) sum=0; tptr[x]=(Byte)sum; } } //----------------------- TheBitmap->Assign(TempBitmap); Repaint(); OrgBitmap->Assign(TheBitmap); delete TempBitmap; } 上述這段程式碼是把彩色的影像轉成灰階,那麼應該如何修改使得灰階變成多層次的呢?如2 level,4 level,8level...等,還有一個問題已經在前面宣告了Orgbitmap,為何compile還會出現Undefined Orgbitmap.呢....煩請各位幫忙解答..3Q
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-10-26 14:12:09 IP:210.68.xxx.xxx 未訂閱
您好:    PO程式碼的方式與版規說明請參考下面連結,煩請修改謝謝您的配合 >
husser123
一般會員


發表:14
回覆:10
積分:4
註冊:2005-04-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-10-26 17:04:16 IP:202.145.xxx.xxx 未訂閱
引言:
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
Graphics::TBitmap *OrgBitmap= new Graphics::TBitmap();//這 裡已經宣告,但COMPILE會出現Undefined OrgBitmap的句子...
  
}
//-------------------------------------------------------------------
void __fastcall TForm1::N2K1Click(TObject *Sender)
{
Graphics::TBitmap *TheBitmap,*TempBitmap;
Byte *ptr, *tptr;
double sum;
unsigned int red,green,blue;
LOGPALETTE MyPalette;
PALETTEENTRY pal[1];
HPALETTE MyPaletteHandle;
//----------------------
TheBitmap=Image1->Picture->Bitmap;
//----------------------
TempBitmap=new Graphics::TBitmap();
TempBitmap->Width=TheBitmap->Width;
TempBitmap->Height=TheBitmap->Height;
TempBitmap->PixelFormat=pf8bit;
//----------------------
MyPalette.palVersion=0x300;
MyPalette.palNumEntries=256;
MyPaletteHandle=CreatePalette(&MyPalette);
for(int i=0;i<256;i  )
{
 pal[0].peRed=i;
 pal[0].peGreen=i;
 pal[0].peBlue=i;
 SetPaletteEntries(MyPaletteHandle,i,1,pal);
}
TempBitmap->Palette=MyPaletteHandle;
//--------------------------------
for(int y=0;yHeight;y  )
{
ptr=(Byte*)TheBitmap->ScanLine[y];
tptr=(Byte*)TempBitmap->ScanLine[y];
for(int x=0;xWidth;x  )
 {
 GetPaletteEntries(TheBitmap->Palette,ptr[x],1,pal);
 red=pal[0].peRed;green=pal[0].peGreen;blue=pal[0].peBlue;
 sum=red*0.299 green*0.587 blue*0.114;
 if(sum>255)
  sum=255;
 if(sum<0)
  sum=0;
  
  //  以128二值化
  if(sum>=128)
    sum=255;
  else 
    sum=0;
  
  tptr[x]=(Byte)sum;
 }
}
//-----------------------
    TheBitmap->Assign(TempBitmap);
    Repaint();
    OrgBitmap->Assign(TheBitmap);
    delete TempBitmap;
}
上述這段程式碼是把彩色的影像轉成灰階,那麼應該如何修改使得灰階變成多層次的呢?如2 level,4 level,8level...等,還有一個問題已經在前面宣告了Orgbitmap,為何compile還會出現Undefined Orgbitmap.呢....煩請各位幫忙解答..3Q
至於2階4階...,if多用幾個吧!
deh3215
一般會員


發表:3
回覆:4
積分:1
註冊:2005-10-22

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-10-26 20:24:10 IP:140.117.xxx.xxx 未訂閱
首先感謝husser123兄的回覆,多層次的灰階部分應該沒問題了,不過OrgBitmap的部分還是怪怪的...另外對發表新主題的部分,小第下次會改進,請版主海涵. #include  #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Graphics::TBitmap *OrgBitmap= new Graphics::TBitmap(); } . . 略 . . //------------------------------------------------------------------- void __fastcall TForm1::N2K1Click(TObject *Sender) { Graphics::TBitmap *TheBitmap,*TempBitmap,*OrgBitmap;//紅色部分是小弟自己加的 Byte *ptr, *tptr; double sum; unsigned int red,green,blue; LOGPALETTE MyPalette; PALETTEENTRY pal[1]; HPALETTE MyPaletteHandle; //---------------------- TheBitmap=Image1->Picture->Bitmap; //---------------------- TempBitmap=new Graphics::TBitmap(); TempBitmap->Width=TheBitmap->Width; TempBitmap->Height=TheBitmap->Height; TempBitmap->PixelFormat=pf8bit; //---------------------- MyPalette.palVersion=0x300; MyPalette.palNumEntries=256; MyPaletteHandle=CreatePalette(&MyPalette); for(int i=0;i<256;i ) { pal[0].peRed=i; pal[0].peGreen=i; pal[0].peBlue=i; SetPaletteEntries(MyPaletteHandle,i,1,pal); } TempBitmap->Palette=MyPaletteHandle; //-------------------------------- for(int y=0;yHeight;y ) { ptr=(Byte*)TheBitmap->ScanLine[y]; tptr=(Byte*)TempBitmap->ScanLine[y]; for(int x=0;xWidth;x ) { GetPaletteEntries(TheBitmap->Palette,ptr[x],1,pal); red=pal[0].peRed;green=pal[0].peGreen;blue=pal[0].peBlue; sum=red*0.299 green*0.587 blue*0.114; if(sum>255) sum=255; if(sum<0) sum=0; tptr[x]=(Byte)sum; } } //----------------------- TheBitmap->Assign(TempBitmap); Repaint(); OrgBitmap->Assign(TheBitmap); delete TempBitmap; } compille會出現[C Warning] Unit1.cpp(16): W8004 'OrgBitmap' is assigned a value that is never used.... 不知道哪裡出了問題??
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-10-27 00:13:39 IP:218.168.xxx.xxx 未訂閱
再次提醒您,若不遵守恕刪除    PO程式碼的方式與版規說明請參考下面連結,煩請修改謝謝您的配合 >
husser123
一般會員


發表:14
回覆:10
積分:4
註冊:2005-04-17

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-10-27 12:30:02 IP:202.145.xxx.xxx 未訂閱
1.compille出現[C Warning],還是可以run 2.哪裡出問題? Unit1.cpp(16): W8004 'OrgBitmap' is assigned a value that is never used.... 它不是很清楚的說明了嗎?
deh3215
一般會員


發表:3
回覆:4
積分:1
註冊:2005-10-22

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-10-27 17:08:04 IP:140.117.xxx.xxx 未訂閱
這個訊息似乎是指讀取到不該讀的記憶體區塊....灰階的效果是出來了.. 不過這個問題始終無法解決..小弟再試試看好了..多謝husser123的回覆..
系統時間:2024-05-10 6:59:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!