//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <Graphics.hpp>
#include "CenterLine.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
struct indexRec {
int row;
int fromCol;
int toCol;
};
TForm1 *Form1;
indexRec indexM[30000];
long int indexP = 0;
int layerInc = 20;
Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
bool appendPixel2Matrix(int y, int x) {
indexM[indexP].toCol = x;
return true;
}
//---------------------------------------------------------------------------
bool InsertNewSeg2Matrix(int y, int x) {
indexM[indexP].row = y;
indexM[indexP].fromCol = x;
indexM[indexP].toCol = x;
return true;
}
//---------------------------------------------------------------------------
bool isBoard(int x,int y, int currentLayer) {
int xx,yy;
uchar pixelValue;
for(yy=y-1; yy<=y+1; yy++) {
if(yy<0 | yy > pBitmap->Height) continue;
for(xx=x-1; xx<=x+1; xx++) {
if(xx<0 | xx > pBitmap->Width) continue;
if(xx==x & yy==y) continue;
pixelValue = (uchar)pBitmap->Canvas->Pixels[xx][yy];
if(pixelValue==255 | pixelValue== (uchar)(currentLayer-layerInc)) return true;
}
}
return false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
pBitmap->PixelFormat = pf8bit;
int currentLayer = 0;
bool segStart = false;
uchar pixelValue;
bool changed = true;
int x,y,i;
indexP = 0;
layerInc = 20;
pBitmap->LoadFromFile("D:\\工程\\中心线\\1001.bmp");
pBitmap->PixelFormat = pf8bit;
// form index matrix
for (y = 0; y < pBitmap->Height; y++) {
segStart = false;
for(x = 0; x < pBitmap->Width; x++) {
pixelValue = (uchar)pBitmap->Canvas->Pixels[x][y];
if(pixelValue==0) { //black pixel
if(segStart) {appendPixel2Matrix(y,x);continue; }
else {InsertNewSeg2Matrix(y,x);segStart=true;continue;}
}
else { //white pixel
if(segStart) {
segStart = false;
indexP++;
if(indexP>=30000) {
ShowMessage("indexP overflow!!");
return;
}
}
}
}
}
// search board pixel by the index of matrix indexM
changed = true;
currentLayer = 10;
while(changed) {
changed = false;
currentLayer += layerInc;
for(i= 0; i<indexP; i++) {
y = indexM[i].row;
for(x=indexM[i].fromCol; x<=indexM[i].toCol;x++) {
//dataPos = y*cvImage->widthStep;
pixelValue = (uchar)pBitmap->Canvas->Pixels[x][y];
if(pixelValue!=0) continue;
if(isBoard(x,y,currentLayer)) {
pBitmap->Canvas->Pixels[x][y]= currentLayer;
changed = true;
}
}
}
//Form1->Canvas->Draw(0,0,pBitmap);
if(!changed) break;
}
Form1->Canvas->Draw(0,0,pBitmap);
}
//---------------------------------------------------------------------------
以上是代码!帮我看看,谢谢啦!!