♥♥♥ Lê Nhung _ 48 Toán Tin ♥♥♥
ĐỀ CƯƠNG ƠN TẬP : XỬ LÝ ẢNH Serialize:
ar.Read(&bfHeader,sizeof(bfHeader));
if (bfHeader.bfType !=('M'<<8|'B'))
{MessageBox(NULL,"Khong phai tep BITMAP!","Thong bao",MB_OK);
return;}
int m,n;BYTE b;
ar.Read(&biHeader,sizeof(biHeader));
if (biHeader.biBitCount==8)
{int u;
for (u=0;u<256;u++)
ar.Read(&Colors[u],sizeof(RGBQUAD));
for (m=0;m<biHeader.biHeight;m++)
for (n=0;n<biHeader.biWidth;n++) { ar.Read(&b,sizeof(BYTE));
Data[m][n]=b ; } }
else
{MessageBox(NULL,"Khong xu ly anh khac 256 mau!","Thong bao",MB_OK);
return; }
View / On draw:
BYTE b;int m,n;
BYTE R,G,B;
for (m=0;m<pDoc->biHeader.biHeight;m++)
for (n=0;n<pDoc->biHeader.biWidth ;n++) {
b=pDoc->Data[m][n];
R=pDoc->Colors[b].rgbRed;
G=pDoc->Colors[b].rgbGreen;
B=pDoc->Colors[b].rgbBlue;
pDC->SetPixel(n,pDoc->biHeader.biHeight-m,
RGB(R,G,B));
}
Lấy âm bản:
C Doc* pDoc = GetDocument();
int m,n; int L=256;
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
for(m=0;m<M;m++)
for(n=0;n<N;n++)
pDoc->Data[m][n]= L- pDoc->Data[m][n]
Invalidate();
Hiện his
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n,u;
float H[256];
for(u=0;u<256;u++) H[u]=0;
for(m=0;m<M;m++)
for(n=0;n<N;n++)
H[pDoc->Data[m][n]]++;
for(u=0;u<256;u++) H[u]/=M*N;
CClientDC pDC(this);
int x=100,y=400;
for(u=0;u<256;u++)
{ pDC.MoveTo(x+u,y);
pDC.LineTo(x+u,y-H[u]*2000);}
Trang 2quay dọc:
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n;
BYTE X[1000][1000];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
pDoc->Data[m][n]=X[M-1-m]
[n];
Invalidate();
quay ngang
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n;
BYTE X[1000][1000];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n]; for(m=0;m<M;m++)
for(n=0;n<N;n++)
pDoc->Data[m][n]=X[m][N-1-n];
Invalidate();
quay trái:
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n,tg;
BYTE X[1000][1000];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n];
for(m=0;m<N;m++)
for(n=0;n<M;n++)
pDoc->Data[m][n]=X[n][N-1-m];
tg=pDoc->biHeader.biHeight;
pDoc->biHeader.biHeight=pDoc->biHeader.biWidth;
pDoc->biHeader.biWidth=tg;
Invalidate();
quay phải:
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n,tg;
BYTE X[1000][1000];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n]; for(m=0;m<N;m++)
for(n=0;n<M;n++)
pDoc->Data[m][n]=X[M-1-n] [m];
tg=pDoc->biHeader.biHeight;
pDoc->biHeader.biHeight=pDoc->biHeader.biWidth;
pDoc->biHeader.biWidth=tg;
Invalidate();
quay 180 độ:
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n;
BYTE X[1000][1000];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
pDoc->Data[m][n]=X[M-1-m][N-1-n];
Invalidate();
Khuếch đại ảnh bằng phương pháp lặp:
Trang 3C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n;
//chen them hang moi
for(m=M-1;m>=0;m )
for(n=0;n<N;n++) { pDoc->Data[2*m][n]=pDoc->Data[m][n];
pDoc->Data[2*m+1][n]=pDoc->Data[m][n];
} //chen them cot
for(n=N-1;n>=0;n )
for(m=0;m<2*M;m++) { pDoc->Data[m][2*n]=pDoc->Data[m][n];
pDoc->Data[m][2*n+1]=pDoc->Data[m][n];
} pDoc->biHeader.biHeight*=2;
pDoc->biHeader.biWidth*=2;
Invalidate()
Khuếch đại ảnh bằng phương pháp nội suy:
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n;
//Chen theo hang
for (m=M-1;m>=0;m )
for (n=0;n<N;n++)
{ pDoc->Data[2*m][n]=pDoc->Data[m][n];
if (m==M-1)
pDoc->Data[2*m+1][n]=pDoc->Data[m][n]/2;
else pDoc->Data[2*m+1][n]=(pDoc->Data[m][n]+pDoc->Data[m+1][n])/2;
}
//chen theo cot
for (n=N-1;n>=0;n )
for (m=0;m<2*M;m++)
{ pDoc->Data[m][2*n]=pDoc->Data[m][n];
if (n==N-1) pDoc->Data[m][2*n+1]=pDoc->Data[m][n]/2;
pDoc->Data[m][2*n+1]=(pDoc->Data[m][n]+pDoc->Data[m][n+1])/2;
} pDoc->biHeader.biHeight*=2;
pDoc->biHeader.biWidth*=2;
Invalidate();
Loc trung bình
Trang 4C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
BYTE X[1000][1000];
int m,n,k,l,lc=1;
int tong;
BYTE H[3][3]={{1,1,1},{1,1,1},{1,1,1}};
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n];
for(m=lc;m<M-lc;m++)
for(n=lc;n<N-lc;n++) { tong=0;
for(k=0;k<2*lc;k++)
for(l=0;l<2*lc;l++) tong+=H[k][l]*X[m-k+lc][n-l+lc];
pDoc->Data[m][n]=BYTE(tong/9);
} Invalidate();
Lọc trọng số
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
BYTE X[1000][1000];
int m,n,k,l,lc=1;
int tong;
BYTE H[3][3]={{1,1,1},{1,2,1},{1,1,1}};
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n];
for(m=lc;m<M-lc;m++)
for(n=lc;n<N-lc;n++) { tong=0;
for(k=0;k<2*lc;k++)
for(l=0;l<2*lc;l++) tong+=H[k][l]*X[m-k+lc][n-l+lc];
pDoc->Data[m][n]=BYTE(tong/10);
} Invalidate();
Lọc nhị phân Gauss
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
BYTE X[1000][1000];
int m,n,k,l,lc=1;
int tong;
BYTE H[3][3]={{1,2,1},{2,4,2},{1,2,1}};
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n];
Trang 5for(n=lc;n<N-lc;n++)
{ tong=0;
for(k=0;k<=2*lc;k++)
for(l=0;l<=2*lc;l++) tong+=H[k][l]*X[m-k+lc][n-l+lc];
pDoc->Data[m][n]=BYTE(tong/16);
}
Invalidate();
Loc giả trung vị
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int i,j;
int H[256],L=255;
BYTE Y[1000][1000];
int chiso,lc,k,l,K=3,d=3;
int min,max,m1,m2;
//giữ biên
for(i=0;i<M;i++)
for (j=0;j<N;j++)
Y[i][j]=pDoc->Data[i][j];
// tính các điểm trong biên
lc=(3-1)/2;
for (i=lc;i<=M-lc;i++)
for(j=lc;j<=N-lc;j++)
{chiso=0;
for (k=-lc;k<=lc;k++)
for (l=-lc;l<=lc;l++)
H[chiso++]=Y[i+k][j+l];
//tìm max trong các gtri min ( gtri m1)
m1=0;
for (k=0;k<=K*K-d;k++)
{ min=H[k];
for (l=k+1;l<k+d;l++)
if (min>H[l]) min=H[l];
if (m1<min) m1=min;
} //tính min trong các gtri max (gtri m2)
m2=L;
for (k=0;k<=K*K-d;k++)
{ max=H[k];
for (l=k+1;l<k+d;l++)
if (max<H[l]) max=H[l];
if (m2>max) m2=max;
} }
pDoc->Data[i][j]=(m1+m2)/2;
Invalidate();
THAY ĐỔI ĐỘ TƯƠNG PHẢN
C Doc* pDoc = GetDocument();
Trang 6int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int m,n,u;
float LUT[256];
int a=80,Va=120,b=255,Vb=255;
int L=255;
for(u=0;u<=L;u++)
{ if(u<=a)
LUT[u]=(u*Va/a);
else if(u<b)
LUT[u]=Va+(u-a)*(Vb-Va+1)/(b-a+1);
else
LUT[u]=Vb+(u-b)*(L-Vb+1)/(L-b+1);
}
for(m=0;m<M;m++)
for(n=0;n<N;n++) {
pDoc->Data[m][n]=LUT[pDoc->Data[m][n]];
} Invalidate();
PHÂN ẢNH THÀNH 2 VÙNG
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int t0; int L=255;
int tong1, tong2, m, n, n1, n2;
t0=L/2;
while(1)
{ tong1=tong2=0;
n1=n2=0;
for(m=0;m<M;m++)
for(n=0;n<N;n++)
if(pDoc->Data[m][n]<t0)
{ tong1+=pDoc->Data[m][n];n1++;}
else {tong2+=pDoc->Data[m][n];n2++;}
if(tong1/n1+tong2/n2==2*t0)
t0=(tong1/n1+tong2/n2)/2;
}
for(m=0;m<M;m++)
for(n=0;n<N;n++)
if(pDoc->Data[m][n]<t0)
pDoc->Data[m][n]=tong1/n1;
else pDoc->Data[m][n]=tong2/n2;
Invalidate();
PHÂN ẢNH THÀNH 3 VÙNG
C Doc* pDoc = GetDocument();
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int t1=80,t2=160;
int tong1=0,tong2=0,tong3=0;
Trang 7int m,n,n1=0,n2=0,n3=0;
for(m=0;m<M;m++)
for(n=0;n<N;n++)
if(pDoc->Data[m][n]<t1) { tong1+=pDoc->Data[m][n];
n1++;
} else if(pDoc->Data[m][n]<t2) {tong2+=pDoc->Data[m][n];
n2++;
} else { tong3+=pDoc->Data[m][n];
n3++;
} for(m=0;m<M;m++)
for(n=0;n<N;n++)
if(pDoc->Data[m][n]<t1)
pDoc->Data[m][n]=tong1/n1;
else if(pDoc->Data[m][n]<t2)
pDoc->Data[m][n]=tong2/n2;
else pDoc->Data[m][n]=tong3/n3;
Invalidate();
TOÁN TỬ GRADIENT
C Doc* pDoc = GetDocument();
int i,j,k,l;
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int lc=1,K=3,d=100;
int L=255;
BYTE dt[1000][1000];
int hm[3][3]={{1,1,1},{0,0,0},{-1,-1,-1}};
int hn[3][3]={{1,0,-1},{1,0,-1},{1,0,-1}};
// xet cac diem phia ngoai
for(i=0;i<M;i++)
for(j=0;j<N;j++)
if ( 2* pDoc->Data[i][j]>=d)
dt[i][j]=L;
else dt[i][j]=0;
// tinh lai cac diem phia trong
for(i=lc;i<M-lc;i++)
for(j=lc;j<N-lc;j++)
{int tong1=0;
int tong2=0;
for( k=0;k<=2*lc;k++)
for(l=0;l<=2*lc;l++) {tong1+=hm[k][l]*pDoc->Data[i-k+lc][j-l+lc];
tong2+=hn[k][l]*pDoc->Data[i-k+lc][j-l+lc];
} if( abs(tong1)+abs(tong2)>=d)
dt[i][j]=L;
else dt[i][j]=0;
}
Trang 8for(j=0;j<N;j++)
pDoc->Data[i][j]=dt[i][j];
Invalidate();
TOÁN TỬ LA BÀN
C Doc* pDoc = GetDocument();
int i,j,k,l;
BYTE dt[1000][1000];
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int h0[3][3]={{1,0,-1},{1,0,-1},{1,0,-1}};
int h1[3][3]={{0,-1,-1},{1,0,-1},{1,1,0}};
int h2[3][3]={{-1,-1,-1},{0,0,0},{1,1,1}};
int h3[3][3]={{-1,-1,0},{-1,0,1},{0,1,1}};
int h4[3][3]={{-1,0,1},{-1,0,1},{-1,0,1}};
int h5[3][3]={{0,1,1},{-1,0,1},{-1,-1,0}};
int h6[3][3]={{1,1,1},{0,0,0},{-1,-1,-1}};
int h7[3][3]={{1,1,0},{1,0,-1},{0,-1,-1}};
int x[8],max,d=80;
// tim diem co gia tri cao nhat
int lc=1;
int K=3;
// tao ban sao gt
for (i=0;i<M;i++)
for(j=0;j<N;j++)
dt[i][j]=pDoc->Data[i][j];
for (i=0;i<M;i++)
for(j=0;j<N;j++)
if (pDoc->Data[i][j]>d)
pDoc->Data[i][j]=255;
else pDoc->Data[i][j]=0;
//tinh lai cac diem phia trong
for(i=lc;i<M-lc;i++)
for(j=lc;j<N-lc;j++)
{ x[0]=x[1]=x[2]=x[3]=x[4]=x[5]=x[6]=x[7]=0;
for(k=0;k<=2*lc;k++)
for(l=0;l<=2*lc;l++) {
x[0]+=h0[k][l]*dt[i-k+lc][j-l+lc];
x[1]+=h1[k][l]*dt[i-k+lc][j-l+lc];
x[2]+=h2[k][l]*dt[i-k+lc][j-l+lc];
x[3]+=h3[k][l]*dt[i-k+lc][j-l+lc];
x[4]+=h4[k][l]*dt[i-k+lc][j-l+lc];
x[5]+=h5[k][l]*dt[i-k+lc][j-l+lc];
x[6]+=h6[k][l]*dt[i-k+lc][j-l+lc];
x[7]+=h7[k][l]*dt[i-k+lc][j-l+lc];
}
max=abs(x[0]);
for(k=1;k<8;k++)
if( max< abs(x[k]))
max= abs(x[k]);
if (max> d)
pDoc->Data[i][j]=255;
Trang 9else
pDoc->Data[i][j]=0;
}
Invalidate();
TOÁN TỬ LAPLACE
C Doc* pDoc = GetDocument();
int n,m,k,l;
int M=pDoc->biHeader.biHeight;
int N=pDoc->biHeader.biWidth;
int H[3][3]={{-1,2,-1},{2,-4,2},{-1,2,-1}};
BYTE X[1000][1000];
for(m=0;m<M;m++)
for(n=0;n<N;n++)
X[m][n]=pDoc->Data[m][n];
for(m=1;m<M-1;m++)
for(n=1;n<N-1;n++)
{
int tong =0;
for(k=0;k<2;k++)
for(l=0;l<2;l++) tong +=H[k][l]*pDoc->Data[m-k+1][n-l+1];
X[m][n]=tong;
}
for(m=0;m<M;m++)
for(n=0;n<N;n++)
pDoc->Data[m][n]=X[m][n];
Invalidate();