📄 hwpreview.cpp
字号:
dlgline.m_uintLineVal = 0;
if (dlgline.DoModal()!=IDOK) {
return;
}
int iLineHeight = dlgline.m_uintLineHeight;
int iLineVal = dlgline.m_uintLineVal;
int k,j;
i=0;
int * lLine = new int[cr.Height()+1];
for (k= 0 ;k<=cr.Height();k++) {
if (lcount[k]<iLineVal) {
lLine[k]=0;
}else{
lLine[k] = 1;
}
}
while (i<=cr.Height())
{
for (k= i;k<=cr.Height();k++) {
if (lLine[k]==1) {
i=k;
break;
}
}
if (k>cr.Height()) { break; }
for (k=i+1,j=1;k<=cr.Height();k++) {
if (lLine[k]==1) {
j++;
}else{
break;
}
}
if (j>1.3* iLineHeight) {
iLineVal ++;
for (k= 0 ;k<=cr.Height();k++) {
if (lcount[k]<iLineVal) {
lLine[k]=0;
}else{
lLine[k] = 1;
}
}
}else{
i+=j;
}
}
i=0;
while (i<=cr.Height()) {
for (k= i;k<=cr.Height();k++) {
if (lLine[k]==1) {
i=k;
break;
}
}
if (k>cr.Height()) { break; }
for (k=i+1,j=1;k<=cr.Height();k++) {
if (lLine[k]==1) {
j++;
}else{
break;
}
}
//if (j>0.5*iLineHeight) {
m_ListRect.AddTail(new CRect(cr.left,i+cr.top ,cr.right,i+j-1+cr.top));
//}
i+=j;
}
delete [] lLine;
GetDocument()->HDIBCOPYTOHDIB2();
HDIB hdibD = GetDocument()->GetHDIB2();
LPSTR hlpstr = (LPSTR) ::GlobalLock((HGLOBAL) hdibD);
LPSTR hBits = ::FindDIBBits(hlpstr);
LPBYTE hbitsD ;
LONG lHeight = ::DIBHeight(hlpstr);
LONG lWidth = ::DIBWidth(hlpstr);
LONG m_lLineBytes = WIDTHBYTES( lWidth*8 );
memset(hBits,(BYTE)255, lHeight * lWidth);
//int j ;
for (i= cr.top;i<=cr.bottom;i++) {
for (j= 1;j<=lcount[i-cr.top];j++) {
hbitsD = (LPBYTE ) hBits + m_lLineBytes * (lHeight -1 -i) + j+cr.left;
* hbitsD = 0;
}
}
delete[] lcount;
::GlobalUnlock((HGLOBAL) hdibD);
}
void CHwpreView::OnEgVword()
{
// TODO: Add your command handler code here
CRect cr,*pcr;int i = 0 ; POSITION pst;BOOL isfind=FALSE;
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
pcr = ((CRect * ) m_ListRect.GetAt(pst));
cr = *pcr;
if (cr.PtInRect(m_ptCurRect)) {
m_ListRect.RemoveAt(pst);
delete pcr;
isfind =TRUE;
break;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
if (!isfind) {
cr.left = cr.top = 0;cr.right = GetDocument()->GetDocSize().cx -1;
cr.bottom = GetDocument()->GetDocSize().cy -1;
}
int *lcount = new int[cr.Width()+1];
::VprojectDIB2((LPSTR)GetDocument()->GetHDIB(),lcount,cr.left ,cr.top ,cr.right ,cr.bottom );
CDlgLine dlgline;
dlgline.m_uintLineHeight = 20;
dlgline.m_uintLineVal = 0;
if (dlgline.DoModal()!=IDOK) {
return;
}
int iLineHeight = dlgline.m_uintLineHeight;
int iLineVal = dlgline.m_uintLineVal;
int k,j;
i=0;
int * lLine = new int[cr.Width()+1];
for (k= 0 ;k<=cr.Width();k++) {
if (lcount[k]<iLineVal) {
lLine[k]=0;
}else{
lLine[k] = 1;
}
}
while (i<=cr.Width())
{
for (k= i;k<=cr.Width();k++) {
if (lLine[k]==1) {
i=k;
break;
}
}
if (k>cr.Width()) { break; }
for (k=i+1,j=1;k<=cr.Width();k++) {
if (lLine[k]==1) {
j++;
}else{
break;
}
}
if (j>1.3* iLineHeight) {
iLineVal ++;
for (k= 0 ;k<=cr.Width();k++) {
if (lcount[k]<iLineVal) {
lLine[k]=0;
}else{
lLine[k] = 1;
}
}
}else{
i+=j;
}
}
i=0;
while (i<=cr.Width()) {
for (k= i;k<=cr.Width();k++) {
if (lLine[k]==1) {
i=k;
break;
}
}
if (k>cr.Width()) { break; }
for (k=i+1,j=1;k<=cr.Width();k++) {
if (lLine[k]==1) {
j++;
}else{
break;
}
}
//if (j>0.5*iLineHeight) {
m_ListRect.AddTail(new CRect(cr.left+i,cr.top ,cr.left+i+j-1,cr.bottom));
//}
i+=j;
}
delete [] lLine;
GetDocument()->HDIBCOPYTOHDIB2();
HDIB hdibD = GetDocument()->GetHDIB2();
LPSTR hlpstr = (LPSTR) ::GlobalLock((HGLOBAL) hdibD);
LPSTR hBits = ::FindDIBBits(hlpstr);
LPBYTE hbitsD ;
LONG lHeight = ::DIBHeight(hlpstr);
LONG lWidth = ::DIBWidth(hlpstr);
LONG m_lLineBytes = WIDTHBYTES( lWidth*8 );
memset(hBits,(BYTE)255, lHeight * lWidth);
//int j ;
for (i= cr.left;i<=cr.right;i++) {
for (j= 1;j<=lcount[i-cr.left];j++) {
hbitsD = (LPBYTE ) hBits + m_lLineBytes * (lHeight -1 +j-cr.bottom) + i;
* hbitsD = 0;
}
}
delete[] lcount;
::GlobalUnlock((HGLOBAL) hdibD);
}
void CHwpreView::OnViewShownumber()
{
// TODO: Add your command handler code here
m_bshowNumber = !m_bshowNumber;
GetDocument()->UpdateAllViews(NULL);
}
void CHwpreView::OnUpdateViewShownumber(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bshowNumber);
}
void CHwpreView::OnMorphThining()
{
// TODO: Add your command handler code here
// 获取文档
CHwpreDoc * pDoc = GetDocument();
// 指向DIB的指针
LPSTR lpDIB;
// 指向DIB象素指针
LPSTR lpDIBBits;
//pDoc->HDIBCOPYTOHDIB2();
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
// 更改光标形状
BeginWaitCursor();
// 找到DIB图像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
// 调用ThiningDIB()函数对DIB进行闭运算
if (ThiningDIB(lpDIBBits, WIDTHBYTES(::DIBWidth(lpDIB) * 8), ::DIBHeight(lpDIB)))
{
// 设置脏标记
pDoc->SetModifiedFlag(TRUE);
// 更新视图
pDoc->UpdateAllViews(NULL);
}
else
{
// 提示用户
MessageBox("分配内存失败或者图像中含有0和255之外的像素值!", "系统提示" , MB_ICONINFORMATION | MB_OK);
}
// 解除锁定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB2());
// 恢复光标
EndWaitCursor();
}
void CHwpreView::OnMorphDilation()
{
// TODO: Add your command handler code here
CHwpreDoc * pDoc = GetDocument();
// 指向DIB的指针
LPSTR lpDIB;
// 指向DIB象素指针
LPSTR lpDIBBits;
// pDoc->HDIBCOPYTOHDIB2();
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
// 更改光标形状
BeginWaitCursor();
// 找到DIB图像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
int nMode=2;
int structure[3][3];
structure[0][0]=0;
structure[0][1]=0;
structure[0][2]=0;
structure[1][0]=-1;
structure[1][1]=0;
structure[1][2]=-1;
structure[2][0]=0;
structure[2][1]=0;
structure[2][2]=0;
// 调用DilationDIB()函数膨胀DIB
if (DilationDIB(lpDIBBits, WIDTHBYTES(::DIBWidth(lpDIB) * 8), ::DIBHeight(lpDIB), nMode , structure))
{
// 设置脏标记
pDoc->SetModifiedFlag(TRUE);
// 更新视图
pDoc->UpdateAllViews(NULL);
}
else
{
// 提示用户
MessageBox("分配内存失败或者图像中含有0和255之外的像素值!", "系统提示" , MB_ICONINFORMATION | MB_OK);
}
// 解除锁定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB2());
// 恢复光标
EndWaitCursor();
}
void CHwpreView::OnAutoHough()
{
// TODO: Add your command handler code here
CHwpreDoc * pDoc = GetDocument();
HDIB hdib_backup;
// 指向DIB的指针
LPSTR lpDIB;
// 指向DIB象素指针
LPSTR lpDIBBits;
//pDoc->HDIBCOPYTOHDIB2();
hdib_backup = (HDIB) CopyHandle((HGLOBAL)pDoc->GetHDIB2());
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
// 更改光标形状
BeginWaitCursor();
// 找到DIB图像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
ThresholdTrans(lpDIBBits,::DIBWidth(lpDIB),::DIBHeight(lpDIB),(BYTE)250);
int nMode=2;
int structure[3][3];
structure[0][0]=-1;
structure[0][1]=-1;
structure[0][2]=-1;
structure[1][0]=-1;
structure[1][1]=0;
structure[1][2]=-1;
structure[2][0]=0;
structure[2][1]=0;
structure[2][2]=0;
// 调用DilationDIB()函数膨胀DIB
DilationDIB(lpDIBBits, WIDTHBYTES(::DIBWidth(lpDIB) * 8), ::DIBHeight(lpDIB), nMode , structure);
// 调用ThiningDIB()函数细化DIB
ThiningDIB(lpDIBBits, WIDTHBYTES(::DIBWidth(lpDIB) * 8), ::DIBHeight(lpDIB));
// 调用HoughDIB()函数对DIB找到旋转角
double lAngleRotate,lAngleRotate2,lAngle;
HoughDIB2( lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB),-10,10,0.1, 0,::DIBWidth(lpDIB)*4/5,::DIBHeight(lpDIB),::DIBWidth(lpDIB), lAngleRotate);
HoughDIB2( lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB),-10,10,0.1,0,0,::DIBHeight(lpDIB),::DIBWidth(lpDIB)/5,lAngleRotate2);
lAngle=(lAngleRotate+lAngleRotate2)/2;
// 解除锁定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB2());
pDoc->ReplaceHDIB2(hdib_backup);
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
// 找到DIB图像象素起始位置
HDIB hNewDIB=NULL;
// 调用RotateDIB()函数旋转DIB RotateDIB2双线性插值
if(abs(lAngle) > 0.1)
{
hNewDIB=(HDIB) RotateDIB(lpDIB,-lAngle);
if (hNewDIB!=NULL) {
pDoc->ReplaceHDIB2(hNewDIB);
pDoc->InitDIBData2();
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
}
else{
// 提示用户
MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
}
}
lpDIBBits = ::FindDIBBits(lpDIB);
ThresholdTrans(lpDIBBits,::DIBWidth(lpDIB),::DIBHeight(lpDIB),(BYTE)250);
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
// 解除锁定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB2());
// 恢复光标
EndWaitCursor();
}
void CHwpreView::OnGaborFFT()
{
// TODO: Add your command handler code here
CHwpreDoc * pDoc = GetDocument();
// 指向DIB的指针
LPSTR lpDIB;
pDoc->HDIBCOPYTOHDIB2();
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
// 更改光标形状
BeginWaitCursor();
int * image = new int[128*128]; //128*128
::DIBToIntArray(lpDIB,0,0,127,127,image,128,128,ID_IMAGE_01);
// 解除锁定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB2());
double * TransformResult = new double [64];
((CMainFrame*)AfxGetMainWnd())->SetMessageText("正在准备 Gabor 核函数...");
FFTGabor gabor(101,256);
gabor.PrepareKernel();
/*
//单核函数测试
FFTGaborResult * result = new FFTGaborResult(128) ;
for(int Orientation=0; Orientation<8; Orientation++)
for(int Frequency=0; Frequency<4; Frequency++)
{
gabor.GaborTransform(image,128,128,Orientation,Frequency,result);
TransformResult[(Orientation * 4 + Frequency) * 2] = result->Avg;
TransformResult[(Orientation * 4 + Frequency) * 2 + 1] = result->Deta;
}
delete result;
*/
/*
//全部核函数
gabor.GaborTransform(image,128,128,TransformResult);
*/
/*
//全部核函数--复数数组
complex<double> *TD = new complex<double>[256 * 256];
gabor.intTocomplex(TD,image,128,128);
gabor.GaborTransform(TD,128,128,TransformResult);
delete [] TD;
*/
/*
*/ //单核函数测试--复数数组
complex<double> *TD = new complex<double>[256 * 256];
gabor.intTocomplex(TD,image,128,128);
FFTGaborResult * result = new FFTGaborResult(128) ;
for(int Orientation=0; Orientation<8; Orientation++)
for(int Frequency=0; Frequency<4; Frequency++)
{
gabor.GaborTransform(TD,128,128,Orientation,Frequency,result);
TransformResult[(Orientation * 4 + Frequency) * 2] = result->Avg;
TransformResult[(Orientation * 4 + Frequency) * 2 + 1] = result->Deta;
}
delete result;
delete [] TD;
//
delete [] image;
delete [] TransformResult;
// 恢复光标
EndWaitCursor();
}
void CHwpreView::OnGaborFFT32()
{
// TODO: Add your command handler code here
CHwpreDoc * pDoc = GetDocument();
// 指向DIB的指针
LPSTR lpDIB;
pDoc->HDIBCOPYTOHDIB2();
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
// 更改光标形状
BeginWaitCursor();
int * image = new int[32*32]; //128*128
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -