這是我在這個網站中看到的有關直方圖顯示的程式 #include
#pragma hdrstop #include "GetRGBHistogram0.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Form1->DoubleBuffered = true;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Load1Click(TObject *Sender)
{
if ( OpenDialog1->Execute() )
{ Image1->Picture->LoadFromFile(OpenDialog1->FileName);
Image1->Top = 1;
Image1->Left = 1;
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Width = Image1->Picture->Width;
bmp->Height = Image1->Picture->Height;
bmp->Canvas->Draw(0,0,Image1->Picture->Graphic);
Image1->Picture->Bitmap->Assign(bmp);
Image1->Refresh();
delete bmp;
}
}
//---------------------------------------------------------------------------
void JpgToBmp(TImage *image)
{ Graphics::TBitmap *bmp = new Graphics::TBitmap(); bmp->Width = image->Picture->Width;
bmp->Height = image->Picture->Height;
bmp->Canvas->Draw(0,0,image->Picture->Graphic);
image->Picture->Bitmap->Assign(bmp);
image->Refresh(); delete bmp;
} void showRGBHistogram(TImage *image, TCanvas *canvas)
{
Byte * ptr;
int rgb[3][256]={0}, max[3]={0}, r, g, b, gray, index; image->Picture->Bitmap->PixelFormat = pf24bit;
for (int row=0; rowPicture->Height; row )
{ ptr = (Byte*) image->Picture->Bitmap->ScanLine[row];
index = 0;
for (int col=0; colPicture->Width; col )
{ b = ptr[index ];
g = ptr[index 1];
r = ptr[index 2];
rgb[0][b] ;
rgb[1][g] ;
rgb[2][r] ;
index = 3;
}
}
for (int x=0; x<256; x )
{ for (int p=0; p<3; p )
{ if ( rgb[p][x]>max[p] )
max[p] = rgb[p][x];
}
}
for (int p=0; p<3; p ){
float scale = (float)256 / max[p];
canvas->MoveTo(0, (int)(256-rgb[p][0]*scale));
for (int x=1; x<256; x ){
canvas->LineTo(x, (int)(256-rgb[p][x]*scale));
}
}
} //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender)
{
JpgToBmp(Image1);
showRGBHistogram(Image1, Image2->Canvas);
} 但是我又找了一個有關直方圖等化的程式,但是會發生access 無效的問題,
請問我的問題出在哪呢?
我第一次用bcb寫有關影像處理的問題,
請各位大大多多指教~~ 等化的程式
float gray_ratio1;
int gray_level=256;
int **gray_value;
Byte *ptr;
int equalize [2][256];
gray_ratio1 = (float)255/(gray_level-1)/2;
for(int i=0; i<2; i )
for(int j=0; j<256; j )
equalize[i][j] = 0;
Graphics::TBitmap *bmp4 = new Graphics::TBitmap();
bmp4->Assign(Image2->Picture->Bitmap);
for(int i=0; iHeight; i )
{
ptr = (Byte*)bmp4->ScanLine[i];
for(int j=0; jWidth ; j )
equalize[0][ ptr[j*3] ] ;
} for(int i=0; i<256; i )
{
equalize[1][i] = ( (float)equalize[0][i]/(bmp4->Height*bmp4->Width) )*255;
if(i != 255)
equalize[0][i 1] = equalize[0][i];
} for(int i=0; iHeight; i )
{
ptr = (Byte*)bmp4->ScanLine[i];
for(int j=0; j<420; j )
{
ptr[j*3] = equalize[1][ ptr[j*3] ];
if(gray_level != 256)
{
gray_value[i][j] = ( ptr[j*3]/gray_ratio1 1 ) / 2;
ptr[j*3] = gray_value[i][j] * 2 * gray_ratio1;
}
}
Image2->Picture->Bitmap->Assign(bmp4);
ptr = NULL;