📄 filters.cpp
字号:
// Filters.cpp: implementation of the RxxFilters class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "fusion.h"#include "Filters.h"#include "GrayImageProcessing.h"#include <math.h>#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif#define twoPI 2 * 3.141592//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////RxxFilters::RxxFilters(){}RxxFilters::~RxxFilters(){ Empty(); }BYTE* RxxFilters::CannyProcessing(unsigned short* pGrayImage, int nCol, int nRow){ int nSizeOfImage = nCol*nRow; BYTE *pCanny = new BYTE[nSizeOfImage]; BYTE *pGradMag = new BYTE[nSizeOfImage]; ZeroMemory(pCanny,nSizeOfImage); ZeroMemory(pGradMag, nSizeOfImage); const int nMargin =2; int left = nMargin, right = nCol-nMargin; int top = nMargin, bottom = nRow-nMargin;// RxxGrayImageProcessing::Gradient(pGrayImage,left,right,top,bottom, nCol, pGradMag); //免仿蔼阑 焊咯林扁 困秦 函版 RxxGrayImageProcessing::CannyEdgeMSU(pGrayImage,nRow,nCol,pCanny); delete[] pGradMag; return pCanny;}BYTE* RxxFilters::GrayProcessing(){ return 0; }BYTE* RxxFilters::SobelProcessing(unsigned short *pGrayImage, int nCol, int nRow){ int nSizeOfImage = nCol*nRow; BYTE *pSobel = new unsigned char[nSizeOfImage]; ZeroMemory(pSobel, nSizeOfImage); int centerValue1=0; int centerValue2=0; int sum=0; CSize szRestMargin(nCol%4, nRow%4); const int nMargin =1; int left = nMargin, right = nCol-nMargin-szRestMargin.cx; int top = nMargin, bottom = nRow-nMargin-szRestMargin.cy; // unsigned char* pTempGray = new unsigned char[nSizeOfImage]; unsigned short* pTempGray = new unsigned short[nSizeOfImage]; ZeroMemory(pTempGray, nSizeOfImage*sizeof(unsigned short)); int mask1[9]={ -1,0,1,-2,0,2,-1,0,1}; int mask2[9]={ 1,2,1,0,0,0,-1,-2,-1}; memcpy(pTempGray, pGrayImage ,nSizeOfImage*sizeof(unsigned short));/* for(int row=top; row<bottom; row++){ for(int column=left; column<right; column++){ for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ centerValue1 += pTempGray[(i+row)*nCol+(j+column)]*mask1[i*3+j]; centerValue2 += pTempGray[(i+row)*nCol+(j+column)]*mask2[i*3+j]; } } sum=abs(centerValue1)+abs(centerValue2); if(sum>255)sum=255; /////yeni if(row==bottom-1 && column==right-1) TRACE(_T("stop\n")); ////// pSobel[(row+1)*nCol+(column+1)] = (unsigned char)sum; centerValue1=0; centerValue2=0; sum=0; } }*//// // 版拌蔼捞 肋给 贸府登菌澜. // 荐沥等 内靛绰 版拌蔼阑 公炼扒 0栏肺 贸府 (x=0, nRow-1, y=0, nCol-1) for(int row=top; row<bottom-1; row++){ for(int column=left; column<right-1; column++){ for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ centerValue1 += pTempGray[(i+row-1)*nCol+(j+column-1)]*mask1[i*3+j]; centerValue2 += pTempGray[(i+row-1)*nCol+(j+column-1)]*mask2[i*3+j]; } } sum=abs(centerValue1)+abs(centerValue2); if(sum>255)sum=255; pSobel[(row+1)*nCol+(column+1)] = (unsigned char)sum; centerValue1=0; centerValue2=0; sum=0; } }/// if(pTempGray) delete[] pTempGray; return pSobel;}void RxxFilters::Empty(){}void RxxFilters::convolution(unsigned short *data,int row, int col, double *convolutionMask,int mRow,int mCol){ int nSizeOfWindow=mRow*mCol; int half = mRow/2; int start = -half; int end=half; double weight=0; for(int i=0;i<nSizeOfWindow;i++) weight+=convolutionMask[i]; // allocate memeory unsigned short * imageTmp = new unsigned short[row*col]; memset(imageTmp, 0x00, sizeof(unsigned short)*row*col); int nIndexOffset=half*col; int nStartOffset=0; for(int r=half;r<row-half;r++){ int nIndex=nIndexOffset+half;// column start address int nStartIndex=nStartOffset;// column start address for(int c=half;c<col-half;c++,nStartIndex++){ double pVal=convolveMask(data, convolutionMask,nStartIndex,col,mCol); pVal=pVal/weight; imageTmp[nIndex++]=(unsigned short)pVal; } nIndexOffset+=col; // next row start address nStartOffset+=col; // next row start address } // copy from imagTmp to data// for(r=0;r<row;r++)// memcpy(&(data[r*col]),&(imageTmp[r*col]),sizeof(unsigned short)*col); memcpy(data,imageTmp,sizeof(unsigned short)*col*row); delete[] imageTmp; //release memory// delete []imageTmp;}double RxxFilters::convolveMask(unsigned short *pData,double *pConvolutionMask,int nStartIndex, int col, int mCol){ double sum=0; int nIndexMask=0; for(int nY=0;nY<mCol;nY++){ int nIndexData=nStartIndex; for(int nX=0;nX<mCol;nX++){ sum+=pData[nIndexData++]*pConvolutionMask[nIndexMask++]; } nStartIndex+=col; } if(sum <0) sum= -sum; return sum;}void RxxFilters::intConvolution(unsigned short *data,int row, int col, double *convolutionMask,int mRow,int mCol){ int nSizeOfWindow=mRow*mCol; int half = mRow/2; int start = -half; int end=half; double weight=0; for(int i=0;i<nSizeOfWindow;i++) weight+=convolutionMask[i]; // allocate memeory unsigned short * imageTmp = new unsigned short[row*col]; memset(imageTmp, 0x00, sizeof(unsigned short)*row*col); int nIndexOffset=half*col; int nStartOffset=0; for(int r=half;r<row-half;r++){ int nIndex=nIndexOffset+half;// column start address int nStartIndex=nStartOffset;// column start address for(int c=half;c<col-half;c++,nStartIndex++){ double pVal=convolveMask(data, convolutionMask,nStartIndex,col,mCol); pVal=pVal/weight; imageTmp[nIndex++]=(unsigned short)pVal; } nIndexOffset+=col; // next row start address nStartOffset+=col; // next row start address } // copy from imagTmp to data for(r=0;r<row;r++) memcpy(&(data[r*col]),&(imageTmp[r*col]),sizeof(unsigned short)*col); //release memory delete []imageTmp; }void RxxFilters::GaussianConvolution(unsigned short *data, int row, int col,int rMask, int cMask, double sigma){//memory alloc double * gaussMask; gaussMask = new double[rMask*cMask]; genGaussMask(gaussMask,rMask,cMask,sigma); convolution(data, row,col,gaussMask,rMask,cMask); //release memory delete gaussMask;}void RxxFilters::genGaussMask(double *gaussMask,int row,int col,double sigma){ int rCenter = row/2; int cCenter = col/2; int start = -rCenter; int end = rCenter+1; double twoSigmaSqWithConst; twoSigmaSqWithConst = -2*sigma*sigma; int nIndex1D=0; for(int i=start;i<end;i++) for(int j=start;j<end;j++) gaussMask[nIndex1D++] =gaussian(i,j,twoSigmaSqWithConst); }double RxxFilters::gaussian(double u,double v,double twoSigmaSqWithConst){ return(exp((u*u+v*v)/twoSigmaSqWithConst)/twoPI);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -