图像平移、上下翻转、左右翻转

    技术2022-05-11  17

    图像平移 

    图像的平移是对源图像的一个位置上的移动,比较简单,下面贴出代码,是对8位或24位的位图的。 平移前后的对比:   void CICETIMDlg::OnBtnImagemove() {  // TODO: Add your control notification handler code here  if(m_dib.GetBitCount()==0)  {   AfxMessageBox("请先打开位图");   return;  }  int x,y;  CImagePingYiDlg dlg=new CImagePingYiDlg();  if(dlg.DoModal()==IDOK)  {   UpdateData(true);   x=dlg.m_x;   y=dlg.m_y;  }  else  {   return;  }  int nHeight=m_dib.GetHeight();  int lLineBytes=m_dib.GetLineBytes();  int nWidth=m_dib.GetWidth();  int nBitCount=m_dib.GetBitCount();  BYTE* image=m_dib.GetDibData();  BYTE* newImage=new BYTE[nHeight*lLineBytes];  memset(newImage,0,nHeight*lLineBytes);  if(nBitCount==8)  {   for(int i=0;i<(nHeight-y);i++)   {    for(int j=0;j<(nWidth-x);j++)    {     BYTE B=*(image+lLineBytes*(nHeight-1-i)+j);     *(newImage+lLineBytes*(nHeight-1-i-y)+j+x)=B;    }   }  }  else if(nBitCount==24)  {   for(int i=0;i<(nHeight-y);i++)   {    for(int j=0;j<(nWidth-x)*3;j++)    {     //获得图片上第i行,第j列的像素的RGB分量值     BYTE B=*(image+lLineBytes*(nHeight-1-i)+j);     j++;     BYTE G=*(image+lLineBytes*(nHeight-1-i)+j);     j++;     BYTE R=*(image+lLineBytes*(nHeight-1-i)+j);         //     *(newImage+lLineBytes*(nHeight-1-i-y)+(j+x*3))=R;     j--;     *(newImage+lLineBytes*(nHeight-1-i-y)+(j+x*3))=G;     j--;     *(newImage+lLineBytes*(nHeight-1-i-y)+(j+x*3))=B;     j++;     j++;    }   }  }  else  {   AfxMessageBox("暂时只能处理8或24位位图");   return;  }  m_dib.SetDibData(newImage);  ShowImage(m_dib,"图像平移"); }

    图像的左右翻转

    图像的上下翻转和左右翻转也是比较简单的,就不多说了。直接给出代码: void CICETIMDlg::OnBtnLrflipimage() {  // TODO: Add your control notification handler code here  if(m_dib.GetBitCount()==0)  {   AfxMessageBox("请先打开位图");   return;  }  int nBitCount=m_dib.GetBitCount();  int nHeight=m_dib.GetHeight();  int nWidth=m_dib.GetWidth();  long lLineBytes=m_dib.GetLineBytes();  BYTE* image=m_dib.GetDibData();  BYTE* newImage=new BYTE[nHeight*lLineBytes];  if(nBitCount==8)  {   for(int i=0;i<nHeight;i++)   {    for(int j=0;j<nWidth;j++)    {     BYTE B=*(image+lLineBytes*i+j);     *(newImage+lLineBytes*i+(nWidth-1-j))=B;    }   }  }  else if(nBitCount==24)  {   for(int i=0;i<nHeight;i++)   {    for(int j=0;j<nWidth*3;j++)    {     BYTE B=*(image+lLineBytes*i+j);     j++;     BYTE G=*(image+lLineBytes*i+j);     j++;     BYTE R=*(image+lLineBytes*i+j);     *(newImage+lLineBytes*i+(nWidth*3-1-j))=B;     j--;     *(newImage+lLineBytes*i+(nWidth*3-1-j))=G;     j--;     *(newImage+lLineBytes*i+(nWidth*3-1-j))=R;     j++;     j++;    }   }  }  else  {   AfxMessageBox("暂时只能处理8或24位位图");   return;  }  m_dib.SetDibData(newImage);  ShowImage(m_dib,"左右翻转"); }

    图像的上下翻转

    void CICETIMDlg::OnBtnUdflipimage() {  // TODO: Add your control notification handler code here  if(m_dib.GetBitCount()==0)  {   AfxMessageBox("请先打开位图");   return;  }  int nBitCount=m_dib.GetBitCount();  int nHeight=m_dib.GetHeight();  int nWidth=m_dib.GetWidth();  long lLineBytes=m_dib.GetLineBytes();  BYTE* image=m_dib.GetDibData();  BYTE* newImage=new BYTE[nHeight*lLineBytes];  if(nBitCount==8)  {   for(int i=0;i<nHeight;i++)   {    for(int j=0;j<nWidth;j++)    {     BYTE B=*(image+lLineBytes*i+j);     *(newImage+lLineBytes*(nHeight-1-i)+j)=B;    }   }  }  else if(nBitCount==24)  {   for(int i=0;i<nHeight;i++)   {    for(int j=0;j<nWidth*3;j++)    {     BYTE B=*(image+lLineBytes*i+j);     j++;     BYTE G=*(image+lLineBytes*i+j);     j++;     BYTE R=*(image+lLineBytes*i+j);     *(newImage+lLineBytes*(nHeight-1-i)+j)=R;     j--;     *(newImage+lLineBytes*(nHeight-1-i)+j)=G;     j--;     *(newImage+lLineBytes*(nHeight-1-i)+j)=B;     j++;     j++;    }   }  }  else  {   AfxMessageBox("暂时只能处理8或24位位图");   return;  }  m_dib.SetDibData(newImage);  ShowImage(m_dib,"上下翻转"); }

    最新回复(0)