📄 viewcdibview.cpp
字号:
// TODO: Add your command handler code here
// if(m_pthreshold.DoModal()==IDOK)
// int p=m_pthreshold.m_threshold;
// pDoc->m_pDib->EdgeEnhance(10,16);
if(pDoc->m_pDib->IsEmpty())
return;
double p;
thisparam* m_newthisparam=new thisparam;
m_newthisparam=&m_thisparam;
if(m_newthisparam->DoModal()==IDOK)
p=m_newthisparam->m_thisparamnew;
int thr=pDoc->m_pDib->GetThreshold(p);
pDoc->m_pDib->Convert2BW(thr);
pDoc->m_pDib->BuildBitmap();
pDoc->m_pDib->BuildPalette();
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnRever()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_pDib->Reverse();
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnContour2()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
// WaitCursorBegin();
CDib m_tmpDib1;
m_tmpDib1.m_hDib=CopyHandle(pDoc->m_pDib->m_hDib);
CDib m_tmpDib2;
m_tmpDib2.m_hDib=CopyHandle(pDoc->m_pDib->m_hDib);
//pDoc->m_pDib->Contour(TRUE);
m_tmpDib1.Contour(TRUE);
m_tmpDib2.Contour(FALSE);
LPBITMAPINFO lpSource=(LPBITMAPINFO)GlobalLock(pDoc->m_pDib->m_hDib);
LPBITMAPINFO lpSource1=(LPBITMAPINFO)GlobalLock(m_tmpDib1.m_hDib);
LPBITMAPINFO lpSource2=(LPBITMAPINFO)GlobalLock(m_tmpDib2.m_hDib);
if(!lpSource)
{
// WaitCursorEnd();
return;
}
LONG Height=DIBHeight(lpSource);
LONG Width=DIBWidth(lpSource);
int max=256;
int Bytes=BytesPerLine(lpSource);
LONG buffersize=GlobalSize(pDoc->m_pDib->m_hDib);//第二次运行这个函数时这里出错了!
LPSTR lptr;
LPSTR lptr1;
LPSTR lptr2;
for(int i=1;i<=Height;i++)
{
for(int j=0;j<Width;j++)
{
lptr=(LPSTR)lpSource+(buffersize-i*Bytes)+j;
lptr1=(LPSTR)lpSource1+(buffersize-i*Bytes)+j;
lptr2=(LPSTR)lpSource2+(buffersize-i*Bytes)+j;
*lptr=(*lptr1-*lptr2)?*lptr1:*lptr2;
}
}
GlobalUnlock(m_tmpDib1.m_hDib);
GlobalUnlock(m_tmpDib2.m_hDib);
GlobalUnlock(pDoc->m_pDib->m_hDib);
pDoc->m_pDib->BuildBitmap();
pDoc->m_pDib->BuildPalette();
pDoc->UpdateAllViews(NULL);
pDoc->SetModifiedFlag(TRUE);
}
void CViewCDibView::OnLapedge8()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_pDib->EdgeEnhance(10,LAP1);
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnXihua()
{
// TODO: Add your command handler code here
// CViewCDibDoc* pDoc=GetDocument();
// ASSERT_VALID(pDoc);
}
//data
struct person{
int str[8];
double parameter;
};
struct selectPos{
int se1[8];
int se2[8];
};
void initializega(int * p);
void fitness(int * m_p,person * m_person);
void computaFitness();
void crossover();
void mutation();
void getBestPer(int * m_p);
int change(person * m_person);
person old[16];
person cur[16];
person best;
double aver=0,num=0;
selectPos position;
int ga(int * p)
{
int i=100,j=0;
while(j<=255)
{
num+=*(p+j);
aver+=(*(p+j))*(1+j);
j++;
}
aver/=num;
initializega(p);
while(i>=0)
{
for(int j=0;j<16;j++)
{
fitness(p,cur+j);
}
computaFitness();
crossover();
mutation();
i--;
}
for(int jj=0;jj<16;jj++)
{
fitness(p,cur+jj);
}
getBestPer(p);
return T=change(& best);
}
void initializega(int * p)
{
int i=0,j=0;
for(j=0;j<16;j++)
{
for(i=0;i<8;i++)
{
old[j].str[i]=cur[j].str[i]=rand()%2;
}
fitness(p,cur+j);
fitness(p,old+j);
}
}
void fitness(int * m_p,person * m_person)
{
int T=128;
T=change(m_person);
double w0=0,w1=0;
double u0=0,u1=0;
for(int i=0;i<=T;i++)
{
w0+=*(m_p+i);
u0+=*(m_p+i)*(i+1);
}
if(w0!=0)
{
u0/=w0;
w0/=num;
}
else
{
w0=0;
u0=0;
}
for(i=T+1;i<256;i++)
{
w1+=*(m_p+i);
u1+=*(m_p+i)*(i+1);
}
if(w1!=0)
{
u1/=w1;
w1/=num;
}
else
{
u1=0;
w1=0;
}
(* m_person).parameter=w0*(u0-aver)*(u0-aver)+w1*(u1-aver)*(u1-aver);
}
void computaFitness()
{
double total=0;
int i=0;
for(i=0;i<16;i++)
{
total+=cur[i].parameter;
}
i--;
while(i>=0)
{
cur[i].parameter/=total;
i--;
}
i++;
while(i<15)
{
cur[i+1].parameter+=cur[i].parameter;
i++;
}
}
void crossover()
{
struct person * temp=new struct person[16];
//GlobalAlloc(
memcpy(temp,old,16*sizeof(person));
memcpy(old,cur,16*sizeof(person));
int i=0,tt;
for(int jj=0;jj<8;jj++)
{
tt=rand()%100;
double t=(double)tt/99;
for(int j=0;j<16;j++)
{
if(t>cur[j].parameter)
continue;
else
{
position.se1[jj]=j;
break;
}
}
tt=rand()%100;
t=(double)tt/99;
for(j=0;j<16;j++)
{
if(t>cur[j].parameter)
continue;
else
{
position.se2[jj]=j;
break;
}
}
}
for(i=0;i<8;i++)
{
int axis=4;
tt=rand()%100;
double rate=(double)tt/99;
if(rate<=0.7)
{
for(int co=7;co>axis;co--)
{
cur[i].str[co]=old[position.se1[i]].str[co];
cur[i+8].str[co]=old[position.se2[i]].str[co];
}
}
}
delete temp;
}
void mutation()
{
for(int i=0;i<=15;i++)
{
double rate=(double)(rand()%10)/9;
if(rate<=0.01)
{
int axis=rand()%8;
cur[i].str[axis]=1-cur[i].str[axis];
}
}
}
void getBestPer(int * m_p)
{
for (int i=0;i<16;i++)
{
fitness(m_p,cur+i);
}
best=cur[0];
for(int j=0;j<16;j++)
{
if(best.parameter<cur[j].parameter)
best=cur[j];
}
}
int change(person * m_person)
{
int i=0;
int result=0;
for(i=0;i<=7;i++)
{
result+=(*m_person).str[i]*(int)pow(2,7-i);
}
if(result>255)
{
return 255;
}
if(result<0)
{
return 0;
}
return result;
}
void CViewCDibView::OnSubpixel49()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
HDIB tempDib=NULL;
tempDib=CopyHandle(pDoc->m_pDib->m_hDib);
LPBITMAPINFO lptmp=(LPBITMAPINFO )GlobalLock(tempDib);
DWORD buffer=GlobalSize(lptmp);
LONG Height=DIBHeight(tempDib);
LONG Width=DIBWidth(tempDib);
int bytes=BytesPerLine(lptmp);
DWORD imagesize=bytes*Height;
LPBYTE lptmpBits=(LPBYTE)lptmp+buffer-imagesize;
//now lptmpBits is the first pointer to bitmap;
int count[256]={0};
for (int i=0;i<Height-1;i++)
for (int j=0;j<Width-1;j++)
{
int tempGray=*(lptmpBits+i*bytes+j);
count[tempGray]++;
}
//to project the hist of bitmap into c count[256]]
T=ga(count);
for (int ii=0;ii<Height-1;ii++)
for (int jj=0;jj<Width-1;jj++)
{
int tempGray=*(lptmpBits+ii*bytes+jj);
if(T<tempGray)
tempGray=255;
else
tempGray=0;
*(lptmpBits+ii*bytes+jj)=tempGray;
}
GlobalUnlock(tempDib);
pDoc->m_pDib->m_hDib=CopyHandle(tempDib);
pDoc->m_pDib->BuildBitmap();
pDoc->m_pDib->BuildPalette();
pDoc->UpdateAllViews(NULL);
pDoc->SetModifiedFlag(TRUE);
//temDib
}
LPSTR DecodeError(int ErrorCode)
{
static char Message[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, ErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)Message, 1024, NULL);
return Message;
}
double equation(double p1);
bool saveData(result1 * data,int n);
void CViewCDibView::OnSubpixel49ii()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
// CDib tmp;
HDIB tmpDib=NULL;
// tmp.m_hDib=CopyHandle(pDoc->m_pDib->m_hDib);
// tmp.EdgeEnhance(10,LAP2);//laplace convlue
// HANDLE& tmpDib=tmp.m_hDib;//a temp Dib
tmpDib=CopyHandle(pDoc->m_pDib->m_hDib);
if(!EdgeEnhanceDIB(tmpDib,10,LAP1))
{
AfxMessageBox("the edge detection is wrong!");
return;
}
LPBITMAPINFO lptmp=(LPBITMAPINFO)GlobalLock(tmpDib);
if(!lptmp)
{
AfxMessageBox("lptmp is not right!");
return;
}
LPBITMAPINFO lpSrc=(LPBITMAPINFO)GlobalLock(pDoc->m_pDib->m_hDib);
if(!lpSrc)
{
AfxMessageBox("lpSrc is not right!");
return;
}
DWORD buffer=GlobalSize(lpSrc);
LONG Height1=DIBHeight(tmpDib);
LONG Width1=DIBWidth(tmpDib);
LONG Height2=DIBHeight(pDoc->m_pDib->m_hDib);
LONG Width2=DIBWidth(pDoc->m_pDib->m_hDib);
if((Height1!=Height2)||(Width1!=Width2))
{
AfxMessageBox("the height of width of tmp and src is not uniform!");
return;
}
// LPBYTE lptmpBits=(LPBYTE)FindDIBBits(lptmp);
// LPBYTE lpSrcBits=(LPBYTE)FindDIBBits(lpSrc);
int bytes=BytesPerLine(lpSrc);
DWORD imagesize=bytes*Height1;
LPBYTE lptmpBits=(LPBYTE)lptmp+buffer-imagesize;
LPBYTE lpSrcBits=(LPBYTE)lpSrc+buffer-imagesize;
double r=0.0,angle1=0.0,angle2=0.0,ii=0.0,jj=0.0,alfa=0.0,x0=0.0,y0=0.0,p1=0.0,p2=0.0,s=0.0,theta=0;
double m[4]={0.0,0.0,0.0,0.0};
struct result1 subposition[4000]={{0.0,0.0,0,0}};
int numbers=-1;
//start define the vars
int temp1=0;//=*(lptmpBits+i*bytes+j);
char temp2=0;//(char)0;
char gray2[7][7]={{(char)0}};//evaluate the picture to an array
int m_i=0,m_j=0;
char gray=(char)0;
double gray_t=0.0;
int gray_temp=0;
double gray_x=0.0,gray_y=0.0;
//end define the vars
// try
// {
for (int i=3;i<=Height1-4;i++)
{
for (int j=3;j<=Width1-4;j++)
{
temp1=*(lptmpBits+i*bytes+j);
// temp2=(char)0;
// if(temp1>1)
// temp2=(char)255;
// *(lpSrcBits+i*bytes+j)=temp2;
// int temp1=*(lptmpBits+i*bytes+j);
char temp2=*(lpSrcBits+i*bytes+j);
if(temp1>=10)
{
// gray2[7][7]={{(char)0}};//evaluate the picture to an array
m_i=0;
m_j=0;
for(m_i=0;m_i<7;m_i++)
{
for(m_j=0;m_j<7;m_j++)
{
gray2[m_i][m_j]=*(lpSrcBits+(i-3+m_i)*bytes+j-3+m_j);
}
}
m[0]=1.0;
m[1]=0.0;
m[2]=0.0;
m[3]=0.0;
s=theta=0.0;
p1=p2=0.0;
r=angle1=angle2=0.0;
alfa=x0=y0=0.0;
ii=i;jj=j;
gray=0;gray_t=0.0;
gray_x=0.0;gray_y=0.0;//initialize the parameters
m_i=m_j=0;
for(m_i=0;m_i<7;m_i++)
{
for(m_j=0;m_j<7;m_j++)
{
if((m_i==0 && m_j==0)||(m_i==0 && m_j==6)||(m_i==6 && m_j==0)||(m_i==6 && m_j==6))
gray=0;
else
gray=gray2[m_i][m_j];
gray_temp=gray2[m_i][m_j];
m[1]+=gray*m_subp1.element[m_i][m_j];
m[2]+=gray*gray*m_subp1.element[m_i][m_j];
m[3]+=gray*gray*gray*m_subp1.element[m_i][m_j];//to
//get the first present gray moments
gray_t+=gray_temp;
gray_x+=(m_j-3)*gray_temp;
gray_y+=(m_i-3)*gray_temp;//to get the gravity of the circle
}
}
theta=m[2]-m[1]*m[1];
s=(m[3]+2*m[1]*m[1]*m[1]-3*m[1]*m[2])/pow(theta,1.5);
p1=(1+s*sqrt(1/(4+s*s)))/2;
p2=1-p1;//get the p param
p1=(p1>p2)?p2:p1;
p1=equation(p1);
alfa=p1;
// p1*=1.5*pi
// alfa=pow(p1,1.0/3.0);
r=cos(alfa);//get the radium
r=3.5*r;
x0=gray_x/gray_t;
y0=gray_y/gray_t;
angle2=x0/(sqrt(x0*x0+y0*y0));//get the angle of the line
angle1=y0/(sqrt(x0*x0+y0*y0));
numbers++;
ii=i+r*angle1;
jj=j+r*angle2;//to get the real subpixel position
if(p2<=0.5 && p2>=0)
{
subposition[numbers].x=j+r*angle2;
subposition[numbers].y=i-r*angle1;
}
else
{
subposition[numbers].x=j-r*angle2;
subposition[numbers].y=i+r*angle1;
}
subposition[numbers].xx=j;
subposition[numbers].yy=i;//store them
//subposition[numbers].
}
}
}
// }
// catch (...)
// {
// TRACE("%d , %s\n",GetLastError(),DecodeError(GetLastError()));
// }
pDoc->m_pDib->m_hDib=CopyHandle(tmpDib);
GlobalUnlock(tmpDib);
GlobalFree(tmpDib);
pDoc->m_pDib->BuildBitmap();
pDoc->m_pDib->BuildPalette();
GlobalUnlock(pDoc->m_pDib->m_hDib);
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
saveData(subposition,numbers);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -