📄 image.cpp
字号:
largest = p[i]; */
Dib();
return(r_v);
}
void CImage::Binary(int t, BOOL bBackground)
{
unsigned char* img;
long l1,l2;
int h, w;
img = Data();
h = Height();
w = Width();
l2=(long)h*w;
if(bBackground)
for (l1=0;l1<l2;l1++)
if (*(img+l1)>t)
*(img+l1)=255;
else
*(img+l1)=0;
if(!bBackground)
for (l1=0;l1<l2;l1++)
if (*(img+l1)>t)
*(img+l1)=0;
else
*(img+l1)=255;
Dib();
}
void CImage::MidFilter()
{
BYTE* pImageData = Data();
DWORD dwWidth = Width();
DWORD dwHeight = Height();
DWORD dwImageLength = dwWidth*dwHeight;
/////////////////////////////////////
BYTE *lpTempData = new BYTE[dwImageLength];
int nWinSize = 3;
int nHist[256];
BYTE cSortData[100];
int nCount;
int nWinWid,nWinHei;
BYTE cMidValue;
int nNumLessMid;
for(nCount=0;nCount<256;nCount++)
nHist[nCount]=0;
for(int nHeiCount=0;nHeiCount<(int)dwHeight;nHeiCount++)
{for(nCount=0;nCount<256;nCount++)
nHist[nCount]=0;
for(nWinHei=0;nWinHei<nWinSize;nWinHei++)
for(nWinWid=0;nWinWid<nWinSize;nWinWid++)
{cSortData[nWinHei*nWinSize+nWinWid]=
GetBit(pImageData,dwWidth,dwHeight,0-(nWinSize-1)/2+nWinWid,nHeiCount-(nWinSize-1)/2+nWinHei);
nHist[cSortData[nWinHei*nWinSize+nWinWid]]++;
for(nCount=nWinHei*nWinSize+nWinWid;nCount>0;nCount--)
{if(cSortData[nCount]<cSortData[nCount-1])
{unsigned char cTemp=cSortData[nCount];
cSortData[nCount]=cSortData[nCount-1];
cSortData[nCount-1]=cTemp;
}
else break;
}
}
*(lpTempData+DWORD(nHeiCount)*dwWidth) = cSortData[(nWinSize*nWinSize-1)/2];
cMidValue = cSortData[(nWinSize*nWinSize-1)/2];
nNumLessMid = 0;
for(nCount=0;nCount<=(nWinSize*nWinSize-1)/2;nCount++)
{if(cMidValue>cSortData[nCount])
nNumLessMid++;
else break;
}
for(int nWidCount=1;nWidCount<(int)dwWidth;nWidCount++)
{for(nCount=0-(nWinSize-1)/2;nCount<=(nWinSize-1)/2;nCount++)
{nHist[GetBit(pImageData,dwWidth,dwHeight,nWidCount-(nWinSize+1)/2,nHeiCount+nCount)]--;
if(GetBit(pImageData,dwWidth,dwHeight,nWidCount-(nWinSize+1)/2,nHeiCount+nCount)<cMidValue)
nNumLessMid--;
}
for(nCount=0-(nWinSize-1)/2;nCount<=(nWinSize-1)/2;nCount++)
{nHist[GetBit(pImageData,dwWidth,dwHeight,nWidCount+(nWinSize-1)/2,nHeiCount+nCount)]++;
if(GetBit(pImageData,dwWidth,dwHeight,nWidCount+(nWinSize-1)/2,nHeiCount+nCount)<cMidValue)
nNumLessMid++;
}
if(nNumLessMid>(nWinSize*nWinSize-1)/2)
do{cMidValue--;
nNumLessMid-=nHist[cMidValue];
}while(nNumLessMid>(nWinSize*nWinSize-1)/2);
else if(nNumLessMid<(nWinSize*nWinSize-1)/2)
{while(nNumLessMid<((nWinSize*nWinSize-1)/2)&&(nNumLessMid+nHist[cMidValue])<=(nWinSize*nWinSize-1)/2)
{nNumLessMid+=nHist[cMidValue];
cMidValue++;
}
}
*(lpTempData+DWORD(nHeiCount)*dwWidth+nWidCount) = cMidValue;
}
}
for(DWORD dwCount=0;dwCount<dwImageLength;dwCount++)
*(pImageData+dwCount) = *(lpTempData+dwCount);
delete lpTempData;
Dib();
}
void CImage::Smoothing()
{
int conv[3][3];
int i,j,value,v1,v2,v3,high,width;
long j0,j1,j2,target_line_offset,source_line1_offset,source_line2_offset;
BYTE *img, *smooth;
high = Height();
width = Width();
img = Data();
CSize size=CSize(width,high);
CImage* dest=new CImage;//(size);
*dest = *this;
smooth = dest->Data();
//memset(smooth, '\0', (long)high * width);
/* inter pixel */
conv[0][0]=1; conv[0][1]=1; conv[0][2]=1;
conv[1][0]=1; conv[1][1]=1; conv[1][2]=1;
conv[2][0]=1; conv[2][1]=1; conv[2][2]=1;
target_line_offset=(long)-width;
source_line1_offset=0;
source_line2_offset=(long)width;
for (i=1;i<high;i++) {
target_line_offset=source_line1_offset;
source_line1_offset=source_line2_offset;
source_line2_offset+=(long)width;
j1=0; j2=1;
for (j=1;j<width;j++) {
j0=j1; j1=j2; j2++;
value=0;
v1=(int)*(img+target_line_offset+j0)*conv[0][0];
v1+=(int)*(img+target_line_offset+j1)*conv[0][1];
v1+=(int)*(img+target_line_offset+j2)*conv[0][2];
v2=(int)*(img+source_line1_offset+j0)*conv[1][0];
v2+=(int)*(img+source_line1_offset+j1)*conv[1][1];
v2+=(int)*(img+source_line1_offset+j2)*conv[1][2];
v3=(int)*(img+source_line2_offset+j0)*conv[2][0];
v3+=(int)*(img+source_line2_offset+j1)*conv[2][1];
v3+=(int)*(img+source_line2_offset+j2)*conv[2][2];
value=(v1+v2+v3)/9;
if (value>255) value=255;
*(smooth+target_line_offset+j0)=value;
}
}
memcpy(img, smooth, (long)high * width);
Dib();
delete dest;
}
void CImage::Thin()
{
int Bnum, Anum, CN, max;
register int finish, x, y, TW = 1;
int height, width;
BYTE* binimg;
BYTE *up, *down, *left, *right, *curr;
BYTE *upleft, *upright, *downleft, *downright;
BYTE *offset, *temp;
DWORD offset1;
long size;
binimg = Data();
height = Height();
width = Width();
offset = binimg + (long)width - 1L;
finish = 0; //thinner is not finished
size = (long)height * width;
CImage* tmp=new CImage(CSize(width,height));
temp=tmp->Data();
memset(temp, '\0', size);
// add weight calculation: source--binimg ==> dest--binimg
while (finish != 1) {
finish = 1;
offset1 = (long)width - 1L;
for (x = 1; x < height - 1; x++) {
offset1 += 2L;
for (y = 1; y < width - 1; y++) {
if (*(binimg + offset1) == TW) {
curr = binimg + offset1;
up = curr - (unsigned long)width;
down = curr + (unsigned long)width;
left = curr - 1L;
right= curr + 1L;
if (TW >= 64) {
max = *up + *down + *left + *right;
if (max > 255)
*(temp + offset1) = 255;
else
*(temp + offset1) = (BYTE)max;
finish = 2;
}
else {
*(temp + offset1) = *up + *down + *left + *right;
finish = 0;
}
}// if
offset1++;
}// for y
}// for x
if (finish == 0) {
TW *= 4;
memcpy(binimg, temp, size);
}// if
else if(finish == 2)
memcpy(binimg, temp, size);
}// while
// get raletive weight: source--binimg ==> dest--temp
memset(temp, '\0', size);
offset1 = (long)width - 1L;
for (x = 1; x < height - 1; x++) {
offset1 += 2L;
for (y = 1; y < width - 1; y++) {
if (*(binimg + offset1) != 0) {
if (*(binimg + offset1) == 255)
*(temp + offset1) = 4;
else {
curr = binimg + offset1;
up = curr - (unsigned long)width;
down = curr + (unsigned long)width;
left = curr - 1L;
right= curr + 1L;
upleft = up - 1L;
upright = up + 1L;
downleft = down - 1L;
downright = down + 1L;
if (*curr >= *up && *curr >= *down)
(*(temp + offset1))++;
if (*curr >= *left && *curr >= *right)
(*(temp + offset1))++;
if (*curr >= *upleft && *curr >= *downright)
(*(temp + offset1))++;
if (*curr >= *downleft && *curr >= *upright)
(*(temp + offset1))++;
}// else
}// if *binimg+offset1
offset1++;
}// for y
}// for x
// delete non_skel pixel: source--temp ==> dest--temp
offset1 = (long)width - 1L;
for (x = 1; x < height - 1; x++) {
offset1 += 2L;
for (y = 1; y < width - 1; y++) {
if ((*(temp + offset1) > 0) && (*(temp + offset1) < 3)) {
curr = temp + offset1;
up = curr - (unsigned long)width;
down = curr + (unsigned long)width;
left = curr - 1L;
right= curr + 1L;
upleft = up - 1L;
upright = up + 1L;
downleft = down - 1L;
downright = down + 1L;
Bnum = 0;
if (*up) Bnum++;
if (*down) Bnum++;
if (*left) Bnum++;
if (*right) Bnum++;
if (*upleft) Bnum++;
if (*upright) Bnum++;
if (*downleft) Bnum++;
if (*downright) Bnum++;
if (Bnum > 1) {
Anum = 0;
if (*up == 0)
if ((*upright != 0) || (*right != 0)) Anum++;
if (*right == 0)
if ((*downright != 0) || (*down != 0)) Anum++;
if (*down == 0)
if ((*downleft != 0) || (*left != 0)) Anum++;
if (*left == 0)
if ((*upleft != 0) || (*up != 0)) Anum++;
if (Anum == 1) *curr = 0;
}//if Bnum
}// if *temp+offset1
offset1++;
}// for y
}// for x
do {
finish = 0;
offset1 = (long)width - 1L;
for (x = 1; x < height - 1; x++) {
offset1 += 2L;
for (y = 1; y < width - 1; y++) {
if ((*(temp + offset1) > 0) && (*(temp + offset1) < 3)) {
curr = temp + offset1;
up = curr - (unsigned long)width;
down = curr + (unsigned long)width;
left = curr - 1L;
right= curr + 1L;
upleft = up - 1L;
upright = up + 1L;
downleft = down - 1L;
downright = down + 1L;
Bnum = 0;
if (*up) Bnum++;
if (*down) Bnum++;
if (*left) Bnum++;
if (*right) Bnum++;
if (*upleft) Bnum++;
if (*upright) Bnum++;
if (*downleft) Bnum++;
if (*downright) Bnum++;
if (Bnum < 5) {
Anum = 0;
if (*up == 0)
if ((*upright != 0) || (*right != 0)) Anum++;
if (*right == 0)
if ((*downright != 0) || (*down != 0)) Anum++;
if (*down == 0)
if ((*downleft != 0) || (*left != 0)) Anum++;
if (*left == 0)
if ((*upleft != 0) || (*up != 0)) Anum++;
if (Anum == 1) {
*curr = 0;
finish = 1;
}//if Anum
}//if Bnum
}// if *temp+offset1
offset1++;
}// for y
}// for x
}while (finish != 0);
// post_processing
memcpy(binimg, temp, size);
offset = binimg + (long)width - 1L;
for (x = 1; x < height - 1; x++) {
offset += 2L;
for (y = 1; y < width - 1; y++) {
if (*offset > 0)
{
up = offset - (unsigned long)width;
down = offset + (unsigned long)width;
left = offset - 1L;
right= offset + 1L;
upleft = up - 1L;
upright = up + 1L;
downleft = down - 1L;
downright = down + 1L;
CN = 0;
if (*up)
if ((*upright) == 0) CN++; else ;
else
if (*upright) CN++;
if (*upright)
if ((*right) == 0) CN++; else ;
else
if (*right) CN++;
if (*right)
if ((*downright) == 0) CN++; else ;
else
if (*downright) CN++;
if (*downright)
if ((*down) == 0) CN++; else ;
else
if (*down) CN++;
if (*down)
if ((*downleft) == 0) CN++; else ;
else
if (*downleft) CN++;
if (*downleft)
if ((*left) == 0) CN++; else ;
else
if (*left) CN++;
if (*left)
if ((*upleft) == 0) CN++; else ;
else
if (*upleft) CN++;
if (*upleft)
if ((*up) == 0) CN++; else ;
else
if (*up) CN++;
Bnum = 0;
if (*up) Bnum++;
if (*down) Bnum++;
if (*left) Bnum++;
if (*right) Bnum++;
if (*upleft) Bnum++;
if (*upright) Bnum++;
if (*downleft) Bnum++;
if (*downright) Bnum++;
if (CN == 2) {
if (Bnum >= 3) *offset = 0;
else if (Bnum > 0)
if ((*offset) == 1)
*offset = 0;
}//if CN == 2
if ((CN == 4) && (Bnum >= 2)) {
Anum = 0;
if (*up == 0)
if ((*upright != 0) || (*right != 0)) Anum++;
if (*right == 0)
if ((*downright != 0) || (*down != 0)) Anum++;
if (*down == 0)
if ((*downleft != 0) || (*left != 0)) Anum++;
if (*left == 0)
if ((*upleft != 0) || (*up != 0)) Anum++;
if (Anum == 1) *offset = 0;
}// if CN == 4
if (*offset == 1 && Bnum == 0 ) *offset = 0;
if (*offset == 3 && Bnum == 1) {
Bnum = 0;
if (*up == 1) Bnum++;
if (*down == 1) Bnum++;
if (*left == 1) Bnum++;
if (*right == 1) Bnum++;
if (*upleft == 1) Bnum++;
if (*upright == 1) Bnum++;
if (*downleft == 1) Bnum++;
if (*downright == 1) Bnum++;
if (Bnum == 1) *offset = 0;
}//if Bnum == 1
}// if *offset > 0
offset++;
}// for y
}// for x
offset = binimg + (long)width - 1L;
for (x = 1; x < height - 1; x++) {
offset += 2L;
for (y = 1; y < width - 1; y++) {
if (*offset != 0) *offset = 255;
// if (*offset != 0) *offset = 1;
offset++;
}// for y
}// for x
delete tmp;
}// thin
BOOL CImage::NormalHistogram(BYTE step)
{
BYTE g;
int h, w;
BYTE newgrey[GREY];
long counter, pixel_sum;
if(Bits() !=8)
{
AfxMessageBox("can't process non-8bits bitmap!");
return false;
}
BYTE* img = Data();
w = Width();
h = Height();
pixel_sum = (long)h*w;
HistogramEqualize(newgrey, step);
for (counter=0;counter<pixel_sum;counter++)
{
g = *(img+counter);
*(img+counter) = newgrey[g];
}
Dib();
return true;
}
BOOL C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -