下面是我的程式碼,我想要將一張彩色影像轉換成256色灰階之後,在使用平均、中值等等濾波器對灰階影像去濾波。但是,轉灰階之後,濾波卻是對彩色的圖片做濾波,請各位大大可以教我怎麼修改這個程式的程式碼。謝謝囉!!
#include
#include <math.h>
#pragma hdrstop #include "HW4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Open1Click(TObject *Sender)
{
if(this->OpenDialog1->Execute())
{
open = true;
this->Image1->Picture->LoadFromFile(this->OpenDialog1->FileName);
this->Image1->Picture->Bitmap->PixelFormat = pf32bit;
ImageW = this->Image1->Picture->Bitmap->Width;
ImageH = this->Image1->Picture->Bitmap->Height; Data = (RGBA**)new RGBA*[ImageH];
tempData = (RGBA**)new RGBA*[ImageH];
for(int x=0;xImage1->Picture->Bitmap->ScanLine[x],sizeof(RGBA)*ImageW);
memcpy(tempData[x],this->Image1->Picture->Bitmap->ScanLine[x],sizeof(RGBA)*ImageW);
} this->Image2->Picture->Bitmap->Width = ImageW;
this->Image2->Picture->Bitmap->Height = ImageH;
this->Image2->Left = this->Image1->Left this->Image1->Width 20;
this->Image2->Visible = true;
for(int x=0;xImage2->Canvas->Pixels[y][x] = this->Image1->Canvas->Pixels[y][x];
}
}
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Save1Click(TObject *Sender)
{
if(this->SaveDialog1->Execute())
this->Image2->Picture->SaveToFile(this->SaveDialog1->FileName ".bmp");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Close1Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Averagefilter1Click(TObject *Sender)
{
this->Image2->Visible = true;
//配置AverageData
RGBA** AverageData=(RGBA**)new RGBA*[ImageH];
for (int y=0; yCanvas->Pixels[j][i]=(TColor)RGB(AverageData[i][j].R,AverageData[i][j].G,AverageData[i][j].B);
//釋放meandata記憶體
for (int y=0; y0; a--)
{
for(int b=0; btempr[b 1])
{
int temp=tempr[b 1];
tempr[b 1]=tempr[b];
tempr[b]=temp;
}
if(tempg[b]>tempg[b 1])
{
int temp=tempg[b 1];
tempg[b 1]=tempg[b];
tempg[b]=temp;
}
if(tempb[b]>tempb[b 1])
{
int temp=tempb[b 1];
tempb[b 1]=tempb[b];
tempb[b]=temp;
}
}
}
Middledata[i][j].R=tempr[4];
Middledata[i][j].G=tempg[4];
Middledata[i][j].B=tempb[4];
}
}
//show圖
for (int i=0; iCanvas->Pixels[j][i]=(TColor)RGB(Middledata[i][j].R,Middledata[i][j].G,Middledata[i][j].B);
//釋放middledata記憶體
for (int y=0; yPicture->Bitmap;
if(!pic->Empty){
unsigned char **Buffer = new unsigned char*[pic->Height];
for (i=0; i< pic->Height; i )
Buffer[i] = new unsigned char[pic->Width];
pic->PixelFormat = pf32bit;
RGBQUAD *Pixel; for (Row=0; Row < pic->Height ; Row ){
Pixel = (RGBQUAD *) pic->ScanLine[Row];
for (Col=0; Col < pic->Width; Col , Pixel )
Buffer[Row][Col] = (BYTE) ((float)Pixel->rgbRed*0.299
(float)Pixel->rgbGreen*0.587
(float)Pixel->rgbBlue*0.114);} for (Row=0; Row < pic->Height ; Row ){
Pixel = (RGBQUAD *) pic->ScanLine[Row];
for (Col=0; Col < pic->Width; Col , Pixel ){
Pixel->rgbRed = (BYTE) Buffer[Row][Col];
Pixel->rgbGreen = (BYTE) Buffer[Row][Col];
Pixel->rgbBlue = (BYTE) Buffer[Row][Col];
}
}
Image1->Invalidate(); for (i=0; iHeight; i )
delete[] Buffer[i];
delete [] Buffer;
}
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Maxfilter1Click(TObject *Sender)
{
//配置Maxdata
RGBA** Maxdata=(RGBA**)new RGBA*[ImageH];
for (int y=0; y0; a--)
{
for(int b=0; btempr[b 1])
{
int temp=tempr[b 1];
tempr[b 1]=tempr[b];
tempr[b]=temp;
}
if(tempg[b]>tempg[b 1])
{
int temp=tempg[b 1];
tempg[b 1]=tempg[b];
tempg[b]=temp;
}
if(tempb[b]>tempb[b 1])
{
int temp=tempb[b 1];
tempb[b 1]=tempb[b];
tempb[b]=temp;
}
}
}
Maxdata[i][j].R=tempr[8];
Maxdata[i][j].G=tempg[8];
Maxdata[i][j].B=tempb[8];
}
}
//show圖
for (int i=0; iCanvas->Pixels[j][i]=(TColor)RGB(Maxdata[i][j].R,Maxdata[i][j].G,Maxdata[i][j].B);
//釋放Maxdata記憶體
for (int y=0; y0; a--)
{
for(int b=0; btempr[b 1])
{
int temp=tempr[b 1];
tempr[b 1]=tempr[b];
tempr[b]=temp;
}
if(tempg[b]>tempg[b 1])
{
int temp=tempg[b 1];
tempg[b 1]=tempg[b];
tempg[b]=temp;
}
if(tempb[b]>tempb[b 1])
{
int temp=tempb[b 1];
tempb[b 1]=tempb[b];
tempb[b]=temp;
}
}
}
Mindata[i][j].R=tempr[0];
Mindata[i][j].G=tempg[0];
Mindata[i][j].B=tempb[0];
}
}
//show圖
for (int i=0; iCanvas->Pixels[j][i]=(TColor)RGB(Mindata[i][j].R,Mindata[i][j].G,Mindata[i][j].B);
//釋放Maxdata記憶體
for (int y=0; y 愛情換來的是幸福,是永恆,還是心碎的眼淚?