⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skc_bmp.cpp

📁 图像处理(各种基本图像算法的实现)包括各种几何变换
💻 CPP
字号:
// SKC_Bmp.cpp: implementation of the CSKC_Bmp class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "图象处理DIY.h"
#include "SKC_Bmp.h"
#include "math.h"
#define  WISTHBYTE(i)  (i+31)/32*4
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSKC_Bmp::CSKC_Bmp()
{
 m_nPixels=0;
 m_pInfo=NULL;
 m_pPixels=NULL;

}

CSKC_Bmp::~CSKC_Bmp()
{

}

void CSKC_Bmp::DrawDIB(CDC *pDC, int x, int y)
{ASSERT (pDC);
  HDC hdc=pDC->GetSafeHdc();
  if(m_pInfo)
     StretchDIBits(hdc, x,y,m_pInfo->bmiHeader.biWidth,
      m_pInfo->bmiHeader.biHeight,0,0,m_pInfo->bmiHeader.biWidth,
      m_pInfo->bmiHeader.biHeight,m_pPixels,m_pInfo,
       DIB_RGB_COLORS,SRCCOPY); 

}

BOOL CSKC_Bmp::Load(CString filename)
{
	ASSERT(filename);
	BOOL fReturn=TRUE;
	CFile dibFile(filename,CFile::modeRead);

	      delete[](BYTE*)m_pInfo;
		  delete[] m_pPixels;
		  m_pInfo=NULL;
		  m_pPixels=NULL;

		  DWORD dwStart=dibFile.GetPosition();
		  BITMAPFILEHEADER fileHeader;
		  dibFile.Read(&fileHeader,sizeof(BITMAPFILEHEADER));
		  BITMAPINFOHEADER infoHeader;
		  dibFile.Read(&infoHeader,sizeof(BITMAPINFOHEADER));

	int cInfo=sizeof(BITMAPINFOHEADER);
	m_nPixels=fileHeader.bfSize-fileHeader.bfOffBits;
	m_pInfo=(BITMAPINFO*)new BYTE[cInfo];
	memcpy(m_pInfo,&infoHeader,sizeof(BITMAPINFOHEADER));
	m_pPixels=new BYTE[m_nPixels];
	dibFile.Seek(dwStart+fileHeader.bfOffBits,CFile::begin);
	dibFile.Read(m_pPixels,m_nPixels);

	return fReturn;
}




void CSKC_Bmp::Color_To_Gray1()
{ 
	int nWidth=m_pInfo->bmiHeader.biWidth;
  int nHeight=m_pInfo->bmiHeader.biHeight;
  int LineByte=WIDTHBYTE(nWidth*24);
    for(int i=0;i<nHeight;i++)
       for(int j=0;j<LineByte;j+=3)
      { int min;
        min=m_pPixels[i*LineByte+j]<m_pPixels[i*LineByte+j+1]?m_pPixels[i*LineByte+j]:m_pPixels[i*LineByte+j+1];
        min=min<m_pPixels[i*LineByte+j+2]?min:m_pPixels[i*LineByte+j+2];
         m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=m_pPixels[i*LineByte+j+2]=min;
}

}

void CSKC_Bmp::Color_To_Gray2()
{
int nWidth=m_pInfo->bmiHeader.biWidth;
  int nHeight=m_pInfo->bmiHeader.biHeight;
  int LineByte=WIDTHBYTE(nWidth*24);
   for(int i=0;i<nHeight;i++)
       for(int j=0;j<LineByte;j+=3)
      {
          int temp=(int)(0.299*m_pPixels[i*LineByte+j]+
          0.587*m_pPixels[i*LineByte+j+1]+
          0.114*m_pPixels[i*LineByte+j+2]);
     m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=
     m_pPixels[i*LineByte+j+2]=temp;
	   }
}

void CSKC_Bmp::Color_To_Gray3()
{

 int nWidth=m_pInfo->bmiHeader.biWidth;
  int nHeight=m_pInfo->bmiHeader.biHeight;
  int LineByte=WIDTHBYTE(nWidth*24);
    for(int i=0;i<nHeight;i++)
       for(int j=0;j<LineByte;j+=3)
      { int max;
        max=m_pPixels[i*LineByte+j]>m_pPixels[i*LineByte+j+1]?m_pPixels[i*LineByte+j]:m_pPixels[i*LineByte+j+1];
        max=max>m_pPixels[i*LineByte+j+2]?max:m_pPixels[i*LineByte+j+2];
         m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=m_pPixels[i*LineByte+j+2]=max;
}
}

void CSKC_Bmp::Roberts()
{
 
	int nWidth=m_pInfo->bmiHeader.biWidth;
    int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    for(int i=1;i<nHeight-1;i++)
        for(int j=3;j<LineByte-1;j+=3)
	   { 
		  int a=m_pPixels[i*LineByte+j]-m_pPixels[(i+1)*LineByte+j+3];
		  int b=m_pPixels[(i+1)*LineByte+j]-m_pPixels[i*LineByte+j+3];
		  if(a<0) a=-a;
		  if(b<0) b=-b;
		  int c=a+b;
          
		m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=m_pPixels[i*LineByte+j+2]=c;
      
              
	   }
}

 void CSKC_Bmp::middle()
 {	int nWidth=m_pInfo->bmiHeader.biWidth;
     int nHeight=m_pInfo->bmiHeader.biHeight;
     int LineByte=WIDTHBYTE(nWidth*24);
     for(int i=1;i<nHeight-1;i++)
        for(int j=1;j<LineByte-1;j+=3)
 	   {    
              m_pPixels[i*LineByte+j]=MiddleFunc(i,j);
            
          
              m_pPixels[i*LineByte+j+1]=MiddleFunc(i,j+1);

         
             
              m_pPixels[i*LineByte+j+2]=MiddleFunc(i,j+2);
 
 
 	   }
 }



int CSKC_Bmp::MiddleFunc(int i,int j)
{   int nWidth=m_pInfo->bmiHeader.biWidth;
    int LineByte=WIDTHBYTE(nWidth*24);
	int a[9]={m_pPixels[(i-1)*LineByte+j-3],m_pPixels[(i-1)*LineByte+j],m_pPixels[(i-1)*LineByte+j+3],
		              m_pPixels[i*LineByte+j-3],m_pPixels[i*LineByte+j],m_pPixels[i*LineByte+j+3],
		              m_pPixels[(i+1)*LineByte+j-3],m_pPixels[(i+1)*LineByte+j],m_pPixels[(i+1)*LineByte+j+3]};
	for(int x=0;x<9;x++)
      for(int y=0;y<9;y++)
	     if (a[x]>a[y])
		 { int t=a[x];
		       a[x]=a[y];
			   a[y]=t;
		 }

return a[4];
}


void CSKC_Bmp::sobel()
{
   int nWidth=m_pInfo->bmiHeader.biWidth;
     int nHeight=m_pInfo->bmiHeader.biHeight;
     int LineByte=WIDTHBYTE(nWidth*8);
     int bit=WIDTHBYTE(nHeight*nWidth*8);
	 BYTE * p=new BYTE[bit];
    
     for(int i=1;i<nHeight-1;i++)
        for(int j=1;j<LineByte-1;j++)
		{
      int a=abs(m_pInfo->bmiColors[m_pPixels[(i-1)*LineByte+j-1]].rgbRed+2*m_pInfo->bmiColors[m_pPixels[(i-1)*LineByte+j]].rgbRed+m_pInfo->bmiColors[m_pPixels[(i-1)*LineByte+j+1]].rgbRed-
           m_pInfo->bmiColors[m_pPixels[(i+1)*LineByte+j-1]].rgbRed-2*m_pInfo->bmiColors[m_pPixels[(i+1)*LineByte+j]].rgbRed-m_pInfo->bmiColors[m_pPixels[(i+1)*LineByte+j+1]].rgbRed ); 

      int b=abs(m_pInfo->bmiColors[m_pPixels[(i-1)*LineByte+j-1]].rgbRed+m_pInfo->bmiColors[m_pPixels[(i+1)*LineByte+j-1]].rgbRed-
			m_pInfo->bmiColors[m_pPixels[(i-1)*LineByte+j+1]].rgbRed-2*m_pInfo->bmiColors[m_pPixels[i*LineByte+j+1]].rgbRed-m_pInfo->bmiColors[m_pPixels[(i+1)*LineByte+j+1]].rgbRed);
	 
	   m_pInfo->bmiColors[m_pPixels[i*LineByte+j]].rgbRed=
	   m_pInfo->bmiColors[m_pPixels[i*LineByte+j]].rgbGreen=
       m_pInfo->bmiColors[m_pPixels[i*LineByte+j]].rgbBlue=a>b?a:b;
	    
		}	
	
	}

void CSKC_Bmp::chuizhi()
{
   int nWidth=m_pInfo->bmiHeader.biWidth;
     int nHeight=m_pInfo->bmiHeader.biHeight;
     int LineByte=WIDTHBYTE(nWidth*24);
    for(int j=0;j<LineByte;j++) 
	   for(int i=0;i<nHeight/2;i++)
        {  
		 int temp;
			temp=m_pPixels[i*LineByte+j];
            m_pPixels[i*LineByte+j]=m_pPixels[( nHeight-i-1)*LineByte+j];
           m_pPixels[( nHeight-i-1)*LineByte+j]=temp;
	   }
           
}

void CSKC_Bmp::shuiping()
{
     int nWidth=m_pInfo->bmiHeader.biWidth;
     int nHeight=m_pInfo->bmiHeader.biHeight;
     int LineByte=WIDTHBYTE(nWidth*24);
     for(int i=0;i<nHeight;i++)
        for(int j=0;j<LineByte/2;j+=3)
		{   int temp;
			temp=m_pPixels[i*LineByte+j];
            m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+(3*nWidth-j-3)];
			m_pPixels[i*LineByte+(3*nWidth-j-3)]=temp;

            temp=m_pPixels[i*LineByte+j+1];
            m_pPixels[i*LineByte+j+1]=m_pPixels[i*LineByte+(3*nWidth-j-2)];
			m_pPixels[i*LineByte+(3*nWidth-j-2)]=temp;

            temp=m_pPixels[i*LineByte+j+2];
            m_pPixels[i*LineByte+j+2]=m_pPixels[i*LineByte+(3*nWidth-j-1)];
			m_pPixels[i*LineByte+(3*nWidth-j-1)]=temp;
		}


}

void CSKC_Bmp::FenGe()
{
	int nWidth=m_pInfo->bmiHeader.biWidth;
     int nHeight=m_pInfo->bmiHeader.biHeight;
     int LineByte=WIDTHBYTE(nWidth*24);
	 int value;
	 float n[256],p[256];
		  
     float Wo=0.0,W1=0.0,Uo=0.0,U1=0.0, Q2=0.0,N=0.0;
		 for(int i=0;i<256;i++) { n[i]=0.0 ;p[i]=0.0 ;};
		
	 for( i=0;i<nHeight;i++) for(int j=0;j<LineByte;j+=3)	n[m_pPixels[i*LineByte+j]]++;
   
	 for( i=0;i<256;i++)   N=N+n[i];
	 for( i=0;i<256;i++)   p[i]=n[i]/N;

     float max=0.0;
	 for(int k=0;k<256;k++)
	 {   
		 for( i=0;i<k;i++)  Wo=Wo+p[i];
            W1=1-Wo;
		 for( i=0;i<k;i++)  Uo=Uo+(i*p[i])/Wo;
		 for( i=k+1;i<256;i++)	U1=U1+(i*p[i])/W1;
		  Q2=Wo*W1*(U1-Uo)*(U1-Uo);
		  
		  if(Q2>max)   { max=Q2; value=k; } 

	 
	 
	 }
	 
      for( i=0;i<nHeight;i++)
        for(int j=0;j<LineByte;j+=3) 
		{
			if(m_pPixels[i*LineByte+j]<value) 
              m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=m_pPixels[i*LineByte+j+2]=0;
          else
			   m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=m_pPixels[i*LineByte+j+2]=255;
		}
}


int CSKC_Bmp::JJ(int *a,int i,int j)
{     int c=0;
     int nWidth=m_pInfo->bmiHeader.biWidth;
    int LineByte=WIDTHBYTE(nWidth*24);
	int b[9]={m_pPixels[(i-1)*LineByte+j-3],m_pPixels[(i-1)*LineByte+j],m_pPixels[(i-1)*LineByte+j+3],
		              m_pPixels[i*LineByte+j-3],m_pPixels[i*LineByte+j],m_pPixels[i*LineByte+j+3],
		              m_pPixels[(i+1)*LineByte+j-3],m_pPixels[(i+1)*LineByte+j],m_pPixels[(i+1)*LineByte+j+3]};
	
    for(int x=0;x<9;x++)
	 c=c+a[x]*b[x];
	if(abs(c)>255)c=255;
	return abs(c);
							
}

void CSKC_Bmp::Prewitt()
{
     int nWidth=m_pInfo->bmiHeader.biWidth;
     int nHeight=m_pInfo->bmiHeader.biHeight;
     int LineByte=WIDTHBYTE(nWidth*24);
	 int bit=WIDTHBYTE(nHeight*nWidth*24);
	 
	 p1=new BYTE[bit];
     p2=new BYTE[bit];
	 int a[9]={-1,0,1,-1,0,1,-1,0,1},c1=0;    //水平模板;
	 int b[9]={1,1,1,0,0,0,-1,-1,-1},c2=0;    //垂直模板;
     for(int i=1;i<nHeight-1;i++)
        for(int j=3;j<LineByte-1;j+=3)
		{  c1=JJ(a,i,j);                    //水平卷积
           c2=JJ(b,i,j);                    //垂直卷积
           
		   p1[i*LineByte+j]=p1[i*LineByte+j+1]=p1[i*LineByte+j+2]=c1;
           p2[i*LineByte+j]=p2[i*LineByte+j+1]=p2[i*LineByte+j+2]=c2;
		}	 
for(i=0;i<bit;i++)  m_pPixels[i]=p1[i]>p2[i]?p1[i]:p2[i];

delete []p1;    
delete []p2;

}



void CSKC_Bmp::Kirsch()
{
   int nWidth=m_pInfo->bmiHeader.biWidth;
     int nHeight=m_pInfo->bmiHeader.biHeight;
     int LineByte=WIDTHBYTE(nWidth*24);
 int bit=WIDTHBYTE(nHeight*nWidth*24);
	 p1=new BYTE[bit];
     int a1[9]={5,5,5,-3,0,-3,-3,-3,-3}; 
	 int a2[9]={-3,5,5,-3,0,5,-3,-3,-3};
	 int a3[9]={-3,-3,5,-3,0,5,-3,-3,5};
	 int a4[9]={-3,-3,-3,-3,0,5,-3,5,5};
	 int a5[9]={-3,-3,-3,-3,0,-3,5,5,5};
	 int a6[9]={-3,-3,-3,5,0,-3,5,5,-3};
	 int a7[9]={5,-3,-3,5,0,-3,5,-3,-3};
	 int a8[9]={5,5,-3,5,0,-3,-3,-3,-3};
	 int c[9];
	 for(int i=0;i<8;i++) c[i]=0;
     for( i=1;i<nHeight-1;i++)
        for(int j=3;j<LineByte-1;j+=3)
		{
           c[0]=JJ(a1,i,j);                   
           c[1]=JJ(a2,i,j);  
		   c[2]=JJ(a3,i,j);                   
           c[3]=JJ(a4,i,j);  
		   c[4]=JJ(a5,i,j);                   
           c[5]=JJ(a6,i,j);  
		   c[6]=JJ(a7,i,j);                   
           c[7]=JJ(a8,i,j); 
		   int max=c[0];
		   for(int x=0;x<8;x++)
		   max=max>c[x]?max:c[x];
		     
		  p1[i*LineByte+j]=p1[i*LineByte+j+1]=p1[i*LineByte+j+2]=max;

        }
for(i=0;i<bit;i++)  m_pPixels[i]=p1[i];

delete []p1; 
}

void CSKC_Bmp::suoxiao()
{	int nWidth=m_pInfo->bmiHeader.biWidth;
    int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
	int bit=WIDTHBYTE(nHeight*nWidth*6);
    p1=new BYTE[bit];
    for(int i=0;i<nHeight/2;i++)
       for(int j=0;j<LineByte/2;j+=3)
	   {
	   p1[i*LineByte/2+j]=m_pPixels[2*i*LineByte+2*j];
	   p1[i*LineByte/2+j+1]=m_pPixels[2*i*LineByte+2*j+1];
	   p1[i*LineByte/2+j+2]=m_pPixels[2*i*LineByte+2*j+2];
	   
	   }

m_pPixels=p1;

m_pInfo->bmiHeader.biWidth=nWidth/2;
m_pInfo->bmiHeader.biHeight=nHeight/2;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -