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

請問一下灰階圖檔的問題

尚未結案
ayuen
一般會員


發表:19
回覆:34
積分:10
註冊:2003-07-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-21 15:01:11 IP:219.68.xxx.xxx 未訂閱
把一張灰階512*512圖檔(Image1)選取出256*256的區域放在Image2 但是兩張圖的同一點的灰階值竟然不同 且在complement(反白)的時候只做到四分之一寬度的圖 如果把寬度改成1024則可以將整張256*256的圖反白 唉~一直找不出原因 大家幫幫我吧~~謝謝摟 --------------------Unit.h------------------------ public:                // User declarations         __fastcall TForm1(TComponent* Owner);         int StartX,StartY, EndX, EndY;         int TempW;         bool Masking,Pasting,Pasting_Enable;         bool Ok;         Graphics::TBitmap *OrgBitmap,*PasteBitmap;         Graphics::TBitmap *TheBitmap1; ------------------------------------------------  #include  #pragma hdrstop #include #include "Unit1.h" #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { OrgBitmap= new Graphics::TBitmap(); PasteBitmap= new Graphics::TBitmap(); Pasting=false; ScaleFactor=1.0; } //--------------------------------------------------------------------------- void __fastcall TForm1::Open1Click(TObject *Sender) { if (OpenPictureDialog1->Execute()) { Image1->AutoSize=true; Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); OrgBitmap->Assign(Image1->Picture->Bitmap); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { Graphics::TBitmap *TheBitmap; TheBitmap=Image1->Picture->Bitmap; //------------------------------------------------------------------- if (Pasting) { TheBitmap->Canvas->Draw(0,0,OrgBitmap); X=(int)X*ScaleFactor; Y=(int)Y*ScaleFactor; TheBitmap->Canvas->Draw(X-StartX,Y-StartY,PasteBitmap); TheBitmap->Canvas->Pen->Mode=pmBlack; TheBitmap->Canvas->Pen->Width=2; TheBitmap->Canvas->Brush->Style=bsClear; TheBitmap->Canvas->Rectangle(X-StartX,Y-StartY,X-StartX PasteBitmap->Width,Y-StartY PasteBitmap->Height); } lbl1->Caption="(X,Y)=(" AnsiString(Y) "," AnsiString(X) ")"; lbl2->Caption="RGB=(" AnsiString(GetRValue(Image1->Canvas->Pixels[X][Y])) "," AnsiString(GetGValue(Image1->Canvas->Pixels[X][Y])) "," AnsiString(GetBValue(Image1->Canvas->Pixels[X][Y])) ")"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { Graphics::TBitmap *TheBitmap; StartX=(int)X*ScaleFactor; StartY=(int)Y*ScaleFactor; if(Pasting_Enable) { Pasting=true; Pasting_Enable=false; } else { Pasting=false; TheBitmap=Image1->Picture->Bitmap; TheBitmap->Canvas->Draw(0,0,OrgBitmap); } Ok=true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { Graphics::TBitmap *TheBitmap,*TheBitmap1,*TempBitmap; int Width, Height; int TempW; int TFX, TFY, BRX, BRY; TRect SrcRect, DestRect; EndX=(int)X*ScaleFactor; EndY=(int)Y*ScaleFactor; if (Pasting) { TheBitmap=Image1->Picture->Bitmap; TheBitmap->Canvas->Draw(0,0,OrgBitmap); TheBitmap->Canvas->Draw(EndX-StartX,EndY-StartY,PasteBitmap); OrgBitmap->Assign(TheBitmap); } Pasting=false; if(Ok) { TheBitmap=Image1->Picture->Bitmap; TheBitmap->Canvas->Draw(0,0,OrgBitmap); TempBitmap=new Graphics::TBitmap(); Width = abs(EndX - StartX); Height = abs(EndY - StartY); TempW=max(Width,Height); if(TempW<=256) {TempW=256; Width=256; Height=256;} TFX= min(EndX, StartX); BRX=TFX Width; TFY= min(EndY, StartY); BRY=TFY Height; TempBitmap->Width =Width; TempBitmap->Height=Height; TempBitmap->PixelFormat=pf24bit; TempBitmap->Canvas->CopyMode=cmSrcCopy; SrcRect=Rect(TFX, TFY, BRX, BRY); DestRect=Rect(0, 0, Width, Height); TempBitmap->Canvas->CopyRect(DestRect, TheBitmap->Canvas, SrcRect); Clipboard()->Assign(TempBitmap); TheBitmap->Canvas->Pen->Style=psSolid; TheBitmap->Canvas->Pen->Mode=pmWhite; TheBitmap->Canvas->Pen->Width=1; TheBitmap->Canvas->Brush->Style=bsSolid; TheBitmap->Canvas->Brush->Style=bsClear;// TheBitmap->Canvas->Rectangle(TFX,TFY,BRX,BRY); Image2->AutoSize=true; Image2->Stretch=false; TheBitmap1=Image2->Picture->Bitmap; if(TempW<=256) { TempW=256; TheBitmap1->Width=256; TheBitmap1->Height=256; } if (Clipboard()->HasFormat(CF_BITMAP)) { PasteBitmap->Assign(Clipboard()); TheBitmap1->Canvas->Draw(0,0,PasteBitmap); TheBitmap1->Canvas->Pen->Width=2; TheBitmap1->Canvas->Pen->Mode=pmBlack; TheBitmap1->Canvas->Brush->Style=bsClear; TheBitmap1->Canvas->Rectangle(0,0,PasteBitmap->Width,PasteBitmap->Height); } }// if(Ok) Image2->Picture->SaveToFile("D:\\333.bmp"); } //--------------------------------------------------------------------------- void __fastcall TForm1::ComplementClick(TObject *Sender) { Byte *ptr; Graphics::TBitmap *TheBitmap; Image2->Picture->LoadFromFile("D:\\333.bmp"); TheBitmap=Image2->Picture->Bitmap; Image2->Picture->Bitmap->Assign(TheBitmap); for(int y=0;yHeight;y ) { ptr=(Byte*)TheBitmap->ScanLine[y]; for(int x=0;xWidth;x ) ptr[x]=(Byte)(255-ptr[x]); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Image2MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { lbl3->Caption="(X,Y)=(" AnsiString(Y) "," AnsiString(X) ")"; lbl4->Caption="RGB=(" AnsiString(GetRValue(Image1->Canvas->Pixels[X][Y])) "," AnsiString(GetGValue(Image1->Canvas->Pixels[X][Y])) "," AnsiString(GetBValue(Image1->Canvas->Pixels[X][Y])) ")"; } //--------------------------------------------------------------------------- 發表人 - ayuen 於 2003/09/21 16:50:39
brook
資深會員


發表:57
回覆:323
積分:371
註冊:2002-07-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-21 21:39:32 IP:211.76.xxx.xxx 未訂閱
void __fastcall TForm1::ComplementClick(TObject *Sender)
{
Byte *ptr;
Graphics::TBitmap *TheBitmap;
Image2->Picture->LoadFromFile("D:\\333.bmp");
TheBitmap=Image2->Picture->Bitmap;
Image2->Picture->Bitmap->Assign(TheBitmap);    // 加上這一行看看
TheBitmap->PixelFormat = pf24bit;    for(int y=0;yHeight;y  )
{ ptr=(Byte*)TheBitmap->ScanLine[y];
for(int x=0;xWidth;x  )
ptr[x]=(Byte)(255-ptr[x]);
}
 
ayuen
一般會員


發表:19
回覆:34
積分:10
註冊:2003-07-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-09-21 21:52:57 IP:219.68.xxx.xxx 未訂閱
還是不行耶 跟原本的還是一樣 不過還是謝謝你摟
JerryKuo
版主


發表:42
回覆:571
積分:322
註冊:2003-03-10

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-09-22 09:12:52 IP:61.230.xxx.xxx 未訂閱
引言: 還是不行耶 跟原本的還是一樣 不過還是謝謝你摟
程式有點長,加個註解會比較閱讀。
brook
資深會員


發表:57
回覆:323
積分:371
註冊:2002-07-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-09-22 11:01:06 IP:218.160.xxx.xxx 未訂閱
看了你的for迴路,才注意到你一次只處理1個byte,改PixelFormat為8bit. TheBitmap->PixelFormat = pf8bit; 可以讓你處理到整個劃面,不會只處理一半,至於灰化的公式 ptr[x]=(Byte)(255-ptr[x]);這部份我就不清楚了,歹勢. 發表人 - brook 於 2003/09/22 11:02:08
ayuen
一般會員


發表:19
回覆:34
積分:10
註冊:2003-07-31

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-09-22 12:05:20 IP:219.68.xxx.xxx 未訂閱
謝謝大家的幫忙 但是因為我要的是原圖是每一個pixel是pf24bit 如果把PixelFormat->pf8bit那麼圖會變的難看 如果我想把處理後的圖變成一次處理24bit的話 那我要如何修改呢 例如說在complement裡我要如何修改迴圈為一次處理24bit 而非8bit呢 因為我剛剛接觸影像處理所以有點不順手 請大家幫幫忙摟 謝謝
brook
資深會員


發表:57
回覆:323
積分:371
註冊:2002-07-12

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-09-22 13:08:00 IP:218.160.xxx.xxx 未訂閱
只要把寬乘3即可. for(int x=0;xWidth*3;x )
系統時間:2024-06-27 0:22:40
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!