📄 zxdib.cpp
字号:
int i;
int temp;
int *pTemp = new int[level];
for(i=0; i<level; i++)
pTemp[i] = pHg[i];
temp = (69*pTemp[0]+4*pTemp[1]-6*pTemp[2]+4*pTemp[3]-pTemp[4])/70;
if(temp<0) temp = 0;
pHg[0] = (BYTE)temp;
temp = (2*pTemp[0]+27*pTemp[1]+12*pTemp[2]-8*pTemp[3]+2*pTemp[4])/35;
if(temp<0) temp = 0;
pHg[1] = (BYTE)temp;
for(i=2; i<level-2; i++)
{
temp = (-3*pTemp[i-2]+12*pTemp[i-1]+17*pTemp[i]+12*pTemp[i+1]-3*pTemp[i+2])/35;
if(temp<0) temp=0;
pHg[i] = (BYTE)temp;
}
temp = (2*pTemp[level-5]-8*pTemp[level-4]+12*pTemp[level-3]+27*pTemp[level-2]+2*pTemp[level-1])/35;
if(temp<0) temp = 0;
pHg[level-2] = (BYTE)temp;
temp = (-1*pTemp[level-5]+4*pTemp[level-4]-6*pTemp[level-3]+4*pTemp[level-2]+69*pTemp[level-1])/70;
if(temp<0) temp = 0;
pHg[level-1] = (BYTE)temp;
delete pTemp;
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 二值化
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CZXDib::BlackAndWhite(BYTE *pBitmap, DWORD dwHeight, DWORD dwWidth, unsigned char flag)
{
DWORD i,j;
for(j=0; j<dwHeight; j++)
for(i=0; i<dwWidth; i++)
{
if(pBitmap[j*dwWidth+i]<flag) pBitmap[j*dwWidth+i] = 0;
else pBitmap[j*dwWidth+i] = 255;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 用小矩形来降低图像的分辨率
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CZXDib::Cover2ValueImageByRect(BYTE *pBitmap, DWORD dwHeight, DWORD dwWidth,DWORD dwRectHeight,DWORD dwRectWidth)
{
DWORD i,j,k,l;
DWORD temp;
BYTE* pResult=NULL;
DWORD upgate = (DWORD)(dwRectHeight*dwRectWidth*0.80);
DWORD downgate = (DWORD)(dwRectHeight*dwRectWidth*0.30);
pResult = (BYTE*) new BYTE[dwHeight*dwWidth];
for(j=0; j<dwHeight-dwRectHeight; j++)
for(i=0; i<dwWidth-dwRectWidth; i++)
{
temp = 0;
for(k=0; k<dwRectHeight; k++)
for(l=0; l<dwRectWidth; l++)
{
if(pBitmap[(j+k)*dwWidth+l+i])
temp ++;
}
if(temp>upgate)
for(k=0; k<dwRectHeight; k++)
for(l=0; l<dwRectWidth; l++)
pResult[(j+k)*dwWidth+l+i]=255;
else if(temp<downgate)
for(k=0; k<dwRectHeight; k++)
for(l=0; l<dwRectWidth; l++)
pResult[(j+k)*dwWidth+l+i]=0;
else
for(k=0; k<dwRectHeight; k++)
for(l=0; l<dwRectWidth; l++)
pResult[(j+k)*dwWidth+l+i]= pResult[(j+k)*dwWidth+l+i]?255:0;
}
for(i=0;i<dwHeight; i++)
for(j=0;j<dwWidth; j++)
pBitmap[i*dwWidth+j] = pResult[i*dwWidth+j];
delete pResult;
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 通过投影来确定目标的位置区域
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CZXDib::ProjectAndFindTargetRegion(BYTE *pBitmap, DWORD dwHeight, DWORD dwWidth, RECT &rect)
{
DWORD i,j;
bool contenue;
int nGate = 8;
DWORD * pLine=NULL;
DWORD * pCol=NULL;
pLine = new DWORD [dwWidth];
pCol = new DWORD [dwHeight];
for(i=0; i<dwWidth; i++)
pLine[i] = 0;
for(j=0; j<dwHeight;j++)
pCol[j] =0;
for(j=0; j<dwHeight; j++)
for(i=0; i<dwWidth; i++)
{
if(pBitmap[j*dwWidth+i])
pCol[j]++;
}
rect.bottom = dwHeight-1;
rect.right = dwWidth-1;
rect.top = rect.left = 0;
// for(i=0;i<5;i++)
// HistogramSmoothBy5Points(pLine,dwWidth);
DWORD // HistogramSmoothBy5Points(pCol,dwHeight);
i_from=0,i_to=0,counter=0,counter_max=0,i_from_max,i_to_max;
for(i=0; i<dwHeight-2; i++)
{
if(pCol[i]>nGate && !i_from)
{
i_from = i;
counter =pCol[i];
}
else
{
if(i_from && (pCol[i]>nGate || pCol[i+1]>nGate))
counter+=pCol[i];
else
if(pCol[i]<nGate && !i_to && i_from && pCol[i+1]<nGate)
{
i_to = i;
counter += pCol[i];
if(i_to - i_from < 10)
counter = 0;
if(counter>counter_max)
{
counter_max = counter;
i_from_max = i_from;
i_to_max = i_to;
}
i_from = i_to =0;
counter = 0;
}
}
}
if(i_from && !i_to && counter>counter_max)
{
counter_max = counter;
i_from_max = i_from;
i_to_max = i-1;
}
if(counter_max)
{
rect.top = i_from_max;
rect.bottom = i_to_max;
}
if(!counter_max) return false;
counter = counter_max/(i_to_max-i_from_max);
counter /= 3;
contenue = true;
for(i=rect.top;i<rect.bottom && contenue;i++)
if(pCol[i]<counter)
rect.top++;
else
contenue=false;
contenue = true;
for(i=rect.bottom;i>=rect.top && contenue;i--)
if(pCol[i]<counter)
rect.bottom--;
else
contenue=false;
if(rect.bottom>rect.top+100)
rect.bottom = rect.top+100;
//计算在图像竖直方向的投影:
for(j=rect.top; j<rect.bottom; j++)
for(i=0; i<dwWidth; i++)
{
if(pBitmap[j*dwWidth+i])
pLine[i]++;
}
i_from=0,i_to=0,counter=0,counter_max=0,i_from_max,i_to_max;
for(i=0; i<dwWidth-2; i++)
{
if(pLine[i]>nGate && !i_from)
{
i_from = i;
counter =pLine[i];
}
else
{
if(i_from && (pLine[i]>nGate || pLine[i+1]>nGate))
counter+=pLine[i];
else
if(pLine[i]<nGate && !i_to && i_from && pLine[i+1]<nGate)
{
i_to = i;
counter += pLine[i];
if(i_to - i_from < 10)
counter = 0;
if(counter>counter_max)
{
counter_max = counter;
i_from_max = i_from;
i_to_max = i_to;
}
i_from = i_to =0;
counter = 0;
}
}
}
if(i_from && !i_to && counter>counter_max)
{
counter_max = counter;
i_from_max = i_from;
i_to_max = i-1;
}
if(counter_max)
{
rect.left = i_from_max;
rect.right = i_to_max;
}
if(!counter_max) return false;
counter = counter_max/(i_to_max-i_from_max);
counter = counter * 1/3;
contenue = true;
for(i=rect.left;i<rect.right && contenue;i++)
if(pLine[i]<counter)
rect.left++;
else
contenue=false;
contenue = true;
for(i=rect.right;i>=rect.left && contenue;i--)
if(pLine[i]<counter)
rect.right--;
else
contenue=false;
delete pCol;
delete pLine;
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// 在矩阵中画线:
//
//////////////////////////////////////////////////////////////////////////////////////////////////
bool CZXDib::DrawLineInTheMatric(int x_from, int y_from, int x_to, int y_to, BYTE *pBitmap,DWORD dwHeight,DWORD dwWidth,BYTE color)
{
DWORD position=0;
double position1;
int temp;
double temp1,temp2;
y_from = dwHeight - y_from -1;
y_to = dwHeight - y_to - 1;
if(abs(x_from-x_to)>abs(y_from-y_to))
{
if(x_from>x_to)
{
temp = x_to;
x_to = x_from;
x_from = temp;
temp = y_to;
y_to = y_from;
y_from = temp;
}
temp1 = y_to-y_from;
temp2 = x_to-x_from;
temp1 /= temp2;
position1 = y_from;
position = y_from;
for( int i=x_from; i<=x_to; i++)
{
pBitmap[i+(position)*dwWidth] = color ;
position1 += temp1;
position = (DWORD) (position1+0.5);
}
}
else
{
if(y_from>y_to)
{
temp = y_to;
y_to = y_from;
y_from = temp;
temp = x_to;
x_to = x_from;
x_from = temp;
}
temp1 = x_to-x_from;
temp2 = y_to-y_from;
temp1 /= temp2;
position1 = x_from;
position = x_from;
for( int i=y_from; i<=y_to; i++)
{
pBitmap[position+(i)*dwWidth] = color ;
position1 += temp1;
position = (DWORD) (position1+0.5);
}
}
return true;
}
///////////////////////////
// 在24位真彩色矩阵中划线
///////////////////////////
bool CZXDib::DrawLineInTheRGBMatric(int x_from, int y_from, int x_to, int y_to, BYTE *pBitmap,DWORD dwHeight,DWORD dwWidth,RGBQUAD color)
{
DWORD position=0;
double position1;
int temp;
double temp1,temp2;
if(x_from <0 || x_to <0 ||y_from<0 || y_to<0) return false;
if(x_from >dwWidth-1 || x_to >dwWidth-1 ||y_from>dwHeight-1 || y_to > dwHeight-1) return false;
y_from = dwHeight - y_from -1;
y_to = dwHeight - y_to - 1;
if(abs(x_from-x_to)>abs(y_from-y_to))
{
if(x_from>x_to)
{
temp = x_to;
x_to = x_from;
x_from = temp;
temp = y_to;
y_to = y_from;
y_from = temp;
}
temp1 = y_to-y_from;
temp2 = x_to-x_from;
temp1 /= temp2;
position1 = y_from;
position = y_from;
for( int i=x_from; i<=x_to; i++)
{
pBitmap[(i+(position)*dwWidth)*3 ] = color.rgbBlue;
pBitmap[(i+(position)*dwWidth)*3+1] = color.rgbGreen;
pBitmap[(i+(position)*dwWidth)*3+2] = color.rgbRed;
position1 += temp1;
position = (DWORD) (position1+0.5);
}
}
else
{
if(y_from>y_to)
{
temp = y_to;
y_to = y_from;
y_from = temp;
temp = x_to;
x_to = x_from;
x_from = temp;
}
temp1 = x_to-x_from;
temp2 = y_to-y_from;
temp1 /= temp2;
position1 = x_from;
position = x_from;
for( int i=y_from; i<=y_to; i++)
{
pBitmap[(position+(i)*dwWidth)*3 ] = color.rgbBlue;
pBitmap[(position+(i)*dwWidth)*3+1] = color.rgbGreen;
pBitmap[(position+(i)*dwWidth)*3+2] = color.rgbRed;
position1 += temp1;
position = (DWORD) (position1+0.5);
}
}
return true;
}
////////////////////////////////////////////////////////////////////////
//
// 直方图平滑:
//
////////////////////////////////////////////////////////////////////////
bool CZXDib::HistogramSmoothBy5Points(DWORD *pHg,int level)
{
int i;
int temp;
int *pTemp = new int[level];
for(i=0; i<level; i++)
pTemp[i] = pHg[i];
temp = (69*pTemp[0]+4*pTemp[1]-6*pTemp[2]+4*pTemp[3]-pTemp[4])/70;
if(temp<0) temp = 0;
pHg[0] = (WORD)temp;
temp = (2*pTemp[0]+27*pTemp[1]+12*pTemp[2]-8*pTemp[3]+2*pTemp[4])/35;
if(temp<0) temp = 0;
pHg[1] = (WORD)temp;
for(i=2; i<level-2; i++)
{
temp = (-3*pTemp[i-2]+12*pTemp[i-1]+17*pTemp[i]+12*pTemp[i+1]-3*pTemp[i+2])/35;
if(temp<0) temp=0;
pHg[i] = (WORD)temp;
}
temp = (2*pTemp[level-5]-8*pTemp[level-4]+12*pTemp[level-3]+27*pTemp[level-2]+2*pTemp[level-1])/35;
if(temp<0) temp = 0;
pHg[level-2] = (WORD)temp;
temp = (-1*pTemp[level-5]+4*pTemp[level-4]-6*pTemp[level-3]+4*pTemp[level-2]+69*pTemp[level-1])/70;
if(temp<0) temp = 0;
pHg[level-1] = (WORD)temp;
delete pTemp;
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////
//
// 归一化到灰度图像的范围内
//
///////////////////////////////////////////////////////////////////////////////////////////
bool CZXDib::RegularToGrayLevel(double *pBitmap, DWORD dwHeight, DWORD dwWidth,BYTE*& pResult)
{
DWORD i,j;
double max=pBitmap[0];
double min=pBitmap[0];
double temp;
if(pResult) delete pResult;
pResult = new BYTE[dwHeight*dwWidth];
for(i=0; i<dwHeight;i++)
for(j=0; j<dwWidth; j++)
if(pBitmap[i*dwWidth+j]>max) max = pBitmap[i*dwWidth+j];
else
if(pBitmap[i*dwWidth+j]<min) min = pBitmap[i*dwWidth+j];
max -= min;
for(i=0; i<dwHeight;i++)
for(j=0; j<dwWidth; j++)
{
temp = (pBitmap[i*dwWidth+j]-min)*255.0/max;
pResult[i*dwWidth+j] = (BYTE) temp;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////
//
// 归一化到灰度图像的范围内
//
///////////////////////////////////////////////////////////////////////////////////////////
bool CZXDib::RegularToGrayLevel(DWORD *pBitmap, DWORD dwHeight, DWORD dwWidth,BYTE*& pResult)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -