#define INIT_IMAGE(Img, wid, hei, widstep){if(Img.imageData != 0) delete [] Img.imageData;\
Img.imageData = new BYTE [(widstep) * (hei)]; memset(Img.imageData, 0, sizeof(BYTE) * (widstep) * (hei));\
Img.width = (wid); Img.height = hei; Img.widthstep = widstep;}
//释放
#define RLSE_BUFFER(x) {if (x != 0) delete [] x; x = NULL;}
#define MATRIX_REF(PR, NUMROWS, R, C) \
(*((PR) + (NUMROWS)*(C) + (R)))
static int weights3[3][3] = { {1, 8, 64}, {2, 16, 128}, {4, 32, 256} };
int iptNhood3Offset(unsigned char *pBWin, int numRows, int numCols,
int r, int c)
{
int minR, maxR, minC, maxC;
int rr, cc;
int result = 0;
if (r == 0)
{
minR = 1;
}
else
{
minR = 0;
}
if (r == (numRows-1))
{
maxR = 1;
}
else
{
maxR = 2;
}
if (c == 0)
{
minC = 1;
}
else
{
minC = 0;
}
if (c == (numCols-1))
{
maxC = 1;
}
else
{
maxC = 2;
}
for (rr = minR; rr <= maxR; rr++)
{
for (cc = minC; cc <= maxC; cc++)
{
result += weights3[rr][cc] *
(MATRIX_REF(pBWin, numRows, r + rr - 1, c + cc - 1) != 0);
}
}
return(result);
}
void iptMyapplylutc(IMAGE BWin, IMAGE& BWout, BYTE* lut)
{
int numRows, numCols;
int r, c;
unsigned char *pBWin;
unsigned char *plut;
unsigned char *pBWout;
pBWin = BWin.imageData;
plut = lut;
pBWout = BWout.imageData;
numRows = BWin.height;
numCols = BWin.width;
for (c = 0; c < numCols; c++)
{
for (r = 0; r < numRows; r++)
{
MATRIX_REF(pBWout, numRows, r, c) = (unsigned char)
(*(plut + iptNhood3Offset(pBWin, numRows, numCols, r, c)) == 0? 0: 255);
}
}
}
//图像细化导出函数
void Thin(IMAGE Src, IMAGE& Dst, int n)
{
int i, j;
int wid = Src.width;
int hei = Src.height;
int widEff = Src.widthstep;
int nSize = wid * hei * sizeof(BYTE);
IMAGE mySrc, myDst,c, lastc, image_iter1;
mySrc.imageData = NULL;
INIT_IMAGE(mySrc, wid, hei, wid);
myDst.imageData = NULL;
INIT_IMAGE(myDst, wid, hei, wid);
c.imageData = NULL;
INIT_IMAGE(c, wid, hei, wid);
lastc.imageData = NULL;
INIT_IMAGE(lastc, wid, hei, wid);
image_iter1.imageData = NULL;
INIT_IMAGE(image_iter1, wid, hei, wid);
//mySrc, myDst,...
for (i = 0; i < hei; i++)
{
for (j = 0; j < wid; j++)
{
*(mySrc.imageData + j * hei + i) = *(Src.imageData + i * widEff + j);
*(myDst.imageData + j * hei + i) = *(Src.imageData + i * widEff + j);
*(c.imageData + j * hei + i) = *(Src.imageData + i * widEff + j);
*(lastc.imageData + j * hei + i) = *(Src.imageData + i * widEff + j);
*(image_iter1.imageData + j * hei + i) = *(Src.imageData + i * widEff + j);
}
}
unsigned char lut1[512] =
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1
,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1
,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,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,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,1,1,0,0,1,1,0,1,1,1};
int iter = 1;
bool done = 0;
bool equalC;
while (!done)
{
memcpy(lastc.imageData, c.imageData, nSize);
iptMyapplylutc(c, image_iter1, lut1);
iptMyapplylutc(image_iter1, c, lut2);
for (i = 0; i < nSize; i++)
{
if ( *(lastc.imageData+i) != *(c.imageData+i) )
{
equalC = 0;
break;
}
}
done = ((iter >= n) | equalC);
iter++;
}
//返回结果
for (i = 0; i < hei; i++)
{
for (j = 0; j < wid; j++)
{
*(Dst.imageData + i * widEff + j) = *(c.imageData + j * hei + i);
}
}
//释放空间
RLSE_BUFFER(mySrc.imageData);
RLSE_BUFFER(myDst.imageData);
RLSE_BUFFER(c.imageData);
RLSE_BUFFER(lastc.imageData);
RLSE_BUFFER(image_iter1.imageData);
}
欢迎使用CSDN-markdown编辑器
最新推荐文章于 2021-01-07 23:01:28 发布