wei7278 您好: 我試著將下列左圖的灰階統計出來,再將統計的結果畫在右圖上。
使用到list,請參考Builder的線上說明及下列連結自行研究了解一下。
< href="http://www.msoe.edu/eecs/cese/resources/stl/list.htm">http://www.msoe.edu/eecs/cese/resources/stl/list.htm。
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "UseList0.h"
#include
using namespace std; //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------- struct myPoint
{ int x, y;
myPoint()
{ x=y= 0 ;
}
myPoint(int px, int py)
{ x = px;
y = py;
}
}; __fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void toGray(TImage *image)
{ Byte * ptr;
int 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];
gray = 0.299*r + 0.587*g + 0.114*b;
ptr[index ] = (Byte)gray;
ptr[index+1] = (Byte)gray;
ptr[index+2] = (Byte)gray;
index += 3;
}
}
} //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
list gray[256];
Byte *ptr;
int value, index; toGray(Image1);
Image1->Refresh();
Image1->Picture->Bitmap->PixelFormat = pf24bit; for (int y=0; yHeight; y++)
{ ptr = (Byte*) Image1->Picture->Bitmap->ScanLine[y];
index = 0;
for (int x=0; xWidth; x++)
{ value = (int)ptr[index];
gray[value].push_back(myPoint(x,y));
index += 3;
}
}
ImageResult->AutoSize = true;
ImageResult->Picture->Bitmap->Width = Image1->Picture->Width;
ImageResult->Picture->Bitmap->Height = Image1->Picture->Height;
ImageResult->Picture->Bitmap->PixelFormat = pf24bit; int px, py;
list::const_iterator iter; for (int k=0; k<256; k++)
{ iter = gray[k].begin();
for (int p=0; px;
py = iter->y;
ImageResult->Picture->Bitmap->Canvas->Pixels[px][py] = (TColor) RGB(k,k,k);
iter++;
}
}
ImageResult->Refresh();
}
//---------------------------------------------------------------------------
RichTop 敬上 =====*****
把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題!
=====#####