灰階的問題 |
答題得分者是:taishyang
|
silent852
一般會員 發表:1 回覆:1 積分:0 註冊:2009-11-08 發送簡訊給我 |
[code cpp] //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- #include #include <math.h><br />#pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if (OpenPictureDialog1->Execute()) Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { if (Image2->Picture->Bitmap->Empty==true) ShowMessage("No image to save!"); else if (SavePictureDialog1->Execute()) Image2->Picture->SaveToFile(SavePictureDialog1->FileName); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { Graphics::TBitmap *Bmp = new Graphics::TBitmap(); Bmp->PixelFormat=pf24bit; Bmp->Assign(Image1->Picture->Bitmap); Byte *ptr; int r,g,b; int gray; int i,j; int threshold; for(j=0;j {ptr = (Byte *)Bmp->ScanLine[j]; for(i=0;i { b=ptr[i*3]; g=ptr[i*3 1]; r=ptr[i*3 2]; gray=(b*28 g*151 r*77)/256; ptr[i*3]=(Byte)gray; ptr[i*3 1]=(Byte)gray; ptr[i*3 2]=(Byte)gray; } } Image2->Picture->Assign(Bmp); delete Bmp ; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender) { Graphics::TBitmap *Bmp = new Graphics::TBitmap(); Byte *ptr; int gray; int i,j; int threshold; Bmp->PixelFormat=pf24bit; Bmp->Assign(Image1->Picture->Bitmap); for(j=0; j ptr = (Byte *)Bmp->ScanLine[j]; for(i=0; i gray = (ptr[i]*0.114 ptr[i 1]*0.587 ptr[i 2]*0.299); if(gray > 0) gray = 255; else gray = 0; ptr[i] = ptr[i 1] = ptr[i 2] = (Byte)gray; } } Image2->Picture->Assign(Bmp); delete Bmp; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button5Click(TObject *Sender) { Graphics::TBitmap *Bmp = new Graphics::TBitmap(); Byte *ptr; int gray; int i,j; int sx = 0; int sy = 0; int **pic = 0; int thresh; Bmp->Assign(Image1->Picture->Bitmap); Bmp->PixelFormat=pf24bit; pic = new int*[Bmp->Height]; for(i = 0; i< Bmp->Height; i ) pic[i] = new int[Bmp->Width]; Bmp->Assign(Image1->Picture->Bitmap); for(j=0; j ptr = (Byte *)Bmp->ScanLine[j]; for(i=0; i gray = (ptr[i*3]*0.114 ptr[i*3 1]*0.587 ptr[i*3 2]*0.299); pic[j][i] = ptr[i*3] = ptr[i*3 1] = ptr[i*3 2] = (Byte)gray; } } for(j=1; j ptr = (Byte *)Bmp->ScanLine[j]; for(i=1; i sx = -1*pic[j-1][i-1] pic[j 1][i-1] -2*pic[j-1][i] 2*pic[j 1][i] - pic[j-1][i 1] pic[j 1][i 1]; sy = pic[j-1][i-1] 2*pic[j][i-1] pic[j 1][i-1] - 1*pic[j-1][i 1] - 2*pic[j][i 1] - pic[j 1][i 1]; if(sqrt(sx*sx sy*sy)>thresh) { ptr[i*3] = ptr[i*3 1] = ptr[i*3 2] = 0; } else ptr[i*3] = ptr[i*3 1] = ptr[i*3 2] = 255; } } Image2->Picture->Assign(Bmp); for (i=0; i< Bmp->Height; i ) delete [] pic[i]; delete [] pic; delete Bmp; } //--------------------------------------------------------------------------- [/code] 執行灰階後,解析度變低了,而且顏色也沒有改變。
附加檔案:4b0a65bf84386_灰階.rar
編輯記錄
silent852 重新編輯於 2009-11-23 21:21:32, 註解 無‧
|
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
silent852
一般會員 發表:1 回覆:1 積分:0 註冊:2009-11-08 發送簡訊給我 |
|
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |