直方圖等化問題 |
缺席
|
超級賽亞人3
一般會員 發表:38 回覆:53 積分:23 註冊:2007-06-05 發送簡訊給我 |
寫了一個Histogram Equalization的程式
用BMP的圖檔測試 連續的開啟圖檔後 有時候開的圖檔 不知是原本的對比度落差很大 會有部分區塊是黑黑的 想問一下這部份要如何解決 我覺得問題是在118行到136行之間 但試了幾次都沒有解決 [code cpp] #include #pragma hdrstop #include <math.h><br />#include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { Image3->Canvas->Pixels[0][0]=(TColor)RGB(255,255,255); //把Image3變成白色的 Image4->Canvas->Pixels[0][0]=(TColor)RGB(255,255,255); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { int c,d; char OutMatrix[230][256]; // char IMatrix[230][256] ; // char sum[256]; if(OpenPictureDialog1->Execute()) { //TForm1 *Form1 = new TForm1(Application); for(c=0;c<=500;c ) { for(d=0;d<=300;d ) { Image3->Canvas->Pixels[c][d]=(TColor)RGB(255,255,255); Image4->Canvas->Pixels[c][d]=(TColor)RGB(255,255,255);//開圖檔前把Image(3、4)Set White } } /**********************************灰階化***********************************/ Form1->Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);//Image1讀一張圖檔 Byte *ptr;//用來指像素行的指標 int r,g,b;//rgb三原色 int grey;//用來存灰階化後的值 float count[256]={0}; //各色階計數 Graphics::TBitmap *Bmp = new Graphics::TBitmap(); //定義一個圖象句柄// Bmp->Assign(Form1->Image1->Picture->Bitmap); // 將Image1 的Bitmap轉存到TBitmap Bmp for(d = 0; d < Bmp->Height; d) { ptr = (Byte *) Bmp->ScanLine[d]; //該行像素位址給ptr for(c = 0; c < Bmp->Width; c) { b=ptr[c*3]; //該點第一個位子是藍色 g=ptr[c*3 1]; //該點第二個位子是綠色 r=ptr[c*3 2]; //該點第三個位子是紅色 grey=0.299*r 0.587*g 0.114*b; //計算灰階後的值 ptr[c*3]=(Byte)grey; //存回灰階後的值 ptr[c*3 1]=(Byte)grey; ptr[c*3 2]=(Byte)grey; count[grey]; //灰階化後的各色階的計數 } } Image1->Picture->Assign(Bmp); //將灰階後的圖檔分配到Image1 /***************************灰階化後的Histogram***************************/ int reckon = Form1->Image1->Picture->Width * Form1->Image1->Picture->Height; //寬X高=點數 int height=0; //直方圖高度 float percentage=0; float max=0; int x,y; for(x=0; x <256; x) if(max < count[x]) max = count[x]; //找累計最多的次數 for(x = 0; x < 256; x ) //畫直方圖 { if(count[x]!= 0) //掃瞄灰階0~255 { percentage = count[x]/ max; height = percentage*200; for(y = 0; y <= height; y ) { //OutMatrix[y][x]=sum[IMatrix[y][x]]*2.55; //Image3->Canvas->Pixels[x][230-y]=(TColor)RGB(OutMatrix[y][x],OutMatrix[y][x],OutMatrix[y][x]);//畫Histogram的Image3高度 Image3->Canvas->Pixels[x 1][230-y]=(TColor)RGB(0,0,0); //增加線條的粗細 } } } /************************將灰階的直方圖等化*****************************/ /* 公式:Pg(g)=1/g /* 轉換函數:g =(g Form1 = new TForm1(Application); int cdfMin; //累計cdf值中最小的 float HCount[256]={0}; //cdf(0~255) //HCount[0]=count[0]; for(x = 0; x < 256; x ) HCount[x] = HCount[x-1] count[x]; //計算cdf for(x = 0; x < 256; x ) { if(HCount[x]!=0) { cdfMin=HCount[x]; //找出cdf的Min break; } } int Hstorage;//儲存直方圖等化後的值 float Recount[256]={0}; for(x = 0; x < Bmp->Height; x) { ptr = (Byte *) Bmp->ScanLine[x]; for(y = 0; y < Bmp->Width; y) { Hstorage = floor(((HCount[ptr[y*3]]-cdfMin)/(reckon - cdfMin))*255); ptr[y*3]=(Byte)Hstorage; ptr[y*3 1]=(Byte)Hstorage; ptr[y*3 2]=(Byte)Hstorage; Recount[Hstorage]; //灰階化後的各色階的計數 (用來重畫Histogram) } } Image2->Picture->Assign(Bmp); //將灰階後的圖檔分配到Image2 //修改後的histogram 產生 int max2=0; for(x = 0; x <256; x) if(max2 < count[x]) max2 = count[x]; for(x = 0; x < 256; x) { if(Recount[x]!= 0) { percentage = Recount[x]/(double)max2; height = percentage*200; for(int y = 0; y <= height; y ) { //Image4->Canvas->Pixels[x][230-y]=(TColor)RGB(OutMatrix[y][x],OutMatrix[y][x],OutMatrix[y][x]); Image4->Canvas->Pixels[x][230-y]=(TColor)RGB(0,0,0); } } } Image3->Repaint(); Image4->Repaint(); } } //--------------------------------------------------------------------------- [/code] .h檔 //--------------------------------------------------------------------------- #ifndef Unit2H #define Unit2H //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />#include #include #include //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TImage *Image1; TImage *Image2; TImage *Image3; TImage *Image4; TLabel *Label1; TLabel *Label2; TLabel *Label3; TLabel *Label4; TButton *Button1; TOpenPictureDialog *OpenPictureDialog1; void __fastcall FormCreate(TObject *Sender); void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif 編輯記錄
超級賽亞人3 重新編輯於 2009-07-10 12:25:31, 註解 無‧
|
超級賽亞人3
一般會員 發表:38 回覆:53 積分:23 註冊:2007-06-05 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |